feat(templateGeneratorWorker): ci/cd, основной метод вынесен в отдельный класс.

This commit is contained in:
Mikhail Kuznetsov
2025-08-19 11:13:56 +10:00
parent 2c005f038e
commit 963da184f1
9 changed files with 202 additions and 127 deletions

View File

@@ -13,6 +13,7 @@ variables:
PROD_NAME_NEXT_RUN: "parr/parr-next-run"
PROD_TEMPLATE_DISTRIBOTOR: "parr/parr-template-distributor"
PROD_TEMPLATE_ACTIVATOR: "parr/parr-template-activator"
PROD_TEMPLATE_GENERATOR: "parr/parr-template-generator"
PROD_JOB_AUTO_CONTROL: "parr/parr-job-auto-control"
stages:
@@ -659,6 +660,54 @@ prod_template_activator_deploy:
- ${RUNNER}
### TEMPLATE GENERATOR PROD ###
prod_template_generator_build:
stage: build
only:
- /^tg[0-9]+\.[0-9]+\.[0-9]+$/
except:
- branches
services:
- name: docker:20.10.21-dind
command: [
"--insecure-registry=10.99.253.167:8090",
"--registry-mirror=http://10.99.253.167:8090",
"--insecure-registry=harbor.dvgd.rzd",
"--tls=false"
]
variables:
DOCKER_HOST: tcp://docker:2375
DOCKER_DRIVER: overlay2
DOCKER_TLS_CERTDIR: ""
script:
- APP_VERSION=$(echo $CI_COMMIT_TAG | tr -d tg)
- IMAGE_VERSION=$(echo $CI_COMMIT_TAG | sed 's/tg/v/g')
- docker build -t $REPO/$PROD_TEMPLATE_GENERATOR:$IMAGE_VERSION -t $REPO/$PROD_TEMPLATE_GENERATORR:latest -t $PROD_TEMPLATE_GENERATOR:$IMAGE_VERSION -t $PROD_TEMPLATE_GENERATOR:latest --build-arg app_version=$APP_VERSION -f PARR.TemplateGeneratorWorker/Dockerfile .
- docker login -u $HARBOR_PUSH_USER -p $HARBOR_PUSH_PASS $REPO
- docker push --all-tags $REPO/$PROD_TEMPLATE_GENERATOR
tags:
- docker
prod_template_generator_deploy:
stage: deploy
environment:
name: parr-template-generator
only:
- /^tg[0-9]+\.[0-9]+\.[0-9]+$/
except:
- branches
script:
- IMAGE_VERSION=$(echo $CI_COMMIT_TAG | sed 's/tg/v/g')
- docker login -u $HARBOR_PULL_USER -p $HARBOR_PULL_PASS $REPO
- tag=$IMAGE_VERSION docker stack deploy -c docker-compose.template-generator.yml parr-template-generator --with-registry-auth
parallel:
matrix:
- RUNNER: shell-api-swarm-01
tags:
- ${RUNNER}
### JOB AUTO CONTROL PROD ###
prod_job_auto_control_build:
stage: build

View File

@@ -1,28 +0,0 @@
# See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.
# This stage is used when running from VS in fast mode (Default for Debug configuration)
FROM mcr.microsoft.com/dotnet/runtime:7.0 AS base
WORKDIR /app
# This stage is used to build the service project
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["NuGet.config", "."]
COPY ["PARR.TemplateGeneratorWorker/PARR.TemplateGeneratorWorker.csproj", "PARR.TemplateGeneratorWorker/"]
RUN dotnet restore "./PARR.TemplateGeneratorWorker/PARR.TemplateGeneratorWorker.csproj"
COPY . .
WORKDIR "/src/PARR.TemplateGeneratorWorker"
RUN dotnet build "./PARR.TemplateGeneratorWorker.csproj" -c $BUILD_CONFIGURATION -o /app/build
# This stage is used to publish the service project to be copied to the final stage
FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./PARR.TemplateGeneratorWorker.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
# This stage is used in production or when running from VS in regular mode (Default when not using the Debug configuration)
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "PARR.TemplateGeneratorWorker.dll"]

View File

@@ -0,0 +1,7 @@
namespace PARR.TemplateGeneratorWorker
{
public interface ITemplateGenerator
{
Task GenerateTemplateAsync(string msg);
}
}

View File

