117 lines
3.9 KiB
C#
117 lines
3.9 KiB
C#
using Hcs.WebApp.Data.Hcs;
|
|
using Hcs.WebApp.Data.Hcs.CampaignArgs;
|
|
using Hcs.WebApp.Services;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Hcs.WebApp.BackgroundServices.CampaignManagers
|
|
{
|
|
public class ParseHousesDataManager_15_7_0_1(
|
|
IServiceScopeFactory scopeFactory,
|
|
OperationExecutionState operationExecutionState,
|
|
ResultGetState resultGetState,
|
|
DataParsingState dataParsingState,
|
|
Campaign campaign) : ManagerBase(scopeFactory, operationExecutionState, resultGetState, dataParsingState, campaign)
|
|
{
|
|
private enum Step
|
|
{
|
|
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)
|
|
{
|
|
dataParsingState.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);
|
|
}
|
|
}
|
|
}
|