Refactor campaign manager

This commit is contained in:
2025-10-30 17:12:37 +09:00
parent e31a075f58
commit 9515e8080c
5 changed files with 94 additions and 92 deletions

View File

@ -26,23 +26,14 @@ namespace Hcs.WebApp.BackgroundServices
{ {
if (stoppingToken.IsCancellationRequested) return; if (stoppingToken.IsCancellationRequested) return;
using var scope = scopeFactory.CreateScope();
var manager = managerFactory.CreateManager(campaign); var manager = managerFactory.CreateManager(campaign);
if (manager != null) if (manager != null)
{ {
try
{
await manager.StartAsync(scope);
managers.Add(manager); managers.Add(manager);
} }
catch (Exception e)
{
await manager.EndWithFailAsync(scope, e);
}
}
else else
{ {
using var scope = scopeFactory.CreateScope();
var headquartersService = scope.ServiceProvider.GetRequiredService<HeadquartersService>(); var headquartersService = scope.ServiceProvider.GetRequiredService<HeadquartersService>();
await headquartersService.SetCampaignEndedWithFailAsync(campaign.Id, "Не удалось найти подходящий менеджер кампании"); await headquartersService.SetCampaignEndedWithFailAsync(campaign.Id, "Не удалось найти подходящий менеджер кампании");
} }
@ -50,14 +41,13 @@ namespace Hcs.WebApp.BackgroundServices
foreach (var manager in managers) foreach (var manager in managers)
{ {
using var scope = scopeFactory.CreateScope();
try try
{ {
await manager.CheckStateAsync(scope); await manager.ProcessAsync();
} }
catch (Exception e) catch (Exception e)
{ {
await manager.EndWithFailAsync(scope, e); await manager.EndWithFailAsync(e);
} }
} }

View File

@ -3,40 +3,58 @@ using Hcs.WebApp.Services;
namespace Hcs.WebApp.BackgroundServices.CampaignManagers namespace Hcs.WebApp.BackgroundServices.CampaignManagers
{ {
public class ExportCommonRegistryElementsManager_15_7_0_1(OperationExecutionState operationExecutionState, ResultWaitState resultWaitState, Campaign campaign) : ManagerBase(operationExecutionState, resultWaitState, campaign) public class ExportCommonRegistryElementsManager_15_7_0_1(
IServiceScopeFactory scopeFactory,
OperationExecutionState operationExecutionState,
ResultWaitState resultWaitState,
Campaign campaign) : ManagerBase(scopeFactory, operationExecutionState, resultWaitState, campaign)
{ {
private enum Step private enum Step
{ {
Start = 0, Start = 0,
Execution = 1, ExecutionWait = 1,
Wait = 2, ResultWait = 2,
End = 3 End = 3
} }
public override async Task StartAsync(IServiceScope scope) private RegistryService? registryService;
{
if (campaign.StartedAt.HasValue)
{
State = IManager.ManagerState.Started;
return; protected RegistryService RegistryService => registryService ??= scope.ServiceProvider.GetRequiredService<RegistryService>();
public override async Task ProcessAsync()
{
switch ((Step)campaign.Step)
{
case Step.Start:
await DoStartStepAsync();
break;
case Step.ExecutionWait:
await DoExecutionWaitStepAsync();
break;
case Step.ResultWait:
await DoResultWaitStepAsync();
break;
}
} }
var headquartersService = scope.ServiceProvider.GetRequiredService<HeadquartersService>(); private async Task DoStartStepAsync()
var registryService = scope.ServiceProvider.GetRequiredService<RegistryService>(); {
using var context = headquartersService.GetNewContext(); IEnumerable<Operation> operations;
using var context = HeadquartersService.GetNewContext();
using var transaction = await context.Database.BeginTransactionAsync(); using var transaction = await context.Database.BeginTransactionAsync();
IEnumerable<Operation> operations = null;
try try
{ {
await headquartersService.SetCampaignStartedAsync(context, campaign.Id); await HeadquartersService.SetCampaignStartedAsync(context, campaign.Id);
campaign.Step = (int)Step.Execution; campaign.Step = (int)Step.ExecutionWait;
await headquartersService.UpdateCampaignStepAsync(context, campaign); await HeadquartersService.UpdateCampaignStepAsync(context, campaign);
var registryCount = await registryService.GetRegistryCountAsync(context, true); var registryCount = await RegistryService.GetRegistryCountAsync(context, true);
operations = await headquartersService.InitiateOperationsAsync(context, registryCount, campaign.Id, Operation.OperationType.NsiCommon_ExportNsiItem_15_7_0_1); operations = await HeadquartersService.InitiateOperationsAsync(context, registryCount, campaign.Id, Operation.OperationType.NsiCommon_ExportNsiItem_15_7_0_1);
await registryService.SetOperationsToRegistriesAsync(context, true, operations); await RegistryService.SetOperationsToRegistriesAsync(context, true, operations);
await transaction.CommitAsync(); await transaction.CommitAsync();
} }
@ -58,20 +76,14 @@ namespace Hcs.WebApp.BackgroundServices.CampaignManagers
State = IManager.ManagerState.Started; State = IManager.ManagerState.Started;
} }
public override async Task CheckStateAsync(IServiceScope scope) private async Task DoExecutionWaitStepAsync()
{ {
if (State == IManager.ManagerState.Started) if (operationExecutionState.HasCampaignOperation(campaign.Id)) return;
{
switch ((Step)campaign.Step)
{
case Step.Execution:
if (!operationExecutionState.HasCampaignOperation(campaign.Id))
{
campaign.Step = (int)Step.Wait;
var headquartersService = scope.ServiceProvider.GetRequiredService<HeadquartersService>();
await headquartersService.UpdateCampaignStepAsync(campaign);
var operations = await headquartersService.GetOperationsAsync(campaign.Id); campaign.Step = (int)Step.ResultWait;
await HeadquartersService.UpdateCampaignStepAsync(campaign);
var operations = await HeadquartersService.GetOperationsAsync(campaign.Id);
foreach (var operation in operations) foreach (var operation in operations)
{ {
if (!string.IsNullOrEmpty(operation.MessageGuid)) if (!string.IsNullOrEmpty(operation.MessageGuid))
@ -80,20 +92,19 @@ namespace Hcs.WebApp.BackgroundServices.CampaignManagers
} }
} }
} }
break;
case Step.Wait: private async Task DoResultWaitStepAsync()
if (!resultWaitState.HasCampaignOperation(campaign.Id))
{ {
var headquartersService = scope.ServiceProvider.GetRequiredService<HeadquartersService>(); if (resultWaitState.HasCampaignOperation(campaign.Id)) return;
using var context = headquartersService.GetNewContext();
using var context = HeadquartersService.GetNewContext();
using var transaction = await context.Database.BeginTransactionAsync(); using var transaction = await context.Database.BeginTransactionAsync();
try try
{ {
campaign.Step = (int)Step.End; campaign.Step = (int)Step.End;
await headquartersService.UpdateCampaignStepAsync(context, campaign); await HeadquartersService.UpdateCampaignStepAsync(context, campaign);
await headquartersService.SetCampaignEndedAsync(campaign.Id); await HeadquartersService.SetCampaignEndedAsync(campaign.Id);
await transaction.CommitAsync(); await transaction.CommitAsync();
} }
@ -106,9 +117,5 @@ namespace Hcs.WebApp.BackgroundServices.CampaignManagers
State = IManager.ManagerState.Ended; State = IManager.ManagerState.Ended;
} }
break;
}
}
}
} }
} }