@@ -6,57 +6,46 @@ namespace PARR.TemplateGeneratorWorker.Services
{
internal class ValidatorService : IValidatorService
{
private readonly IServiceProvider serviceProvider;
private readonly ILogger<ValidatorService> logger;
private readonly IJobService jobService;
private readonly IUnitService unitService;
public ValidatorService(
IServiceProvider serviceProvider,
ILogger<ValidatorService> logger
ILogger<ValidatorService> logger,
IJobService jobService,
IUnitService unitService
)
{
this.serviceProvider = serviceProvider;
this.logger = logger;
this.jobService = jobService;
this.unitService = unitService;
}
public async Task<bool> IsValidAsync(Guid jobId, Guid unitId)
{
using (var scope = serviceProvider.CreateScope())
var job = await jobService
.Get().AsNoTracking()
.FirstOrDefaultAsync(t => t.Id == jobId);
if (job == null)
{
var jobService = GetServiceInScope<IJobService>(scope);
var job = await jobService
.Get().AsNoTracking()
.FirstOrDefaultAsync(t => t.Id == jobId);
if (job == null)
{
logger.LogError($"Не найдена регалментная работа {nameof(jobId)}: {jobId}");
return false;
}
var unitService = GetServiceInScope<IUnitService>(scope);
var unit = await unitService
.Get().AsNoTracking()
.FirstOrDefaultAsync(t => t.Id == unitId);
if (unit == null)
{
logger.LogError($"Не найден элемент конфигурации {nameof(unitId)}: {unitId}");
return false;
}
return true;
logger.LogError($"Не найдена регалментная работа {nameof(jobId)}: {jobId}");
return false;
}
}
var unit = await unitService
.Get().AsNoTracking()
.FirstOrDefaultAsync(t => t.Id == unitId);
private Service GetServiceInScope<Service>(IServiceScope scope)
{
var service = scope.ServiceProvider.GetService<Service>();
if (service == null)
throw new Exception($"Не найден сервис: {nameof(Service)}");
if (unit == null)
{
logger.LogError($"Не найден элемент конфигурации {nameof(unitId)}: {unitId}");
return false;
}
return service;
return true;
}
}
}

View File

@@ -0,0 +1,86 @@

using Microsoft.EntityFrameworkCore;
using PARR.BLL.Domain.Mq;
using PARR.BLL.Services.Interfaces;
using PARR.Common.Domain;
using PARR.Constants;
using PARR.DAL.DomainServices.Interfaces;
using PARR.DAL.Models;
using PARR.DAL.Models.Job;
using PARR.DAL.Models.Unit;
using PARR.DAL.Services.Interfaces;
using PARR.DAL.Services.Interfaces.Job;
using PARR.TemplateGeneratorWorker.Services;
namespace PARR.TemplateGeneratorWorker
{
internal class TemplateGenerator : ITemplateGenerator
{
private readonly ILogger<TemplateGenerator> logger;
private readonly ITransformService transformService;
private readonly IValidatorService validatorService;
private readonly IJobService jobService;
private readonly IShortcodesService shortcodesService;
private readonly ITemplateService templateService;
public TemplateGenerator(
ILogger<TemplateGenerator> logger,
ITransformService transformService,
IValidatorService validatorService,
IJobService jobService,
IShortcodesService shortcodesService,
ITemplateService templateService
)
{
this.logger = logger;
this.transformService = transformService;
this.validatorService = validatorService;
this.jobService = jobService;
this.shortcodesService = shortcodesService;
this.templateService = templateService;
}
public async Task GenerateTemplateAsync(string msg)
{
logger.LogInformation($"Получили запрос: {msg}");
var query = transformService.GetModelFromJson<TemplateGeneratorWorkerMq>(msg);
if (query == null)
return;
if (!await validatorService.IsValidAsync(query.JobId, query.UnitId))
{
logger.LogError($"Некорректные параметры регламентной работы или Unit {nameof(Job)}: {query.JobId}, {nameof(Unit)}: {query.UnitId}");
return;
}
var job = await jobService
.Get().AsNoTracking()
.FirstOrDefaultAsync(t => t.Id == query.JobId);
var templateName = await shortcodesService.ApplyShortcodesAsync(job!.TemplateNameMask!, query.UnitId, query.JobId);
var template = new Template
{
Id = Guid.NewGuid(),
Name = templateName,
UnitId = query.UnitId,
JobId = query.JobId,
IsActiveTemplate = query.IsActiveTemplate ?? false,
IsActiveSchedule = query.IsActiveSchedule ?? false,
InitiatorComment = query.HistoryInitiator?.InitiatorComment,
InitiatorParrComponentId = query.HistoryInitiator?.InitiatorParrComponentId
};
if (!await templateService.CreateAsync(template) || !await templateService.CommitAsync(new HistoryInitiator { InitiatorComment = "Запрос на генерацию с тестового шаблона", InitiatorParrComponentId = ParrComponentsEnum.TemplateGenerator }))
{
logger.LogError($"Ошибка при создании шаблона: Name: {template.Name}, {nameof(template.Job)}: {template.Job}, {nameof(template.Unit)}: {template.UnitId}");
}
else
{
logger.LogInformation($"Создан шаблон: Name: {template.Name}, {nameof(template.Job)}: {template.JobId}, {nameof(template.Unit)}: {template.UnitId}");
}
}
}
}

View File

@@ -16,8 +16,7 @@ namespace PARR.TemplateGeneratorWorker
configuration.GetSection(nameof(MqSettings)).Bind(mqSettings);
services.AddSingleton(mqSettings);
//services.AddTransient<ITemplateActivator, TemplateActivator>();
//services.AddTransient<IMqTemplateActivator, MqTemplateActivator>();
services.AddTransient<ITemplateGenerator, TemplateGenerator>();
services.AddTransient<IValidatorService, ValidatorService>();
}

