Add result wait service

This commit is contained in:
2025-10-30 12:25:03 +09:00
parent 3bd7341ecc
commit e31a075f58
10 changed files with 152 additions and 32 deletions

View File

@ -3,8 +3,16 @@ using Hcs.WebApp.Services;
namespace Hcs.WebApp.BackgroundServices.CampaignManagers
{
public class ExportCommonRegistryElementsManager_15_7_0_1(OperationExecutionState operationExecutionState, Campaign campaign) : ManagerBase(operationExecutionState, campaign)
public class ExportCommonRegistryElementsManager_15_7_0_1(OperationExecutionState operationExecutionState, ResultWaitState resultWaitState, Campaign campaign) : ManagerBase(operationExecutionState, resultWaitState, campaign)
{
private enum Step
{
Start = 0,
Execution = 1,
Wait = 2,
End = 3
}
public override async Task StartAsync(IServiceScope scope)
{
if (campaign.StartedAt.HasValue)
@ -22,10 +30,14 @@ namespace Hcs.WebApp.BackgroundServices.CampaignManagers
try
{
await headquartersService.SetCampaignStartedAsync(context, campaign.Id);
await headquartersService.SetCampaignStepAsync(context, campaign.Id, 1);
campaign.Step = (int)Step.Execution;
await headquartersService.UpdateCampaignStepAsync(context, campaign);
var registryCount = await registryService.GetRegistryCountAsync(context, true);
operations = await headquartersService.InitiateOperationsAsync(context, registryCount, campaign.Id, Operation.OperationType.NsiCommon_ExportNsiItem_15_7_0_1);
await registryService.SetOperationsToRegistriesAsync(context, true, operations);
await transaction.CommitAsync();
}
catch
@ -50,13 +62,51 @@ namespace Hcs.WebApp.BackgroundServices.CampaignManagers
{
if (State == IManager.ManagerState.Started)
{
var headquartersService = scope.ServiceProvider.GetRequiredService<HeadquartersService>();
var hasActiveOperations = await headquartersService.HasActiveOperationsAsync(campaign.Id);
if (!hasActiveOperations)
switch ((Step)campaign.Step)
{
await headquartersService.SetCampaignEndedAsync(campaign.Id);
case Step.Execution:
if (!operationExecutionState.HasCampaignOperation(campaign.Id))
{
campaign.Step = (int)Step.Wait;
var headquartersService = scope.ServiceProvider.GetRequiredService<HeadquartersService>();
await headquartersService.UpdateCampaignStepAsync(campaign);
State = IManager.ManagerState.Ended;
var operations = await headquartersService.GetOperationsAsync(campaign.Id);
foreach (var operation in operations)
{
if (!string.IsNullOrEmpty(operation.MessageGuid))
{
resultWaitState.EnqueueOperation(operation);
}
}
}
break;
case Step.Wait:
if (!resultWaitState.HasCampaignOperation(campaign.Id))
{
var headquartersService = scope.ServiceProvider.GetRequiredService<HeadquartersService>();
using var context = headquartersService.GetNewContext();
using var transaction = await context.Database.BeginTransactionAsync();
try
{
campaign.Step = (int)Step.End;
await headquartersService.UpdateCampaignStepAsync(context, campaign);
await headquartersService.SetCampaignEndedAsync(campaign.Id);
await transaction.CommitAsync();
}
catch
{
transaction?.Rollback();
throw;
}
State = IManager.ManagerState.Ended;
}
break;
}
}
}