Implement parse campaign manager

This commit is contained in:
2025-11-20 11:22:37 +09:00
parent 8fdb855d05
commit f3249da6d9
6 changed files with 121 additions and 11 deletions

View File

@ -8,7 +8,8 @@ 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)
{
private enum Step
{

View File

@ -8,7 +8,8 @@ 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)
{
private enum Step
{

View File

@ -8,11 +8,13 @@ namespace Hcs.WebApp.BackgroundServices.CampaignManagers
IServiceScopeFactory scopeFactory,
OperationExecutionState operationExecutionState,
ResultGetState resultGetState,
DataParsingState dataParsingState,
Campaign campaign) : IManager
{
protected readonly IServiceScope scope = scopeFactory.CreateScope();
protected readonly OperationExecutionState operationExecutionState = operationExecutionState;
protected readonly ResultGetState resultGetState = resultGetState;
protected readonly DataParsingState dataParsingState = dataParsingState;
protected readonly Campaign campaign = campaign;
private HeadquartersService? headquartersService;

View File

@ -2,7 +2,7 @@
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 OperationExecutionState operationExecutionState = operationExecutionState;
@ -12,9 +12,9 @@ namespace Hcs.WebApp.BackgroundServices.CampaignManagers
{
return campaign.Type switch
{
Campaign.CampaignType.ExportPrivateRegistryElements_15_7_0_1 => new ExportPrivateRegistryElementsManager_15_7_0_1(serviceScopeFactory, operationExecutionState, resultGetState, campaign),
Campaign.CampaignType.ExportCommonRegistryElements_15_7_0_1 => new ExportCommonRegistryElementsManager_15_7_0_1(serviceScopeFactory, operationExecutionState, resultGetState, campaign),
Campaign.CampaignType.ParseHousesData_15_7_0_1 => new ParseHousesDataManager_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, dataParsingState, campaign),
Campaign.CampaignType.ParseHousesData_15_7_0_1 => new ParseHousesDataManager_15_7_0_1(serviceScopeFactory, operationExecutionState, resultGetState, dataParsingState, campaign)
};
}
}

View File

@ -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);
}
}
}