Implement parse campaign manager
This commit is contained in:
@ -1,4 +1,7 @@
|
||||
using Hcs.WebApp.Data.Hcs;
|
||||
using Hcs.WebApp.Data.Hcs.CampaignArgs;
|
||||
using Hcs.WebApp.Services;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Hcs.WebApp.BackgroundServices.CampaignManagers
|
||||
{
|
||||
@ -6,13 +9,108 @@ namespace Hcs.WebApp.BackgroundServices.CampaignManagers
|
||||
IServiceScopeFactory scopeFactory,
|
||||
OperationExecutionState operationExecutionState,
|
||||
ResultGetState resultGetState,
|
||||
Campaign campaign) : ManagerBase(scopeFactory, operationExecutionState, resultGetState, campaign)
|
||||
DataParsingState dataParsingState,
|
||||
Campaign campaign) : ManagerBase(scopeFactory, operationExecutionState, resultGetState, dataParsingState, campaign)
|
||||
{
|
||||
protected override int StepCount => throw new NotImplementedException();
|
||||
|
||||
public override Task ProcessAsync()
|
||||
private enum Step
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
Start = 0,
|
||||
ParseWait = 1,
|
||||
End = 2
|
||||
}
|
||||
|
||||
private FileToParseService? fileToParseService;
|
||||
|
||||
protected override int StepCount => Enum.GetValues(typeof(Step)).Length;
|
||||
|
||||
protected FileToParseService FileToParseService => fileToParseService ??= scope.ServiceProvider.GetRequiredService<FileToParseService>();
|
||||
|
||||
public override async Task ProcessAsync()
|
||||
{
|
||||
switch ((Step)campaign.Step)
|
||||
{
|
||||
case Step.Start:
|
||||
await DoStartStepAsync();
|
||||
break;
|
||||
|
||||
case Step.ParseWait:
|
||||
await DoParseWaitStepAsync();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task DoStartStepAsync()
|
||||
{
|
||||
DateTime startedAt = default;
|
||||
IEnumerable<Operation>? operations = null;
|
||||
|
||||
using var context = HeadquartersService.GetNewContext();
|
||||
var executionStrategy = context.Database.CreateExecutionStrategy();
|
||||
await executionStrategy.ExecuteAsync(async () =>
|
||||
{
|
||||
using var transaction = await context.Database.BeginTransactionAsync();
|
||||
try
|
||||
{
|
||||
startedAt = DateTime.UtcNow;
|
||||
await HeadquartersService.SetCampaignStartedAsync(context, campaign.Id, startedAt);
|
||||
|
||||
await ProgressStepAsync(context, (int)Step.ParseWait);
|
||||
|
||||
var args = campaign.DeserializeArgs() as CampaignParseArgs;
|
||||
operations = await HeadquartersService.InitiateOperationsAsync(context, 1, campaign.Id, Operation.OperationType.ParseHousesData_15_7_0_1);
|
||||
await FileToParseService.SetOperationToFileToParseAsync(context, args!.FileToParseId, operations.First());
|
||||
|
||||
await transaction.CommitAsync();
|
||||
}
|
||||
catch
|
||||
{
|
||||
transaction?.Rollback();
|
||||
|
||||
throw;
|
||||
}
|
||||
});
|
||||
|
||||
if (operations != null)
|
||||
{
|
||||
operationExecutionState.EnqueueOperation(operations.First());
|
||||
}
|
||||
|
||||
State = IManager.ManagerState.Started;
|
||||
|
||||
InvokeOnCampaignStarted(startedAt);
|
||||
}
|
||||
|
||||
private async Task DoParseWaitStepAsync()
|
||||
{
|
||||
if (dataParsingState.HasCampaignOperation(campaign.Id)) return;
|
||||
|
||||
DateTime endedAt = default;
|
||||
|
||||
using var context = HeadquartersService.GetNewContext();
|
||||
var executionStrategy = context.Database.CreateExecutionStrategy();
|
||||
await executionStrategy.ExecuteAsync(async () =>
|
||||
{
|
||||
using var transaction = await context.Database.BeginTransactionAsync();
|
||||
try
|
||||
{
|
||||
await ProgressStepAsync(context, (int)Step.End);
|
||||
|
||||
endedAt = DateTime.UtcNow;
|
||||
await HeadquartersService.SetCampaignEndedAsync(context, campaign.Id, endedAt);
|
||||
|
||||
await transaction.CommitAsync();
|
||||
}
|
||||
catch
|
||||
{
|
||||
transaction?.Rollback();
|
||||
|
||||
throw;
|
||||
}
|
||||
});
|
||||
|
||||
State = IManager.ManagerState.Ended;
|
||||
|
||||
InvokeOnCampaignEnded(endedAt, string.Empty);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user