Implement result getter

This commit is contained in:
2025-11-01 19:15:07 +09:00
parent 6fc2db95ec
commit 821eeb41ae
16 changed files with 243 additions and 18 deletions

View File

@ -14,7 +14,7 @@ namespace Hcs.WebApp.BackgroundServices.OperationExecutors
return new ExportNsiItemExecutor_15_7_0_1(client, scope, operation);
}
throw new NotImplementedException();
throw new NotImplementedException($"Не удалось создать выполнителя операции типа {operation.Type}");
}
}
}

View File

@ -0,0 +1,7 @@
namespace Hcs.WebApp.BackgroundServices.ResultGetters
{
public interface IResultGetter
{
Task<ResultGetterResponse> GetAsync();
}
}

View File

@ -0,0 +1,29 @@
using Hcs.Broker;
using Hcs.WebApp.Data.Hcs;
using Hcs.WebApp.Services;
namespace Hcs.WebApp.BackgroundServices.ResultGetters.NsiCommon
{
public class ExportNsiItemGetter_15_7_0_1(IClient client, IServiceScope scope, Operation operation) : ResultGetterBase(client, scope, operation)
{
public override async Task<ResultGetterResponse> GetAsync()
{
var result = await client.NsiCommon.GetExportNsiItemResultAsync(operation.MessageGuid!);
if (!result.Ready)
{
return ResultGetterResponse.NotReady;
}
if (result.Success)
{
var registryService = scope.ServiceProvider.GetRequiredService<RegistryService>();
var registry = await registryService.GetRegistryByOperationIdAsync(operation.Id);
// TODO
return ResultGetterResponse.Successful;
}
return ResultGetterResponse.Failed;
}
}
}

View File

@ -0,0 +1,14 @@
using Hcs.Broker;
using Hcs.WebApp.Data.Hcs;
namespace Hcs.WebApp.BackgroundServices.ResultGetters
{
public abstract class ResultGetterBase(IClient client, IServiceScope scope, Operation operation) : IResultGetter
{
protected readonly IClient client = client;
protected readonly IServiceScope scope = scope;
protected readonly Operation operation = operation;
public abstract Task<ResultGetterResponse> GetAsync();
}
}

View File

@ -0,0 +1,21 @@
using Hcs.Broker;
using Hcs.WebApp.BackgroundServices.ResultGetters.NsiCommon;
using Hcs.WebApp.Data.Hcs;
namespace Hcs.WebApp.BackgroundServices.ResultGetters
{
public class ResultGetterFactory
{
public IResultGetter CreateResultGetter(IServiceScope scope, IClient client, Operation operation)
{
switch (operation.Type)
{
case Operation.OperationType.NsiCommon_ExportNsiItem_15_7_0_1:
return new ExportNsiItemGetter_15_7_0_1(client, scope, operation);
}
throw new NotImplementedException($"Не удалось создать получателя результата операции типа {operation.Type}");
}
}
}

View File

@ -0,0 +1,9 @@
namespace Hcs.WebApp.BackgroundServices.ResultGetters
{
public enum ResultGetterResponse
{
NotReady,
Successful,
Failed
}
}

View File

@ -1,17 +1,32 @@
using Hcs.Broker;
using Hcs.Broker.Logger;
using Hcs.Broker.MessageCapturer;
using Hcs.WebApp.BackgroundServices.ResultGetters;
using Hcs.WebApp.Config;
using Hcs.WebApp.Data.Hcs;
using Hcs.WebApp.Services;
namespace Hcs.WebApp.BackgroundServices
{
public class ResultWaitService(ResultWaitState state, IServiceScopeFactory scopeFactory) : BackgroundService
public class ResultWaitService(
ResultWaitState state,
ResultGetterFactory resultGetterFactory,
IServiceScopeFactory scopeFactory) : BackgroundService
{
private class WaitState
{
public int attempt;
public int timer;
public bool done;
}
private const int ITERATION_TIME = 10000;
private const int SLEEP_TIME = 30000;
private readonly ResultWaitState state = state;
private readonly ResultGetterFactory resultGetterFactory = resultGetterFactory;
private readonly IServiceScopeFactory scopeFactory = scopeFactory;
private readonly List<(Operation operation, WaitState state)> entries = [];
private Broker.Logger.ILogger logger;
private IMessageCapturer messageCapturer;
@ -28,9 +43,60 @@ namespace Hcs.WebApp.BackgroundServices
while (state.TryDequeueOperation(out var operation))
{
if (stoppingToken.IsCancellationRequested) return;
entries.Add(new (operation, new WaitState()));
}
await Task.Delay(SLEEP_TIME, stoppingToken);
if (entries.Count > 0)
{
await Task.Delay(ITERATION_TIME, stoppingToken);
var scope = scopeFactory.CreateScope();
foreach (var entry in entries)
{
entry.state.timer += ITERATION_TIME;
var send = entry.state.attempt switch
{
0 => entry.state.timer >= 10000,
1 => entry.state.timer >= 60000,
2 => entry.state.timer >= 300000,
3 => entry.state.timer >= 900000,
_ => entry.state.timer >= 1800000,
};
if (send)
{
try
{
var resultGetter = resultGetterFactory.CreateResultGetter(scope, client, entry.operation);
var response = await resultGetter.GetAsync();
switch (response)
{
case ResultGetterResponse.Successful:
case ResultGetterResponse.Failed:
entry.state.done = true;
break;
}
}
catch (Exception e)
{
var headquartersService = scope.ServiceProvider.GetRequiredService<HeadquartersService>();
await headquartersService.SetOperationEndedWithFailAsync(entry.operation.Id, e.Message);
entry.state.done = true;
}
entry.state.attempt++;
entry.state.timer = 0;
}
}
entries.RemoveAll(x => x.state.done);
}
else
{
await Task.Delay(SLEEP_TIME, stoppingToken);
}
}
}

View File

@ -1,7 +0,0 @@
namespace Hcs.WebApp.BackgroundServices.ResultWaiters
{
public interface IResultWaiter
{
// TODO
}
}

View File

@ -1,6 +1,7 @@
using Hcs.WebApp.BackgroundServices;
using Hcs.WebApp.BackgroundServices.CampaignManagers;
using Hcs.WebApp.BackgroundServices.OperationExecutors;
using Hcs.WebApp.BackgroundServices.ResultGetters;
using Hcs.WebApp.Components;
using Hcs.WebApp.Components.Shared;
using Hcs.WebApp.Data.Hcs;
@ -68,6 +69,7 @@ builder.Services.AddSingleton<OperationExecutionState>();
builder.Services.AddSingleton<ResultWaitState>();
builder.Services.AddSingleton<ManagerFactory>();
builder.Services.AddSingleton<ExecutorFactory>();
builder.Services.AddSingleton<ResultGetterFactory>();
builder.Services.AddHostedService<CampaignManagementService>();
builder.Services.AddHostedService<OperationExecutionService>();