diff --git a/Hcs.WebApp/BackgroundServices/DataParsers/DataParserBase.cs b/Hcs.WebApp/BackgroundServices/DataParsers/DataParserBase.cs index 92e5fb5..21533f8 100644 --- a/Hcs.WebApp/BackgroundServices/DataParsers/DataParserBase.cs +++ b/Hcs.WebApp/BackgroundServices/DataParsers/DataParserBase.cs @@ -3,12 +3,11 @@ using Hcs.WebApp.Data.Hcs; namespace Hcs.WebApp.BackgroundServices.DataParsers { - public abstract class DataParserBase(IClient client, IServiceScope scope, Operation operation) : IDataParser + public abstract class DataParserBase(IServiceScope scope, Operation operation) : IDataParser { - protected readonly IClient client = client; protected readonly IServiceScope scope = scope; protected readonly Operation operation = operation; - public abstract Task ParseAsync(); + public abstract Task ParseAsync(); } } diff --git a/Hcs.WebApp/BackgroundServices/DataParsers/DataParserFactory.cs b/Hcs.WebApp/BackgroundServices/DataParsers/DataParserFactory.cs index 39234ee..a831053 100644 --- a/Hcs.WebApp/BackgroundServices/DataParsers/DataParserFactory.cs +++ b/Hcs.WebApp/BackgroundServices/DataParsers/DataParserFactory.cs @@ -5,11 +5,11 @@ namespace Hcs.WebApp.BackgroundServices.DataParsers { public class DataParserFactory { - public IDataParser CreateResultGetter(IServiceScope scope, IClient client, Operation operation) + public IDataParser CreateDataParser(IServiceScope scope, Operation operation) { return operation.Type switch { - Operation.OperationType.ParseHousesData_15_7_0_1 => new HousesDataParser_15_7_0_1(client, scope, operation), + Operation.OperationType.ParseHousesData_15_7_0_1 => new HousesDataParser_15_7_0_1(scope, operation), Operation.OperationType.NsiCommon_ExportNsiItem_15_7_0_1 or Operation.OperationType.Nsi_ExportNsiItem_15_7_0_1 => throw new ArgumentException($"Нельзя использовать операцию с типом {operation.Type} для парсинга") diff --git a/Hcs.WebApp/BackgroundServices/DataParsers/HousesDataParser_15_7_0_1.cs b/Hcs.WebApp/BackgroundServices/DataParsers/HousesDataParser_15_7_0_1.cs index bdaea2f..e183220 100644 --- a/Hcs.WebApp/BackgroundServices/DataParsers/HousesDataParser_15_7_0_1.cs +++ b/Hcs.WebApp/BackgroundServices/DataParsers/HousesDataParser_15_7_0_1.cs @@ -3,9 +3,9 @@ using Hcs.WebApp.Data.Hcs; namespace Hcs.WebApp.BackgroundServices.DataParsers { - public class HousesDataParser_15_7_0_1(IClient client, IServiceScope scope, Operation operation) : DataParserBase(client, scope, operation) + public class HousesDataParser_15_7_0_1(IServiceScope scope, Operation operation) : DataParserBase(scope, operation) { - public override Task ParseAsync() + public override Task ParseAsync() { throw new NotImplementedException(); } diff --git a/Hcs.WebApp/BackgroundServices/DataParsers/IDataParser.cs b/Hcs.WebApp/BackgroundServices/DataParsers/IDataParser.cs index 4a4b13a..da3de14 100644 --- a/Hcs.WebApp/BackgroundServices/DataParsers/IDataParser.cs +++ b/Hcs.WebApp/BackgroundServices/DataParsers/IDataParser.cs @@ -2,6 +2,6 @@ { public interface IDataParser { - Task ParseAsync(); + Task ParseAsync(); } } diff --git a/Hcs.WebApp/BackgroundServices/DataParsingService.cs b/Hcs.WebApp/BackgroundServices/DataParsingService.cs index c7ce20c..9449a44 100644 --- a/Hcs.WebApp/BackgroundServices/DataParsingService.cs +++ b/Hcs.WebApp/BackgroundServices/DataParsingService.cs @@ -1,5 +1,6 @@ using Hcs.WebApp.BackgroundServices.DataParsers; using Hcs.WebApp.Services; +using Hcs.WebApp.Utils; namespace Hcs.WebApp.BackgroundServices { @@ -8,6 +9,8 @@ namespace Hcs.WebApp.BackgroundServices DataParserFactory dataParserFactory, IServiceScopeFactory scopeFactory) : BackgroundService { + private const int SLEEP_TIME = 30000; + private readonly DataParsingState state = state; private readonly DataParserFactory dataParserFactory = dataParserFactory; private readonly IServiceScopeFactory scopeFactory = scopeFactory; @@ -15,6 +18,41 @@ namespace Hcs.WebApp.BackgroundServices protected override async Task ExecuteAsync(CancellationToken stoppingToken) { await InitializeStateAsync(); + + while (!stoppingToken.IsCancellationRequested) + { + while (state.TryDequeueOperation(out var operation)) + { + state.SetProcessingOperation(operation); + + if (stoppingToken.IsCancellationRequested) return; + + var scope = scopeFactory.CreateScope(); + var headquartersService = scope.ServiceProvider.GetRequiredService(); + try + { + var startedAt = DateTime.UtcNow; + await headquartersService.SetOperationStartedAsync(operation.Id, startedAt); + + state.InvokeOnOperationStarted(operation.Id, operation.CampaignId, startedAt); + + var dataParser = dataParserFactory.CreateDataParser(scope, operation); + await dataParser.ParseAsync(); + } + catch (Exception e) + { + var failedAt = DateTime.UtcNow; + var failureReason = e.CombineMessages(); + await headquartersService.SetOperationEndedWithFailAsync(operation.Id, failedAt, failureReason); + + state.InvokeOnOperationEnded(operation.Id, operation.CampaignId, failedAt, failureReason); + } + + state.UnsetProcessingOperation(operation); + } + + await Task.Delay(SLEEP_TIME, stoppingToken); + } } private async Task InitializeStateAsync()