View File

@@ -1,11 +1,6 @@
using Microsoft.EntityFrameworkCore;
using PARR.BLL.Contracts.Interfaces;
using PARR.BLL.Domain.Mq;
using PARR.BLL.Services.Interfaces;
using PARR.Common.Domain;
using PARR.Constants;
using PARR.DAL.DomainServices.Interfaces;
using PARR.DAL.Models;
using PARR.DAL.Services.Interfaces;
using PARR.DAL.Services.Interfaces.Job;
using PARR.TemplateGeneratorWorker.Services;
@@ -18,24 +13,23 @@ namespace PARR.TemplateGeneratorWorker
private readonly ILogger<Worker> logger;
private readonly IMqSettings mqSettings;
private readonly IMqService mqService;
private readonly IServiceProvider serviceProvider;
private readonly ITransformService transformService;
private readonly IValidatorService validatorService;
private readonly IServiceProvider serviceProvider;
private readonly IJobService jobService;
private readonly IShortcodesService shortcodesService;
private readonly ITemplateService templateService;
public Worker(
ILogger<Worker> logger,
MqSettings mqSettings,
IMqService mqService,
ITransformService transformService,
IValidatorService validatorService,
IServiceProvider serviceProvider
)
{
this.logger = logger;
this.mqSettings = mqSettings;
this.mqService = mqService;
this.transformService = transformService;
this.validatorService = validatorService;
this.serviceProvider = serviceProvider;
}
@@ -61,61 +55,14 @@ namespace PARR.TemplateGeneratorWorker
private async Task GenerateTemplateAsync(string msg)
{
logger.LogInformation($"{this.GetType().Name}. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: {msg}");
var query = transformService.GetModelFromJson<TemplateGeneratorWorkerMq>(msg);
if (query == null)
return;
if (!await validatorService.IsValidAsync(query.JobId, query.UnitId))
{
logger.LogError($"<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> {nameof(query.JobId)}: {query.JobId}");
return;
}
using (var scope = serviceProvider.CreateScope())
{
var jobService = GetServiceInScope<IJobService>(scope);
var job = await jobService
.Get().AsNoTracking()
.FirstOrDefaultAsync(t => t.Id == query.JobId);
var shortcodesService = GetServiceInScope<IShortcodesService>(scope);
var templateName = await shortcodesService.ApplyShortcodesAsync(job!.TemplateNameMask!, query.UnitId, query.JobId);
var template = new Template
{
Id = Guid.NewGuid(),
Name = templateName,
UnitId = query.UnitId,
JobId = query.JobId,
IsActiveTemplate = query.IsActiveTemplate ?? false,
IsActiveSchedule = query.IsActiveSchedule ?? false,
InitiatorComment = query.HistoryInitiator?.InitiatorComment,
InitiatorParrComponentId = query.HistoryInitiator?.InitiatorParrComponentId
};
var templateService = GetServiceInScope<ITemplateService>(scope);
if (!await templateService.CreateAsync(template) || !await templateService.CommitAsync(new HistoryInitiator { InitiatorComment = "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>", InitiatorParrComponentId = ParrComponentsEnum.TemplateGenerator }))
{
logger.LogError($"<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: Name: {template.Name}, {nameof(template.Job)}: {template.JobId}, {nameof(template.Unit)}: {template.UnitId}");
}
else
{
logger.LogInformation($"<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: Name: {template.Name}, {nameof(template.Job)}: {template.JobId}, {nameof(template.Unit)}: {template.UnitId}");
}
var templateGenerator = scope.ServiceProvider.GetService<ITemplateGenerator>();
if (templateGenerator == null)
throw new Exception($"<22><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: {nameof(ITemplateGenerator)}");
await templateGenerator.GenerateTemplateAsync(msg);
}
}
private Service GetServiceInScope<Service>(IServiceScope scope)
{
var service = scope.ServiceProvider.GetService<Service>();
if (service == null)
throw new Exception($"<22><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: {nameof(Service)}");
return service;
}
}
}

View File

@@ -9,6 +9,7 @@
<DockerServiceName>parr.api</DockerServiceName>
</PropertyGroup>
<ItemGroup>
<None Include="docker-compose.template-generator.yml" />
<None Include="docker-compose.aihit-loader.yml" />
<None Include="docker-compose.aihit-relationships-syncer.yml" />
<None Include="docker-compose.aihit-syncer.yml" />

View File

@@ -0,0 +1,25 @@
version: '3.4'
#PARR TEMPLATE ACTIVATOR
services:
parr-template-activator:
image: harbor.dvgd.rzd/parr/parr-template-generator:${tag-latest}
environment:
- ASPNETCORE_ENVIRONMENT=Production
- TZ=Europe/Moscow
logging:
driver: fluentd
options:
fluentd-address: dvgd-efk-01.dvgd.oao.rzd:24224
fluentd-retry-wait: '30s'
fluentd-max-retries: '30'
tag: parr.template-generator.serilog
deploy:
replicas: 4
networks:
parr-network:
networks:
parr-network:
driver: overlay
external: true