diff --git a/Hcs.WebApp/BackgroundServices/OperationExecutionService.cs b/Hcs.WebApp/BackgroundServices/OperationExecutionService.cs index 6784890..fb6bee8 100644 --- a/Hcs.WebApp/BackgroundServices/OperationExecutionService.cs +++ b/Hcs.WebApp/BackgroundServices/OperationExecutionService.cs @@ -7,13 +7,13 @@ using Hcs.WebApp.Services; namespace Hcs.WebApp.BackgroundServices { - public class OperationExecutionService(OperationExecutionState state, IServiceScopeFactory scopeFactory) : BackgroundService + public class OperationExecutionService(OperationExecutionState state, ExecutorFactory executorFactory, IServiceScopeFactory scopeFactory) : BackgroundService { private const int SLEEP_TIME = 30000; private readonly OperationExecutionState state = state; + private readonly ExecutorFactory executorFactory = executorFactory; private readonly IServiceScopeFactory scopeFactory = scopeFactory; - private readonly ExecutorFactory executorFactory = new(); private Broker.Logger.ILogger logger; private IMessageCapturer messageCapturer; diff --git a/Hcs.WebApp/BackgroundServices/OperationExecutors/ExecutorBase.cs b/Hcs.WebApp/BackgroundServices/OperationExecutors/ExecutorBase.cs index d13fc5c..6a8c10b 100644 --- a/Hcs.WebApp/BackgroundServices/OperationExecutors/ExecutorBase.cs +++ b/Hcs.WebApp/BackgroundServices/OperationExecutors/ExecutorBase.cs @@ -3,9 +3,10 @@ using Hcs.WebApp.Data.Hcs; namespace Hcs.WebApp.BackgroundServices.OperationExecutors { - public abstract class ExecutorBase(IClient client, Operation operation) : IExecutor + public abstract class ExecutorBase(IClient client, IServiceScopeFactory scopeFactory, Operation operation) : IExecutor { protected readonly IClient client = client; + protected readonly IServiceScopeFactory scopeFactory = scopeFactory; protected readonly Operation operation = operation; public abstract Task ExecuteAsync(CancellationToken cancellationToken); diff --git a/Hcs.WebApp/BackgroundServices/OperationExecutors/ExecutorFactory.cs b/Hcs.WebApp/BackgroundServices/OperationExecutors/ExecutorFactory.cs index 24f87b5..697d5a4 100644 --- a/Hcs.WebApp/BackgroundServices/OperationExecutors/ExecutorFactory.cs +++ b/Hcs.WebApp/BackgroundServices/OperationExecutors/ExecutorFactory.cs @@ -4,14 +4,16 @@ using Hcs.WebApp.Data.Hcs; namespace Hcs.WebApp.BackgroundServices.OperationExecutors { - public class ExecutorFactory + public class ExecutorFactory(IServiceScopeFactory scopeFactory) { + protected readonly IServiceScopeFactory scopeFactory = scopeFactory; + public IExecutor CreateExecutor(IClient client, Operation operation) { switch (operation.Type) { case Operation.OperationType.NsiCommon_ExportNsiItem_15_7_0_1: - return new ExportNsiItemExecutor_15_7_0_1(client, operation); + return new ExportNsiItemExecutor_15_7_0_1(client, scopeFactory, operation); } throw new NotImplementedException(); diff --git a/Hcs.WebApp/BackgroundServices/OperationExecutors/NsiCommon/ExportNsiItemExecutor_15_7_0_1.cs b/Hcs.WebApp/BackgroundServices/OperationExecutors/NsiCommon/ExportNsiItemExecutor_15_7_0_1.cs index 5562565..253ef78 100644 --- a/Hcs.WebApp/BackgroundServices/OperationExecutors/NsiCommon/ExportNsiItemExecutor_15_7_0_1.cs +++ b/Hcs.WebApp/BackgroundServices/OperationExecutors/NsiCommon/ExportNsiItemExecutor_15_7_0_1.cs @@ -1,13 +1,18 @@ using Hcs.Broker; +using Hcs.Service.Async.NsiCommon; using Hcs.WebApp.Data.Hcs; +using Hcs.WebApp.Services; namespace Hcs.WebApp.BackgroundServices.OperationExecutors.NsiCommon { - public class ExportNsiItemExecutor_15_7_0_1(IClient client, Operation operation) : ExecutorBase(client, operation) + public class ExportNsiItemExecutor_15_7_0_1(IClient client, IServiceScopeFactory scopeFactory, Operation operation) : ExecutorBase(client, scopeFactory, operation) { public override async Task ExecuteAsync(CancellationToken cancellationToken) { - return await client.NsiCommon.RequestExportNsiItemAsync(1, Service.Async.NsiCommon.ListGroup.NSI, cancellationToken); + using var scope = scopeFactory.CreateScope(); + var registryService = scope.ServiceProvider.GetRequiredService(); + var registry = await registryService.GetRegistryByOperationId(operation.Id); + return await client.NsiCommon.RequestExportNsiItemAsync(registry.Number, ListGroup.NSI, cancellationToken); } } } diff --git a/Hcs.WebApp/Program.cs b/Hcs.WebApp/Program.cs index ea8bf71..9ffd183 100644 --- a/Hcs.WebApp/Program.cs +++ b/Hcs.WebApp/Program.cs @@ -1,5 +1,6 @@ using Hcs.WebApp.BackgroundServices; using Hcs.WebApp.BackgroundServices.CampaignManagers; +using Hcs.WebApp.BackgroundServices.OperationExecutors; using Hcs.WebApp.Components; using Hcs.WebApp.Components.Shared; using Hcs.WebApp.Data.Hcs; @@ -65,6 +66,7 @@ builder.Services.AddScoped(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); +builder.Services.AddSingleton(); builder.Services.AddHostedService(); builder.Services.AddHostedService(); diff --git a/Hcs.WebApp/Services/RegistryService.cs b/Hcs.WebApp/Services/RegistryService.cs index 69f85fd..d081483 100644 --- a/Hcs.WebApp/Services/RegistryService.cs +++ b/Hcs.WebApp/Services/RegistryService.cs @@ -14,5 +14,11 @@ namespace Hcs.WebApp.Services where registry.IsCommon == isCommon select registry).ToListAsync(); } + + public async Task GetRegistryByOperationId(int operationId) + { + using var context = factory.CreateDbContext(); + return await context.Registries.SingleAsync(x => x.LastSyncOperationId == operationId); + } } }