Implement parse campaign manager
This commit is contained in:
@ -8,7 +8,8 @@ namespace Hcs.WebApp.BackgroundServices.CampaignManagers
|
|||||||
IServiceScopeFactory scopeFactory,
|
IServiceScopeFactory scopeFactory,
|
||||||
OperationExecutionState operationExecutionState,
|
OperationExecutionState operationExecutionState,
|
||||||
ResultGetState resultGetState,
|
ResultGetState resultGetState,
|
||||||
Campaign campaign) : ManagerBase(scopeFactory, operationExecutionState, resultGetState, campaign)
|
DataParsingState dataParsingState,
|
||||||
|
Campaign campaign) : ManagerBase(scopeFactory, operationExecutionState, resultGetState, dataParsingState, campaign)
|
||||||
{
|
{
|
||||||
private enum Step
|
private enum Step
|
||||||
{
|
{
|
||||||
|
|||||||
@ -8,7 +8,8 @@ namespace Hcs.WebApp.BackgroundServices.CampaignManagers
|
|||||||
IServiceScopeFactory scopeFactory,
|
IServiceScopeFactory scopeFactory,
|
||||||
OperationExecutionState operationExecutionState,
|
OperationExecutionState operationExecutionState,
|
||||||
ResultGetState resultGetState,
|
ResultGetState resultGetState,
|
||||||
Campaign campaign) : ManagerBase(scopeFactory, operationExecutionState, resultGetState, campaign)
|
DataParsingState dataParsingState,
|
||||||
|
Campaign campaign) : ManagerBase(scopeFactory, operationExecutionState, resultGetState, dataParsingState, campaign)
|
||||||
{
|
{
|
||||||
private enum Step
|
private enum Step
|
||||||
{
|
{
|
||||||
|
|||||||
@ -8,11 +8,13 @@ namespace Hcs.WebApp.BackgroundServices.CampaignManagers
|
|||||||
IServiceScopeFactory scopeFactory,
|
IServiceScopeFactory scopeFactory,
|
||||||
OperationExecutionState operationExecutionState,
|
OperationExecutionState operationExecutionState,
|
||||||
ResultGetState resultGetState,
|
ResultGetState resultGetState,
|
||||||
|
DataParsingState dataParsingState,
|
||||||
Campaign campaign) : IManager
|
Campaign campaign) : IManager
|
||||||
{
|
{
|
||||||
protected readonly IServiceScope scope = scopeFactory.CreateScope();
|
protected readonly IServiceScope scope = scopeFactory.CreateScope();
|
||||||
protected readonly OperationExecutionState operationExecutionState = operationExecutionState;
|
protected readonly OperationExecutionState operationExecutionState = operationExecutionState;
|
||||||
protected readonly ResultGetState resultGetState = resultGetState;
|
protected readonly ResultGetState resultGetState = resultGetState;
|
||||||
|
protected readonly DataParsingState dataParsingState = dataParsingState;
|
||||||
protected readonly Campaign campaign = campaign;
|
protected readonly Campaign campaign = campaign;
|
||||||
|
|
||||||
private HeadquartersService? headquartersService;
|
private HeadquartersService? headquartersService;
|
||||||
|
|||||||
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
namespace Hcs.WebApp.BackgroundServices.CampaignManagers
|
namespace Hcs.WebApp.BackgroundServices.CampaignManagers
|
||||||
{
|
{
|
||||||
public class ManagerFactory(IServiceScopeFactory serviceScopeFactory, OperationExecutionState operationExecutionState, ResultGetState resultGetState)
|
public class ManagerFactory(IServiceScopeFactory serviceScopeFactory, OperationExecutionState operationExecutionState, ResultGetState resultGetState, DataParsingState dataParsingState)
|
||||||
{
|
{
|
||||||
protected readonly IServiceScopeFactory serviceScopeFactory = serviceScopeFactory;
|
protected readonly IServiceScopeFactory serviceScopeFactory = serviceScopeFactory;
|
||||||
protected readonly OperationExecutionState operationExecutionState = operationExecutionState;
|
protected readonly OperationExecutionState operationExecutionState = operationExecutionState;
|
||||||
@ -12,9 +12,9 @@ namespace Hcs.WebApp.BackgroundServices.CampaignManagers
|
|||||||
{
|
{
|
||||||
return campaign.Type switch
|
return campaign.Type switch
|
||||||
{
|
{
|
||||||
Campaign.CampaignType.ExportPrivateRegistryElements_15_7_0_1 => new ExportPrivateRegistryElementsManager_15_7_0_1(serviceScopeFactory, operationExecutionState, resultGetState, campaign),
|
Campaign.CampaignType.ExportPrivateRegistryElements_15_7_0_1 => new ExportPrivateRegistryElementsManager_15_7_0_1(serviceScopeFactory, operationExecutionState, resultGetState, dataParsingState, campaign),
|
||||||
Campaign.CampaignType.ExportCommonRegistryElements_15_7_0_1 => new ExportCommonRegistryElementsManager_15_7_0_1(serviceScopeFactory, operationExecutionState, resultGetState, campaign),
|
Campaign.CampaignType.ExportCommonRegistryElements_15_7_0_1 => new ExportCommonRegistryElementsManager_15_7_0_1(serviceScopeFactory, operationExecutionState, resultGetState, dataParsingState, campaign),
|
||||||
Campaign.CampaignType.ParseHousesData_15_7_0_1 => new ParseHousesDataManager_15_7_0_1(serviceScopeFactory, operationExecutionState, resultGetState, campaign)
|
Campaign.CampaignType.ParseHousesData_15_7_0_1 => new ParseHousesDataManager_15_7_0_1(serviceScopeFactory, operationExecutionState, resultGetState, dataParsingState, campaign)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,4 +1,7 @@
|
|||||||
using Hcs.WebApp.Data.Hcs;
|
using Hcs.WebApp.Data.Hcs;
|
||||||
|
using Hcs.WebApp.Data.Hcs.CampaignArgs;
|
||||||
|
using Hcs.WebApp.Services;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
namespace Hcs.WebApp.BackgroundServices.CampaignManagers
|
namespace Hcs.WebApp.BackgroundServices.CampaignManagers
|
||||||
{
|
{
|
||||||
@ -6,13 +9,108 @@ namespace Hcs.WebApp.BackgroundServices.CampaignManagers
|
|||||||
IServiceScopeFactory scopeFactory,
|
IServiceScopeFactory scopeFactory,
|
||||||
OperationExecutionState operationExecutionState,
|
OperationExecutionState operationExecutionState,
|
||||||
ResultGetState resultGetState,
|
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();
|
private enum Step
|
||||||
|
|
||||||
public override Task ProcessAsync()
|
|
||||||
{
|
{
|
||||||
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -26,5 +26,13 @@ namespace Hcs.WebApp.Services
|
|||||||
await context.SaveChangesAsync();
|
await context.SaveChangesAsync();
|
||||||
return fileToParse;
|
return fileToParse;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task SetOperationToFileToParseAsync(HcsDbContext context, int fileToParseId, Operation operation)
|
||||||
|
{
|
||||||
|
var fileToParse = await context.FilesToParse.FirstOrDefaultAsync(x => x.Id == fileToParseId);
|
||||||
|
fileToParse!.LastParseOperationId = operation.Id;
|
||||||
|
|
||||||
|
await context.SaveChangesAsync();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user