2024-07-25 17:23:08 +10:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
using PARR.BLL.Domain.Mq;
|
|
|
|
|
|
using PARR.BLL.Services.Interfaces;
|
|
|
|
|
|
|
|
|
|
|
|
namespace PARR.TemplateActivator;
|
|
|
|
|
|
|
|
|
|
|
|
internal class MqTemplateActivator : IMqTemplateActivator
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly MqSettings mqSettings;
|
|
|
|
|
|
private readonly IMqService mqService;
|
|
|
|
|
|
private readonly ILogger<MqTemplateActivator> logger;
|
|
|
|
|
|
private readonly ITransformService transformService;
|
|
|
|
|
|
private readonly IValidatorService validatorService;
|
|
|
|
|
|
private readonly IServiceProvider serviceProvider;
|
|
|
|
|
|
|
|
|
|
|
|
public MqTemplateActivator(
|
|
|
|
|
|
MqSettings mqSettings,
|
|
|
|
|
|
IMqService mqService,
|
|
|
|
|
|
ILogger<MqTemplateActivator> 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-25 17:23:08 +10:00
|
|
|
|
{
|
2025-08-20 14:10:22 +10:00
|
|
|
|
var isConnected = await mqService.InitConsumerAsync(mqSettings, UpdateTemplateStatusAsync);
|
2024-07-25 17:23:08 +10:00
|
|
|
|
|
|
|
|
|
|
if (!isConnected)
|
|
|
|
|
|
throw new Exception("Ошибка при подключении к RabbitMq");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private async Task UpdateTemplateStatusAsync(string msg)
|
|
|
|
|
|
{
|
2025-11-26 14:34:03 +10:00
|
|
|
|
logger.LogInformation($"Получили запрос: {msg}");
|
2024-07-25 17:23:08 +10:00
|
|
|
|
|
|
|
|
|
|
var query = transformService.GetModelFromJson<TemplateActivatorMq>(msg);
|
|
|
|
|
|
if (query == null)
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
2025-11-26 14:34:03 +10:00
|
|
|
|
if (!await validatorService.IsValidObjectIdAsync(query.Id, query.EntityType))
|
2024-07-25 17:23:08 +10:00
|
|
|
|
{
|
2025-11-26 14:34:03 +10:00
|
|
|
|
logger.LogError($"Не найдет объект {nameof(query.Id)}: {query.Id}, типа {nameof(query.EntityType)}: {query.EntityType}.");
|
2024-07-25 17:23:08 +10:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
using (var scope = serviceProvider.CreateScope())
|
|
|
|
|
|
{
|
|
|
|
|
|
var service = scope.ServiceProvider.GetService<ITemplateActivator>();
|
|
|
|
|
|
if (service == null)
|
2025-11-26 14:34:03 +10:00
|
|
|
|
throw new Exception($"Не найден сервис: {nameof(service.GetType)}");
|
2024-07-25 17:23:08 +10:00
|
|
|
|
|
2025-11-26 14:34:03 +10:00
|
|
|
|
await service.ChangeStateAsync(query);
|
2024-07-25 17:23:08 +10:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-20 14:10:22 +10:00
|
|
|
|
public async Task StopAsync()
|
2024-07-25 17:23:08 +10:00
|
|
|
|
{
|
2025-08-26 12:04:26 +10:00
|
|
|
|
await mqService.DisposeAsync();
|
2024-07-25 17:23:08 +10:00
|
|
|
|
}
|
|
|
|
|
|
}
|