2024-07-05 15:17:48 +10:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
2026-04-14 16:27:27 +10:00
|
|
|
|
using PARR.Core.Common.Interfaces;
|
|
|
|
|
|
using PARR.Core.Common.Interfaces.RabbitServices;
|
2026-04-14 12:01:49 +10:00
|
|
|
|
using PARR.Domain.Common.Rabbit.Messages;
|
2024-07-05 15:17:48 +10:00
|
|
|
|
using PARR.TemplateDistributor.Services;
|
|
|
|
|
|
using PARR.TemplateDistributor.Settings;
|
|
|
|
|
|
|
|
|
|
|
|
namespace PARR.TemplateDistributor
|
2024-07-02 11:43:59 +10:00
|
|
|
|
{
|
|
|
|
|
|
internal class MqTemplateDistributor : IMqTemplateDistributor
|
|
|
|
|
|
{
|
2024-07-05 15:17:48 +10:00
|
|
|
|
private readonly MqSettings mqSettings;
|
2026-04-14 12:01:49 +10:00
|
|
|
|
private readonly IRabbitService mqService;
|
2024-07-05 15:17:48 +10:00
|
|
|
|
private readonly ILogger<MqTemplateDistributor> logger;
|
|
|
|
|
|
private readonly ITransformService transformService;
|
|
|
|
|
|
private readonly IValidatorService validatorService;
|
|
|
|
|
|
private readonly IServiceProvider serviceProvider;
|
|
|
|
|
|
|
|
|
|
|
|
public MqTemplateDistributor(MqSettings mqSettings,
|
2026-04-14 12:01:49 +10:00
|
|
|
|
IRabbitService mqService,
|
2024-07-05 15:17:48 +10:00
|
|
|
|
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;
|
|
|
|
|
|
}
|
2025-08-20 14:10:22 +10:00
|
|
|
|
public async Task StartAsync()
|
2024-07-05 15:17:48 +10:00
|
|
|
|
{
|
|
|
|
|
|
|
2025-08-20 14:10:22 +10:00
|
|
|
|
var isConnected = await mqService.InitConsumerAsync(mqSettings, UpdateScheduleAsync);
|
2024-07-05 15:17:48 +10:00
|
|
|
|
|
|
|
|
|
|
if (!isConnected)
|
|
|
|
|
|
throw new Exception("Ошибка при подключении к RabbitMq");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-20 14:10:22 +10:00
|
|
|
|
public async Task StopAsync()
|
2024-07-05 15:17:48 +10:00
|
|
|
|
{
|
2026-01-21 16:28:17 +10:00
|
|
|
|
await mqService.DisposeAsync();
|
2024-07-05 15:17:48 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private async Task UpdateScheduleAsync(string msg)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogInformation($"Получили запрос: {msg}");
|
|
|
|
|
|
|
|
|
|
|
|
var query = transformService.GetModelFromJson<TemplateDistributorMq>(msg);
|
|
|
|
|
|
if (query == null)
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
2026-01-21 16:28:17 +10:00
|
|
|
|
if (!await validatorService.IsValidJobGroupAsync(query.JobGroupId))
|
2024-07-05 15:17:48 +10:00
|
|
|
|
{
|
2026-01-21 16:28:17 +10:00
|
|
|
|
logger.LogError("Не корректные параметры группы работ {jobGroupId}. Не буду ничего делать.", query.JobGroupId);
|
2024-07-05 15:17:48 +10:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
using (var scope = serviceProvider.CreateScope())
|
|
|
|
|
|
{
|
|
|
|
|
|
var service = scope.ServiceProvider.GetService<ITemplateDistributor>();
|
|
|
|
|
|
if (service == null)
|
2026-01-21 16:28:17 +10:00
|
|
|
|
throw new Exception($"Не найден сервис: {nameof(ITemplateDistributor)}");
|
2024-07-05 15:17:48 +10:00
|
|
|
|
|
2026-01-22 10:35:27 +10:00
|
|
|
|
await service.DistributeAsync(query);
|
2024-07-05 15:17:48 +10:00
|
|
|
|
}
|
2024-07-02 15:04:23 +10:00
|
|
|
|
}
|
2024-07-02 11:43:59 +10:00
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|