using Hcs.Broker; using Hcs.Broker.Logger; using Hcs.Broker.MessageCapturer; using Hcs.WebApp.BackgroundServices.OperationExecutors; using Hcs.WebApp.Config; using Hcs.WebApp.Services; namespace Hcs.WebApp.BackgroundServices { public class OperationExecutionService(OperationExecutionState state, IServiceScopeFactory scopeFactory) : BackgroundService { private const int SLEEP_TIME = 30000; private readonly OperationExecutionState state = state; private readonly IServiceScopeFactory scopeFactory = scopeFactory; private readonly ExecutorFactory executorFactory = new(); private Broker.Logger.ILogger logger; private IMessageCapturer messageCapturer; private IClient client; protected override async Task ExecuteAsync(CancellationToken stoppingToken) { await InitializeStateAsync(); InitializeClient(); var scope = scopeFactory.CreateScope(); var headquartersService = scope.ServiceProvider.GetRequiredService(); while (!stoppingToken.IsCancellationRequested) { while (state.TryDequeueOperation(out var operation)) { if (stoppingToken.IsCancellationRequested) return; var messageGuid = string.Empty; try { var executor = executorFactory.CreateExecutor(client, operation); messageGuid = await executor.ExecuteAsync(stoppingToken); } catch { // TODO: Добавить таймауты и макс количество попыток выполнения операции state.EnqueueOperation(operation); } if (!string.IsNullOrEmpty(messageGuid)) { await headquartersService.SetOperationMessageGuidAsync(operation.Id, messageGuid); } } await Task.Delay(SLEEP_TIME, stoppingToken); } } private async Task InitializeStateAsync() { using var scope = scopeFactory.CreateScope(); var headquartersService = scope.ServiceProvider.GetRequiredService(); var operations = await headquartersService.GetInitiatedOperationsAsync(); foreach (var operation in operations) { state.EnqueueOperation(operation); } } private void InitializeClient() { logger = new ActionLogger(); messageCapturer = new FileMessageCapturer("messages", logger); using var scope = scopeFactory.CreateScope(); var configuration = scope.ServiceProvider.GetRequiredService(); var config = configuration.GetSection("BrokerConfig").Get()!; var clientProvider = scope.ServiceProvider.GetRequiredService(); client = clientProvider.CreateClient(config, logger, messageCapturer); } } }