From e7f646c408bb6fc915bc1d12ba4ad5a88f5d5c9b Mon Sep 17 00:00:00 2001 From: "HOME-LAPTOP\\kshkulev" Date: Sat, 1 Nov 2025 20:13:20 +0900 Subject: [PATCH] Fix campaign steps --- .../OperationExecutionService.cs | 4 ++++ .../OperationExecutionState.cs | 19 +++++++++++++++---- .../BackgroundServices/ResultWaitService.cs | 7 +++++++ .../BackgroundServices/ResultWaitState.cs | 19 +++++++++++++++---- 4 files changed, 41 insertions(+), 8 deletions(-) diff --git a/Hcs.WebApp/BackgroundServices/OperationExecutionService.cs b/Hcs.WebApp/BackgroundServices/OperationExecutionService.cs index 09eefa0..834e974 100644 --- a/Hcs.WebApp/BackgroundServices/OperationExecutionService.cs +++ b/Hcs.WebApp/BackgroundServices/OperationExecutionService.cs @@ -29,6 +29,8 @@ namespace Hcs.WebApp.BackgroundServices { while (state.TryDequeueOperation(out var operation)) { + state.SetProcessingOperation(operation); + if (stoppingToken.IsCancellationRequested) return; var scope = scopeFactory.CreateScope(); @@ -49,6 +51,8 @@ namespace Hcs.WebApp.BackgroundServices { await headquartersService.SetOperationMessageGuidAsync(operation.Id, messageGuid); } + + state.UnsetProcessingOperation(operation); } await Task.Delay(SLEEP_TIME, stoppingToken); diff --git a/Hcs.WebApp/BackgroundServices/OperationExecutionState.cs b/Hcs.WebApp/BackgroundServices/OperationExecutionState.cs index a1480f2..5e11a5b 100644 --- a/Hcs.WebApp/BackgroundServices/OperationExecutionState.cs +++ b/Hcs.WebApp/BackgroundServices/OperationExecutionState.cs @@ -5,21 +5,32 @@ namespace Hcs.WebApp.BackgroundServices { public class OperationExecutionState { - private readonly ConcurrentQueue operations = new(); + private readonly ConcurrentQueue operationsInQueue = new(); + private readonly HashSet operationsInProcess = []; public void EnqueueOperation(Operation operation) { - operations.Enqueue(operation); + operationsInQueue.Enqueue(operation); } public bool TryDequeueOperation(out Operation operation) { - return operations.TryDequeue(out operation); + return operationsInQueue.TryDequeue(out operation); + } + + public void SetProcessingOperation(Operation operation) + { + operationsInProcess.Add(operation); + } + + public void UnsetProcessingOperation(Operation operation) + { + operationsInProcess.Remove(operation); } public bool HasCampaignOperation(int campaignId) { - return operations.Any(x => x.CampaignId == campaignId); + return operationsInProcess.Any(x => x.CampaignId == campaignId); } } } diff --git a/Hcs.WebApp/BackgroundServices/ResultWaitService.cs b/Hcs.WebApp/BackgroundServices/ResultWaitService.cs index 5d60fc4..d3af252 100644 --- a/Hcs.WebApp/BackgroundServices/ResultWaitService.cs +++ b/Hcs.WebApp/BackgroundServices/ResultWaitService.cs @@ -42,6 +42,8 @@ namespace Hcs.WebApp.BackgroundServices { while (state.TryDequeueOperation(out var operation)) { + state.SetProcessingOperation(operation); + if (stoppingToken.IsCancellationRequested) return; entries.Add(new (operation, new WaitState())); @@ -86,6 +88,11 @@ namespace Hcs.WebApp.BackgroundServices entry.state.attempt++; entry.state.timer = 0; } + + if (entry.state.done) + { + state.UnsetProcessingOperation(entry.operation); + } } entries.RemoveAll(x => x.state.done); diff --git a/Hcs.WebApp/BackgroundServices/ResultWaitState.cs b/Hcs.WebApp/BackgroundServices/ResultWaitState.cs index f88c3d8..7d6a128 100644 --- a/Hcs.WebApp/BackgroundServices/ResultWaitState.cs +++ b/Hcs.WebApp/BackgroundServices/ResultWaitState.cs @@ -5,21 +5,32 @@ namespace Hcs.WebApp.BackgroundServices { public class ResultWaitState { - private readonly ConcurrentQueue operations = new(); + private readonly ConcurrentQueue operationsInQueue = new(); + private readonly HashSet operationsInProcess = []; public void EnqueueOperation(Operation operation) { - operations.Enqueue(operation); + operationsInQueue.Enqueue(operation); } public bool TryDequeueOperation(out Operation operation) { - return operations.TryDequeue(out operation); + return operationsInQueue.TryDequeue(out operation); + } + + public void SetProcessingOperation(Operation operation) + { + operationsInProcess.Add(operation); + } + + public void UnsetProcessingOperation(Operation operation) + { + operationsInProcess.Remove(operation); } public bool HasCampaignOperation(int campaignId) { - return operations.Any(x => x.CampaignId == campaignId); + return operationsInProcess.Any(x => x.CampaignId == campaignId); } } }