feat(templateGeneratorWorker): Создан Worker читающий очередь и генерирующий новые объекты Template
This commit is contained in:
28
PARR.TemplateGeneratorWorker/Dockerfile
Normal file
28
PARR.TemplateGeneratorWorker/Dockerfile
Normal file
@@ -0,0 +1,28 @@
|
||||
# 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"]
|
||||
@@ -0,0 +1,25 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Worker">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<UserSecretsId>dotnet-PARR.TemplateGeneratorWorker-4ea24dbb-6876-46db-9b6e-68e93850120e</UserSecretsId>
|
||||
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Elastic.CommonSchema.Serilog" Version="8.6.1" />
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
|
||||
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.22.1-Preview.1" />
|
||||
<PackageReference Include="Serilog.Extensions.Hosting" Version="7.0.0" />
|
||||
<PackageReference Include="Serilog.Settings.Configuration" Version="7.0.0" />
|
||||
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
|
||||
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\PARR.BLL\PARR.BLL.csproj" />
|
||||
<ProjectReference Include="..\PARR.DAL\PARR.DAL.csproj" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
30
PARR.TemplateGeneratorWorker/Program.cs
Normal file
30
PARR.TemplateGeneratorWorker/Program.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using Elastic.CommonSchema.Serilog;
|
||||
using PARR.TemplateGeneratorWorker;
|
||||
using Serilog;
|
||||
|
||||
var builder = Host.CreateApplicationBuilder();
|
||||
|
||||
builder.Services.AddLogging(config =>
|
||||
{
|
||||
config.ClearProviders();
|
||||
|
||||
var logger = new LoggerConfiguration();
|
||||
|
||||
if (builder.Environment.IsProduction())
|
||||
logger.WriteTo.Console(new EcsTextFormatter());
|
||||
else
|
||||
logger.WriteTo.Console();
|
||||
|
||||
logger.ReadFrom.Configuration(builder.Configuration);
|
||||
|
||||
config.AddSerilog(logger.CreateLogger());
|
||||
});
|
||||
|
||||
builder.Services.InstallTemplateGeneratorWorkerSerivces(builder.Configuration);
|
||||
builder.Configuration.AddTemplateGeneratorWorkerConfigurations(builder.Services);
|
||||
builder.Services.AddTemplateGeneratorWorkerSettings(builder.Configuration);
|
||||
|
||||
builder.Services.AddHostedService<Worker>();
|
||||
|
||||
var host = builder.Build();
|
||||
host.Run();
|
||||
14
PARR.TemplateGeneratorWorker/Properties/launchSettings.json
Normal file
14
PARR.TemplateGeneratorWorker/Properties/launchSettings.json
Normal file
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"profiles": {
|
||||
"PARR.TemplateGeneratorWorker": {
|
||||
"commandName": "Project",
|
||||
"environmentVariables": {
|
||||
"DOTNET_ENVIRONMENT": "Development"
|
||||
},
|
||||
"dotnetRunMessages": true
|
||||
},
|
||||
"Container (Dockerfile)": {
|
||||
"commandName": "Docker"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace PARR.TemplateGeneratorWorker.Services
|
||||
{
|
||||
public interface IValidatorService
|
||||
{
|
||||
Task<bool> IsValidAsync(Guid jobId, Guid unitId);
|
||||
}
|
||||
}
|
||||
62
PARR.TemplateGeneratorWorker/Services/ValidatorService.cs
Normal file
62
PARR.TemplateGeneratorWorker/Services/ValidatorService.cs
Normal file
@@ -0,0 +1,62 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PARR.DAL.Services.Interfaces.Job;
|
||||
using PARR.DAL.Services.Interfaces.Unit;
|
||||
|
||||
namespace PARR.TemplateGeneratorWorker.Services
|
||||
{
|
||||
internal class ValidatorService : IValidatorService
|
||||
{
|
||||
private readonly IServiceProvider serviceProvider;
|
||||
private readonly ILogger<ValidatorService> logger;
|
||||
|
||||
public ValidatorService(
|
||||
IServiceProvider serviceProvider,
|
||||
ILogger<ValidatorService> logger
|
||||
)
|
||||
{
|
||||
this.serviceProvider = serviceProvider;
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
|
||||
public async Task<bool> IsValidAsync(Guid jobId, Guid unitId)
|
||||
{
|
||||
using (var scope = serviceProvider.CreateScope())
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private Service GetServiceInScope<Service>(IServiceScope scope)
|
||||
{
|
||||
var service = scope.ServiceProvider.GetService<Service>();
|
||||
if (service == null)
|
||||
throw new Exception($"Не найден сервис: {nameof(Service)}");
|
||||
|
||||
return service;
|
||||
}
|
||||
}
|
||||
}
|
||||
12
PARR.TemplateGeneratorWorker/Settings/MqSettings.cs
Normal file
12
PARR.TemplateGeneratorWorker/Settings/MqSettings.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using PARR.BLL.Contracts.Interfaces;
|
||||
|
||||
namespace PARR.TemplateGeneratorWorker.Settings
|
||||
{
|
||||
public class MqSettings : IMqSettings//TODO почему-то только публик работает
|
||||
{
|
||||
public string HostName { get; set; } = string.Empty;
|
||||
public string QueueName { get; set; } = string.Empty;
|
||||
public string User { get; set; } = string.Empty;
|
||||
public string Password { get; set; } = string.Empty;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using PARR.BLL;
|
||||
using PARR.DAL;
|
||||
using PARR.TemplateGeneratorWorker.Services;
|
||||
using PARR.TemplateGeneratorWorker.Settings;
|
||||
|
||||
namespace PARR.TemplateGeneratorWorker
|
||||
{
|
||||
public static class TemplateGeneratorWorkerInstaller
|
||||
{
|
||||
public static void InstallTemplateGeneratorWorkerSerivces(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
services.InstallBllServices(configuration);
|
||||
services.InstallDalServices(configuration);
|
||||
|
||||
var mqSettings = new MqSettings();
|
||||
configuration.GetSection(nameof(MqSettings)).Bind(mqSettings);
|
||||
services.AddSingleton(mqSettings);
|
||||
|
||||
//services.AddTransient<ITemplateActivator, TemplateActivator>();
|
||||
//services.AddTransient<IMqTemplateActivator, MqTemplateActivator>();
|
||||
services.AddTransient<IValidatorService, ValidatorService>();
|
||||
}
|
||||
|
||||
public static IConfigurationBuilder AddTemplateGeneratorWorkerConfigurations(this IConfigurationBuilder builder, IServiceCollection services)
|
||||
{
|
||||
builder.AddDalConfigurations(services);
|
||||
|
||||
return builder;
|
||||
}
|
||||
|
||||
public static void AddTemplateGeneratorWorkerSettings(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
services.AddDallSettings(configuration);
|
||||
}
|
||||
}
|
||||
}
|
||||
121
PARR.TemplateGeneratorWorker/Worker.cs
Normal file
121
PARR.TemplateGeneratorWorker/Worker.cs
Normal file
@@ -0,0 +1,121 @@
|
||||
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;
|
||||
using PARR.TemplateGeneratorWorker.Settings;
|
||||
|
||||
namespace PARR.TemplateGeneratorWorker
|
||||
{
|
||||
public class Worker : BackgroundService
|
||||
{
|
||||
private readonly ILogger<Worker> logger;
|
||||
private readonly IMqSettings mqSettings;
|
||||
private readonly IMqService mqService;
|
||||
private readonly ITransformService transformService;
|
||||
private readonly IValidatorService validatorService;
|
||||
private readonly IServiceProvider serviceProvider;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
var isConnected = mqService.InitConsumer(mqSettings, GenerateTemplateAsync);
|
||||
|
||||
if (!isConnected)
|
||||
throw new Exception("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20> RabbitMq");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public override Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
mqService.Dispose();
|
||||
|
||||
return base.StopAsync(cancellationToken);
|
||||
}
|
||||
|
||||
|
||||
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}");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
11
PARR.TemplateGeneratorWorker/appsettings.Development.json
Normal file
11
PARR.TemplateGeneratorWorker/appsettings.Development.json
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.Hosting.Lifetime": "Information"
|
||||
}
|
||||
},
|
||||
"MqSettings": {
|
||||
"HostName": "10.99.253.216"
|
||||
}
|
||||
}
|
||||
35
PARR.TemplateGeneratorWorker/appsettings.json
Normal file
35
PARR.TemplateGeneratorWorker/appsettings.json
Normal file
@@ -0,0 +1,35 @@
|
||||
{
|
||||
"ConnectionStrings": {
|
||||
"DefaultConnection": "Server=10.99.253.184;Database=parr;User Id=app_parr; Password=PosdfkhT&)%sdfligL&%5546;"
|
||||
},
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.Hosting.Lifetime": "Information"
|
||||
}
|
||||
},
|
||||
"Serilog": {
|
||||
"MinimumLevel": {
|
||||
"Default": "Debug",
|
||||
"Override": {
|
||||
"Microsoft": "Warning",
|
||||
"Microsoft.Hosting.Lifetime": "Debug"
|
||||
}
|
||||
},
|
||||
"WriteTo": [
|
||||
{
|
||||
"Name": "File",
|
||||
"Args": {
|
||||
"path": "log/log-.txt",
|
||||
"rollingInterval": "Day"
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"MqSettings": {
|
||||
"HostName": "parr-rabbitmq",
|
||||
"QueueName": "parr-template-generator",
|
||||
"User": "template_generator_reader",
|
||||
"Password": "Jy7vsoygcl7e0:Qy"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user