Load initiated operations
This commit is contained in:
@ -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 OperationExecutionState state = state;
|
||||||
|
private readonly IServiceScopeFactory scopeFactory = scopeFactory;
|
||||||
|
|
||||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||||
{
|
{
|
||||||
|
await InitializeState();
|
||||||
|
|
||||||
|
var scope = scopeFactory.CreateScope();
|
||||||
|
var operationService = scope.ServiceProvider.GetRequiredService<OperationService>();
|
||||||
|
|
||||||
while (!stoppingToken.IsCancellationRequested)
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -26,5 +26,25 @@ namespace Hcs.WebApp.Services
|
|||||||
await context.SaveChangesAsync();
|
await context.SaveChangesAsync();
|
||||||
return operation;
|
return operation;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<IEnumerable<Operation>> GetInitiatedOperations()
|
||||||
|
{
|
||||||
|
using var context = factory.CreateDbContext();
|
||||||
|
return await (from operation in context.Operations
|
||||||
|
where !operation.EndedAt.HasValue && string.IsNullOrEmpty(operation.MessageGuid)
|
||||||
|
select operation).ToListAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task SetOperationMessageGuid(int operationId, string messageGuid)
|
||||||
|
{
|
||||||
|
using var context = factory.CreateDbContext();
|
||||||
|
var operation = await context.Operations.FirstOrDefaultAsync(x => x.Id == operationId);
|
||||||
|
if (operation != null)
|
||||||
|
{
|
||||||
|
operation.MessageGuid = messageGuid;
|
||||||
|
|
||||||
|
await context.SaveChangesAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user