feat(templateDistributor): логика распределения шаблонов по периоду
This commit is contained in:
@@ -1,12 +1,74 @@
|
||||
namespace PARR.TemplateDistributor
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using PARR.BLL.Domain.Mq;
|
||||
using PARR.BLL.Services.Interfaces;
|
||||
using PARR.TemplateDistributor.Services;
|
||||
using PARR.TemplateDistributor.Settings;
|
||||
|
||||
namespace PARR.TemplateDistributor
|
||||
{
|
||||
internal class MqTemplateDistributor : IMqTemplateDistributor
|
||||
{
|
||||
public void Start() {
|
||||
private readonly MqSettings mqSettings;
|
||||
private readonly IMqService mqService;
|
||||
private readonly ILogger<MqTemplateDistributor> logger;
|
||||
private readonly ITransformService transformService;
|
||||
private readonly IValidatorService validatorService;
|
||||
private readonly IServiceProvider serviceProvider;
|
||||
|
||||
// принимать TemplateDistributorMq
|
||||
public MqTemplateDistributor(MqSettings mqSettings,
|
||||
IMqService mqService,
|
||||
ILogger<MqTemplateDistributor> logger,
|
||||
ITransformService transformService,
|
||||
IValidatorService validatorService,
|
||||
IServiceProvider serviceProvider
|
||||
)
|
||||
{
|
||||
this.mqSettings = mqSettings;
|
||||
this.mqService = mqService;
|
||||
this.logger = logger;
|
||||
this.transformService = transformService;
|
||||
this.validatorService = validatorService;
|
||||
this.serviceProvider = serviceProvider;
|
||||
}
|
||||
public void Start()
|
||||
{
|
||||
|
||||
var isConnected = mqService.InitConsumer(mqSettings, UpdateScheduleAsync);
|
||||
|
||||
if (!isConnected)
|
||||
throw new Exception("Ошибка при подключении к RabbitMq");
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
mqService.Dispose();
|
||||
}
|
||||
|
||||
private async Task UpdateScheduleAsync(string msg)
|
||||
{
|
||||
logger.LogInformation($"Получили запрос: {msg}");
|
||||
|
||||
var query = transformService.GetModelFromJson<TemplateDistributorMq>(msg);
|
||||
if (query == null)
|
||||
return;
|
||||
|
||||
if (!await validatorService.IsValidApplicationAndWorksAsync(query.ApplicationInWorkId))
|
||||
{
|
||||
logger.LogError($"Некорректные параметры регалментной работы {nameof(query.ApplicationInWorkId)}: {query.ApplicationInWorkId}");
|
||||
return;
|
||||
}
|
||||
|
||||
using (var scope = serviceProvider.CreateScope())
|
||||
{
|
||||
var service = scope.ServiceProvider.GetService<ITemplateDistributor>();
|
||||
if (service == null)
|
||||
throw new Exception($"Не найден сервис: {nameof(service.GetType)}");
|
||||
|
||||
await service.UpdateScheduleAsync(query.ApplicationInWorkId);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public void Stop() { }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user