Implement data parsing service
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
using Hcs.WebApp.BackgroundServices.DataParsers;
|
||||
using Hcs.WebApp.Services;
|
||||
using Hcs.WebApp.Utils;
|
||||
|
||||
namespace Hcs.WebApp.BackgroundServices
|
||||
{
|
||||
@ -8,6 +9,8 @@ namespace Hcs.WebApp.BackgroundServices
|
||||
DataParserFactory dataParserFactory,
|
||||
IServiceScopeFactory scopeFactory) : BackgroundService
|
||||
{
|
||||
private const int SLEEP_TIME = 30000;
|
||||
|
||||
private readonly DataParsingState state = state;
|
||||
private readonly DataParserFactory dataParserFactory = dataParserFactory;
|
||||
private readonly IServiceScopeFactory scopeFactory = scopeFactory;
|
||||
@ -15,6 +18,41 @@ namespace Hcs.WebApp.BackgroundServices
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
await InitializeStateAsync();
|
||||
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
while (state.TryDequeueOperation(out var operation))
|
||||
{
|
||||
state.SetProcessingOperation(operation);
|
||||
|
||||
if (stoppingToken.IsCancellationRequested) return;
|
||||
|
||||
var scope = scopeFactory.CreateScope();
|
||||
var headquartersService = scope.ServiceProvider.GetRequiredService<HeadquartersService>();
|
||||
try
|
||||
{
|
||||
var startedAt = DateTime.UtcNow;
|
||||
await headquartersService.SetOperationStartedAsync(operation.Id, startedAt);
|
||||
|
||||
state.InvokeOnOperationStarted(operation.Id, operation.CampaignId, startedAt);
|
||||
|
||||
var dataParser = dataParserFactory.CreateDataParser(scope, operation);
|
||||
await dataParser.ParseAsync();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
var failedAt = DateTime.UtcNow;
|
||||
var failureReason = e.CombineMessages();
|
||||
await headquartersService.SetOperationEndedWithFailAsync(operation.Id, failedAt, failureReason);
|
||||
|
||||
state.InvokeOnOperationEnded(operation.Id, operation.CampaignId, failedAt, failureReason);
|
||||
}
|
||||
|
||||
state.UnsetProcessingOperation(operation);
|
||||
}
|
||||
|
||||
await Task.Delay(SLEEP_TIME, stoppingToken);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task InitializeStateAsync()
|
||||
|
||||
Reference in New Issue
Block a user