Implement result getter
This commit is contained in:
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user