Wait other states in campaign management service

This commit is contained in:
2025-11-03 11:46:58 +09:00
parent 7cfabe0af2
commit 7d375f27d6
5 changed files with 25 additions and 2 deletions

View File

@ -5,19 +5,25 @@ namespace Hcs.WebApp.BackgroundServices
{
public class CampaignManagementService(
CampaignManagementState campaignManagementState,
OperationExecutionState operationExecutionState,
ResultWaitState resultWaitState,
ManagerFactory managerFactory,
IServiceScopeFactory scopeFactory) : BackgroundService
{
private const int STATES_WAIT_TIME = 1000;
private const int ITERATION_TIME = 1000;
private const int SLEEP_TIME = 30000;
private readonly CampaignManagementState campaignManagementState = campaignManagementState;
private readonly OperationExecutionState operationExecutionState = operationExecutionState;
private readonly ResultWaitState resultWaitState = resultWaitState;
private readonly ManagerFactory managerFactory = managerFactory;
private readonly IServiceScopeFactory scopeFactory = scopeFactory;
private readonly List<IManager> managers = [];
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
await WaitForOtherStates();
await InitializeStateAsync();
while (!stoppingToken.IsCancellationRequested)
@ -64,6 +70,14 @@ namespace Hcs.WebApp.BackgroundServices
}
}
private async Task WaitForOtherStates()
{
while (!operationExecutionState.Ready || !resultWaitState.Ready)
{
await Task.Delay(STATES_WAIT_TIME);
}
}
private async Task InitializeStateAsync()
{
using var scope = scopeFactory.CreateScope();

View File

@ -4,6 +4,7 @@ using Hcs.Broker.MessageCapturer;
using Hcs.WebApp.BackgroundServices.OperationExecutors;
using Hcs.WebApp.Config;
using Hcs.WebApp.Services;
using System.Transactions;
namespace Hcs.WebApp.BackgroundServices
{
@ -69,6 +70,8 @@ namespace Hcs.WebApp.BackgroundServices
{
state.EnqueueOperation(operation);
}
state.Ready = true;
}
private void InitializeClient()

View File

@ -8,6 +8,8 @@ namespace Hcs.WebApp.BackgroundServices
private readonly ConcurrentQueue<Operation> operationsInQueue = new();
private readonly HashSet<Operation> operationsInProcess = [];
public bool Ready { get; set; }
public void EnqueueOperation(Operation operation)
{
operationsInQueue.Enqueue(operation);
@ -30,7 +32,7 @@ namespace Hcs.WebApp.BackgroundServices
public bool HasCampaignOperation(int campaignId)
{
return operationsInProcess.Any(x => x.CampaignId == campaignId);
return operationsInQueue.Any(x => x.CampaignId == campaignId) || operationsInProcess.Any(x => x.CampaignId == campaignId);
}
}
}

View File

@ -114,6 +114,8 @@ namespace Hcs.WebApp.BackgroundServices
{
state.EnqueueOperation(operation);
}
state.Ready = true;
}
private void InitializeClient()

View File

@ -8,6 +8,8 @@ namespace Hcs.WebApp.BackgroundServices
private readonly ConcurrentQueue<Operation> operationsInQueue = new();
private readonly HashSet<Operation> operationsInProcess = [];
public bool Ready { get; set; }
public void EnqueueOperation(Operation operation)
{
operationsInQueue.Enqueue(operation);
@ -30,7 +32,7 @@ namespace Hcs.WebApp.BackgroundServices
public bool HasCampaignOperation(int campaignId)
{
return operationsInProcess.Any(x => x.CampaignId == campaignId);
return operationsInQueue.Any(x => x.CampaignId == campaignId) || operationsInProcess.Any(x => x.CampaignId == campaignId);
}
}
}