Add operation executor
This commit is contained in:
@ -0,0 +1,13 @@
|
||||
using Hcs.Broker;
|
||||
using Hcs.WebApp.Data.Hcs;
|
||||
|
||||
namespace Hcs.WebApp.BackgroundServices.Executors._15_7_0_1.NsiCommon
|
||||
{
|
||||
public class ExportAllRegistryElementsExecutor(IClient client, Operation operation) : ExecutorBase(client, operation)
|
||||
{
|
||||
public override async Task<string> ExecuteAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
return await client.NsiCommon.RequestExportNsiItemAsync(1, Service.Async.NsiCommon.ListGroup.NSI, cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
13
Hcs.WebApp/BackgroundServices/Executors/ExecutorBase.cs
Normal file
13
Hcs.WebApp/BackgroundServices/Executors/ExecutorBase.cs
Normal file
@ -0,0 +1,13 @@
|
||||
using Hcs.Broker;
|
||||
using Hcs.WebApp.Data.Hcs;
|
||||
|
||||
namespace Hcs.WebApp.BackgroundServices.Executors
|
||||
{
|
||||
public abstract class ExecutorBase(IClient client, Operation operation) : IExecutor
|
||||
{
|
||||
protected readonly IClient client = client;
|
||||
protected readonly Operation operation = operation;
|
||||
|
||||
public abstract Task<string> ExecuteAsync(CancellationToken cancellationToken);
|
||||
}
|
||||
}
|
||||
20
Hcs.WebApp/BackgroundServices/Executors/ExecutorFactory.cs
Normal file
20
Hcs.WebApp/BackgroundServices/Executors/ExecutorFactory.cs
Normal file
@ -0,0 +1,20 @@
|
||||
using Hcs.Broker;
|
||||
using Hcs.WebApp.BackgroundServices.Executors._15_7_0_1.NsiCommon;
|
||||
using Hcs.WebApp.Data.Hcs;
|
||||
|
||||
namespace Hcs.WebApp.BackgroundServices.Executors
|
||||
{
|
||||
public class ExecutorFactory
|
||||
{
|
||||
public IExecutor CreateExecutor(IClient client, Operation operation)
|
||||
{
|
||||
switch (operation.Type)
|
||||
{
|
||||
case Operation.OperationType.NsiCommon_15_7_0_1_ExportAllRegistryElements:
|
||||
return new ExportAllRegistryElementsExecutor(client, operation);
|
||||
}
|
||||
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
7
Hcs.WebApp/BackgroundServices/Executors/IExecutor.cs
Normal file
7
Hcs.WebApp/BackgroundServices/Executors/IExecutor.cs
Normal file
@ -0,0 +1,7 @@
|
||||
namespace Hcs.WebApp.BackgroundServices.Executors
|
||||
{
|
||||
public interface IExecutor
|
||||
{
|
||||
Task<string> ExecuteAsync(CancellationToken cancellationToken);
|
||||
}
|
||||
}
|
||||
@ -1,6 +1,7 @@
|
||||
using Hcs.Broker;
|
||||
using Hcs.Broker.Logger;
|
||||
using Hcs.Broker.MessageCapturer;
|
||||
using Hcs.WebApp.BackgroundServices.Executors;
|
||||
using Hcs.WebApp.Config;
|
||||
using Hcs.WebApp.Services;
|
||||
|
||||
@ -12,6 +13,7 @@ namespace Hcs.WebApp.BackgroundServices
|
||||
|
||||
private readonly OperationExecutionState state = state;
|
||||
private readonly IServiceScopeFactory scopeFactory = scopeFactory;
|
||||
private readonly ExecutorFactory executorFactory = new();
|
||||
|
||||
private Broker.Logger.ILogger logger;
|
||||
private IMessageCapturer messageCapturer;
|
||||
@ -19,7 +21,7 @@ namespace Hcs.WebApp.BackgroundServices
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
await InitializeState();
|
||||
await InitializeStateAsync();
|
||||
|
||||
InitializeClient();
|
||||
|
||||
@ -30,19 +32,33 @@ namespace Hcs.WebApp.BackgroundServices
|
||||
{
|
||||
while (state.TryDequeueOperation(out var operation))
|
||||
{
|
||||
await operationService.SetOperationMessageGuid(operation.Id, Guid.NewGuid().ToString());
|
||||
var messageGuid = string.Empty;
|
||||
try
|
||||
{
|
||||
var executor = executorFactory.CreateExecutor(client, operation);
|
||||
messageGuid = await executor.ExecuteAsync(stoppingToken);
|
||||
}
|
||||
catch
|
||||
{
|
||||
state.EnqueueOperation(operation);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(messageGuid))
|
||||
{
|
||||
await operationService.SetOperationMessageGuidAsync(operation.Id, messageGuid);
|
||||
}
|
||||
}
|
||||
|
||||
await Task.Delay(SLEEP_TIME, stoppingToken);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task InitializeState()
|
||||
private async Task InitializeStateAsync()
|
||||
{
|
||||
using var scope = scopeFactory.CreateScope();
|
||||
var operationService = scope.ServiceProvider.GetRequiredService<OperationService>();
|
||||
|
||||
var operations = await operationService.GetInitiatedOperations();
|
||||
var operations = await operationService.GetInitiatedOperationsAsync();
|
||||
foreach (var operation in operations)
|
||||
{
|
||||
state.EnqueueOperation(operation);
|
||||
|
||||
Reference in New Issue
Block a user