Files
parr_api/PARR.AIHITMainSyncer/AihitMainSyncer.cs

56 lines
1.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using PARR.AIHITMainSyncer.Services;
using PARR.AIHITMainSyncer.Settings;
using PARR.BLL.Services.Interfaces;
namespace PARR.AIHITMainSyncer
{
internal class AihitMainSyncer : IAihitMainSyncer
{
private readonly ILogger<AihitMainSyncer> logger;
private readonly IMqService mqService;
private readonly MqSettings mqSettings;
private readonly IServiceProvider serviceProvider;
public AihitMainSyncer(
ILogger<AihitMainSyncer> logger,
IMqService mqService,
MqSettings mqSettings,
IServiceProvider serviceProvider
)
{
this.logger = logger;
this.mqService = mqService;
this.mqSettings = mqSettings;
this.serviceProvider = serviceProvider;
}
public async Task StartAsync()
{
logger.LogInformation("Запуск сервиса синхронизации данных из АИХИТ и ПАРР.");
var isConnected = await mqService.InitConsumerAsync(mqSettings, async (string msg) =>
{
using (var scope = serviceProvider.CreateScope())
{
var syncerService = scope.ServiceProvider.GetService<ISyncerService>();
if (syncerService == null)
throw new Exception("Не смог получить серивс ISyncerService, scope.ServiceProvider.GetService<ISyncerService>()");
await syncerService.SyncAsync(msg);
}
});
if (!isConnected)
throw new Exception("Ошибка при подключении к RabbitMq");
}
public async Task StopAsync()
{
await mqService.DisposeAsync();
}
}
}