Get hcs request result separately

This commit is contained in:
2025-10-31 16:44:54 +09:00
parent a37791d615
commit 6fc2db95ec
6 changed files with 113 additions and 10 deletions

View File

@ -60,7 +60,7 @@ namespace Hcs.WebApp.BackgroundServices
using var scope = scopeFactory.CreateScope();
var headquartersService = scope.ServiceProvider.GetRequiredService<HeadquartersService>();
var operations = await headquartersService.GetInitiatedOperationsAsync();
var operations = await headquartersService.GetNotExecutedOperationsAsync();
foreach (var operation in operations)
{
state.EnqueueOperation(operation);
@ -70,7 +70,7 @@ namespace Hcs.WebApp.BackgroundServices
private void InitializeClient()
{
logger = new ActionLogger();
messageCapturer = new FileMessageCapturer("messages", logger);
messageCapturer = new FileMessageCapturer("outgoing", logger);
using var scope = scopeFactory.CreateScope();
var configuration = scope.ServiceProvider.GetRequiredService<IConfiguration>();

View File

@ -1,7 +1,61 @@
namespace Hcs.WebApp.BackgroundServices
using Hcs.Broker;
using Hcs.Broker.Logger;
using Hcs.Broker.MessageCapturer;
using Hcs.WebApp.Config;
using Hcs.WebApp.Services;
namespace Hcs.WebApp.BackgroundServices
{
public class ResultWaitService
public class ResultWaitService(ResultWaitState state, IServiceScopeFactory scopeFactory) : BackgroundService
{
// TODO
private const int SLEEP_TIME = 30000;
private readonly ResultWaitState state = state;
private readonly IServiceScopeFactory scopeFactory = scopeFactory;
private Broker.Logger.ILogger logger;
private IMessageCapturer messageCapturer;
private IClient client;
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
await InitializeStateAsync();
InitializeClient();
while (!stoppingToken.IsCancellationRequested)
{
while (state.TryDequeueOperation(out var operation))
{
if (stoppingToken.IsCancellationRequested) return;
}
await Task.Delay(SLEEP_TIME, stoppingToken);
}
}
private async Task InitializeStateAsync()
{
using var scope = scopeFactory.CreateScope();
var headquartersService = scope.ServiceProvider.GetRequiredService<HeadquartersService>();
var operations = await headquartersService.GetResultWaitingOperationsAsync();
foreach (var operation in operations)
{
state.EnqueueOperation(operation);
}
}
private void InitializeClient()
{
logger = new ActionLogger();
messageCapturer = new FileMessageCapturer("incoming", logger);
using var scope = scopeFactory.CreateScope();
var configuration = scope.ServiceProvider.GetRequiredService<IConfiguration>();
var config = configuration.GetSection("BrokerConfig").Get<BrokerConfig>()!;
var clientProvider = scope.ServiceProvider.GetRequiredService<IClientProvider>();
client = clientProvider.CreateClient(config, logger, messageCapturer);
}
}
}

View File

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

View File

@ -19,11 +19,19 @@ namespace Hcs.WebApp.Services
select campaign).ToListAsync();
}
public async Task<IEnumerable<Operation>> GetInitiatedOperationsAsync()
public async Task<IEnumerable<Operation>> GetNotExecutedOperationsAsync()
{
using var context = GetNewContext();
return await (from operation in context.Operations
where string.IsNullOrEmpty(operation.MessageGuid)
where string.IsNullOrEmpty(operation.MessageGuid) && !operation.EndedAt.HasValue
select operation).ToListAsync();
}
public async Task<IEnumerable<Operation>> GetResultWaitingOperationsAsync()
{
using var context = GetNewContext();
return await (from operation in context.Operations
where !string.IsNullOrEmpty(operation.MessageGuid) && !operation.EndedAt.HasValue
select operation).ToListAsync();
}