From 1df126935a7cc6a72ae879794edc3aa9164ae2da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Teme=C5=A1inko?= Date: Mon, 5 Oct 2020 15:34:11 +0200 Subject: [PATCH] Fix not decrementing number of running queue tasks on fail (#192) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Jan Temešinko --- Src/Coravel/Queuing/Queue.cs | 10 +++++----- .../CoravelUnitTests/Queuing/QueueMetricTests.cs | 11 +++++++++-- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/Src/Coravel/Queuing/Queue.cs b/Src/Coravel/Queuing/Queue.cs index 37f14ea8..8b9a54e9 100644 --- a/Src/Coravel/Queuing/Queue.cs +++ b/Src/Coravel/Queuing/Queue.cs @@ -208,7 +208,6 @@ private async Task InvokeTask(ActionOrAsyncFunc task) await task.Invoke(); - Interlocked.Decrement(ref this._tasksRunningCount); this._logger?.LogInformation("Queued task finished..."); await this.TryDispatchEvent(new QueueTaskCompleted(task.Guid)); } @@ -216,10 +215,11 @@ private async Task InvokeTask(ActionOrAsyncFunc task) { await this.TryDispatchEvent(new DequeuedTaskFailed(task)); - if (this._errorHandler != null) - { - this._errorHandler(e); - } + _errorHandler?.Invoke(e); + } + finally + { + Interlocked.Decrement(ref this._tasksRunningCount); } } } diff --git a/Src/UnitTests/CoravelUnitTests/Queuing/QueueMetricTests.cs b/Src/UnitTests/CoravelUnitTests/Queuing/QueueMetricTests.cs index 05c1c0b0..db39d9de 100644 --- a/Src/UnitTests/CoravelUnitTests/Queuing/QueueMetricTests.cs +++ b/Src/UnitTests/CoravelUnitTests/Queuing/QueueMetricTests.cs @@ -32,7 +32,11 @@ public async Task TestQueueHasCorrectNumberOfJobsRunning() Queue queue = new Queue(null, new DispatcherStub()); queue.QueueAsyncTask(() => Task.Delay(200)); - queue.QueueAsyncTask(() => Task.Delay(200)); + queue.QueueAsyncTask(async () => + { + await Task.Delay(200); + throw new Exception("Test exception"); + }); queue.QueueAsyncTask(() => Task.Delay(200)); Assert.Equal(3, queue.GetMetrics().WaitingCount()); @@ -45,7 +49,10 @@ public async Task TestQueueHasCorrectNumberOfJobsRunning() Assert.Equal(3, metrics.RunningCount()); await consumingTask; - } + + metrics = queue.GetMetrics(); + Assert.Equal(0, metrics.RunningCount()); + } [Fact] public async Task TestAllGuidMethods()