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

@ -3,24 +3,30 @@ using Hcs.WebApp.Services;
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 ResultWaitState resultWaitState = resultWaitState;
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 abstract Task StartAsync(IServiceScope scope);
public abstract Task ProcessAsync();
public abstract Task CheckStateAsync(IServiceScope scope);
public async Task EndWithFailAsync(IServiceScope scope, Exception e)
public async Task EndWithFailAsync(Exception e)
{
State = IManager.ManagerState.Ended;
await HeadquartersService.SetCampaignEndedWithFailAsync(campaign.Id, e.Message);
var headquartersService = scope.ServiceProvider.GetRequiredService<HeadquartersService>();
await headquartersService.SetCampaignEndedWithFailAsync(campaign.Id, e.Message);
State = IManager.ManagerState.Ended;
}
}
}