View File

@ -11,10 +11,8 @@
public ManagerState State { get; } public ManagerState State { get; }
Task StartAsync(IServiceScope scope); Task ProcessAsync();
Task CheckStateAsync(IServiceScope scope); Task EndWithFailAsync(Exception e);
Task EndWithFailAsync(IServiceScope scope, Exception e);
} }
} }

View File

@ -3,24 +3,30 @@ using Hcs.WebApp.Services;
namespace Hcs.WebApp.BackgroundServices.CampaignManagers namespace Hcs.WebApp.BackgroundServices.CampaignManagers
{ {
public abstract class ManagerBase(OperationExecutionState operationExecutionState, ResultWaitState resultWaitState, Campaign campaign) : IManager public abstract class ManagerBase(
IServiceScopeFactory scopeFactory,
OperationExecutionState operationExecutionState,
ResultWaitState resultWaitState,
Campaign campaign) : IManager
{ {
protected readonly IServiceScope scope = scopeFactory.CreateScope();
protected readonly OperationExecutionState operationExecutionState = operationExecutionState; protected readonly OperationExecutionState operationExecutionState = operationExecutionState;
protected readonly ResultWaitState resultWaitState = resultWaitState; protected readonly ResultWaitState resultWaitState = resultWaitState;
protected readonly Campaign campaign = campaign; protected readonly Campaign campaign = campaign;
private HeadquartersService? headquartersService;
protected HeadquartersService HeadquartersService => headquartersService ??= scope.ServiceProvider.GetRequiredService<HeadquartersService>();
public IManager.ManagerState State { get; protected set; } = IManager.ManagerState.Created; public IManager.ManagerState State { get; protected set; } = IManager.ManagerState.Created;
public abstract Task StartAsync(IServiceScope scope); public abstract Task ProcessAsync();
public abstract Task CheckStateAsync(IServiceScope scope); public async Task EndWithFailAsync(Exception e)
public async Task EndWithFailAsync(IServiceScope scope, Exception e)
{ {
State = IManager.ManagerState.Ended; await HeadquartersService.SetCampaignEndedWithFailAsync(campaign.Id, e.Message);
var headquartersService = scope.ServiceProvider.GetRequiredService<HeadquartersService>(); State = IManager.ManagerState.Ended;
await headquartersService.SetCampaignEndedWithFailAsync(campaign.Id, e.Message);
} }
} }
} }

View File

@ -2,17 +2,18 @@
namespace Hcs.WebApp.BackgroundServices.CampaignManagers namespace Hcs.WebApp.BackgroundServices.CampaignManagers
{ {
public class ManagerFactory(OperationExecutionState operationExecutionState, ResultWaitState resultWaitState) public class ManagerFactory(IServiceScopeFactory serviceScopeFactory, OperationExecutionState operationExecutionState, ResultWaitState resultWaitState)
{ {
protected readonly IServiceScopeFactory serviceScopeFactory = serviceScopeFactory;
protected readonly OperationExecutionState operationExecutionState = operationExecutionState; protected readonly OperationExecutionState operationExecutionState = operationExecutionState;
protected readonly ResultWaitState resultWaitState; protected readonly ResultWaitState resultWaitState = resultWaitState;
public IManager? CreateManager(Campaign campaign) public IManager? CreateManager(Campaign campaign)
{ {
switch (campaign.Type) switch (campaign.Type)
{ {
case Campaign.CampaignType.ExportCommonRegistryElements_15_7_0_1: case Campaign.CampaignType.ExportCommonRegistryElements_15_7_0_1:
return new ExportCommonRegistryElementsManager_15_7_0_1(operationExecutionState, resultWaitState, campaign); return new ExportCommonRegistryElementsManager_15_7_0_1(serviceScopeFactory, operationExecutionState, resultWaitState, campaign);
} }
return null; return null;