diff --git a/Hcs.WebApp/BackgroundServices/CampaignManagementService.cs b/Hcs.WebApp/BackgroundServices/CampaignManagementService.cs index 2693b31..6aac2ed 100644 --- a/Hcs.WebApp/BackgroundServices/CampaignManagementService.cs +++ b/Hcs.WebApp/BackgroundServices/CampaignManagementService.cs @@ -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 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(); diff --git a/Hcs.WebApp/BackgroundServices/OperationExecutionService.cs b/Hcs.WebApp/BackgroundServices/OperationExecutionService.cs index 834e974..e0fe3a2 100644 --- a/Hcs.WebApp/BackgroundServices/OperationExecutionService.cs +++ b/Hcs.WebApp/BackgroundServices/OperationExecutionService.cs @@ -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() diff --git a/Hcs.WebApp/BackgroundServices/OperationExecutionState.cs b/Hcs.WebApp/BackgroundServices/OperationExecutionState.cs index 5e11a5b..d5d25d4 100644 --- a/Hcs.WebApp/BackgroundServices/OperationExecutionState.cs +++ b/Hcs.WebApp/BackgroundServices/OperationExecutionState.cs @@ -8,6 +8,8 @@ namespace Hcs.WebApp.BackgroundServices private readonly ConcurrentQueue operationsInQueue = new(); private readonly HashSet 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); } } } diff --git a/Hcs.WebApp/BackgroundServices/ResultWaitService.cs b/Hcs.WebApp/BackgroundServices/ResultWaitService.cs index d3af252..ccf294d 100644 --- a/Hcs.WebApp/BackgroundServices/ResultWaitService.cs +++ b/Hcs.WebApp/BackgroundServices/ResultWaitService.cs @@ -114,6 +114,8 @@ namespace Hcs.WebApp.BackgroundServices { state.EnqueueOperation(operation); } + + state.Ready = true; } private void InitializeClient() diff --git a/Hcs.WebApp/BackgroundServices/ResultWaitState.cs b/Hcs.WebApp/BackgroundServices/ResultWaitState.cs index 7d6a128..3ed9594 100644 --- a/Hcs.WebApp/BackgroundServices/ResultWaitState.cs +++ b/Hcs.WebApp/BackgroundServices/ResultWaitState.cs @@ -8,6 +8,8 @@ namespace Hcs.WebApp.BackgroundServices private readonly ConcurrentQueue operationsInQueue = new(); private readonly HashSet 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); } } }