Load initiated operations

This commit is contained in:
2025-10-24 17:23:54 +09:00
parent ce78e3a2ad
commit e8fab0c3dd
2 changed files with 49 additions and 4 deletions

View File

@ -1,16 +1,41 @@
namespace Hcs.WebApp.BackgroundServices
using Hcs.WebApp.Services;
namespace Hcs.WebApp.BackgroundServices
{
public class OperationExecutionService(OperationExecutionState state) : BackgroundService
public class OperationExecutionService(OperationExecutionState state, IServiceScopeFactory scopeFactory) : BackgroundService
{
private const int SLEEP_TIME = 30000;
private readonly OperationExecutionState state = state;
private readonly IServiceScopeFactory scopeFactory = scopeFactory;
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
await InitializeState();
var scope = scopeFactory.CreateScope();
var operationService = scope.ServiceProvider.GetRequiredService<OperationService>();
while (!stoppingToken.IsCancellationRequested)
{
await Task.Delay(10000, stoppingToken);
while (state.TryDequeueOperation(out var operation))
{
await operationService.SetOperationMessageGuid(operation.Id, Guid.NewGuid().ToString());
}
// TODO
await Task.Delay(SLEEP_TIME, stoppingToken);
}
}
private async Task InitializeState()
{
using var scope = scopeFactory.CreateScope();
var operationService = scope.ServiceProvider.GetRequiredService<OperationService>();
var operations = await operationService.GetInitiatedOperations();
foreach (var operation in operations)
{
state.EnqueueOperation(operation);
}
}
}