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, 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
{ {

View File

@ -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
{ {

View File

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

View File

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

View File

@ -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
{
Start = 0,
ParseWait = 1,
End = 2
}
public override Task ProcessAsync() 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()
{ {
throw new NotImplementedException(); 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);
} }
} }
} }

View File

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