From 96c562b8bbd1e3d3bf12ab735bf3aa8530b2fb14 Mon Sep 17 00:00:00 2001 From: machonky Date: Thu, 6 Jun 2024 13:21:00 +0800 Subject: [PATCH 1/3] BackgroundWorkers should be restartable to permit changes in Timer interval As BackgroundWorkers are a Singleton, they should be restartable to permit changes in Timer interval. --- .../Abp/BackgroundWorkers/BackgroundWorkerBase.cs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/BackgroundWorkerBase.cs b/framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/BackgroundWorkerBase.cs index 91bc360f72d..ca9cdf4a51c 100644 --- a/framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/BackgroundWorkerBase.cs +++ b/framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/BackgroundWorkerBase.cs @@ -22,10 +22,16 @@ public abstract class BackgroundWorkerBase : IBackgroundWorker protected ILogger Logger => LazyServiceProvider.LazyGetService(provider => LoggerFactory?.CreateLogger(GetType().FullName!) ?? NullLogger.Instance); - protected CancellationTokenSource StoppingTokenSource { get; } - protected CancellationToken StoppingToken { get; } + protected CancellationTokenSource StoppingTokenSource { get; private set; } + + protected CancellationToken StoppingToken { get; private set; } public BackgroundWorkerBase() + { + ResetStoppingCancellationTokenSource(); + } + + private void ResetStoppingCancellationTokenSource() { StoppingTokenSource = new CancellationTokenSource(); StoppingToken = StoppingTokenSource.Token; @@ -42,6 +48,9 @@ public virtual Task StopAsync(CancellationToken cancellationToken = default) Logger.LogDebug("Stopped background worker: " + ToString()); StoppingTokenSource.Cancel(); StoppingTokenSource.Dispose(); + + ResetStoppingCancellationTokenSource(); + return Task.CompletedTask; } From 9e051f6a18805b7a9096fb08effaf59b9859f0fb Mon Sep 17 00:00:00 2001 From: machonky Date: Wed, 12 Jun 2024 10:55:03 +0800 Subject: [PATCH 2/3] Ready for PR --- .../Abp/BackgroundWorkers/BackgroundWorkerBase.cs | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/BackgroundWorkerBase.cs b/framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/BackgroundWorkerBase.cs index ca9cdf4a51c..10718662104 100644 --- a/framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/BackgroundWorkerBase.cs +++ b/framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/BackgroundWorkerBase.cs @@ -22,16 +22,11 @@ public abstract class BackgroundWorkerBase : IBackgroundWorker protected ILogger Logger => LazyServiceProvider.LazyGetService(provider => LoggerFactory?.CreateLogger(GetType().FullName!) ?? NullLogger.Instance); - protected CancellationTokenSource StoppingTokenSource { get; private set; } + protected CancellationTokenSource StoppingTokenSource { get; set; } - protected CancellationToken StoppingToken { get; private set; } + protected CancellationToken StoppingToken { get; set; } public BackgroundWorkerBase() - { - ResetStoppingCancellationTokenSource(); - } - - private void ResetStoppingCancellationTokenSource() { StoppingTokenSource = new CancellationTokenSource(); StoppingToken = StoppingTokenSource.Token; @@ -47,10 +42,7 @@ public virtual Task StopAsync(CancellationToken cancellationToken = default) { Logger.LogDebug("Stopped background worker: " + ToString()); StoppingTokenSource.Cancel(); - StoppingTokenSource.Dispose(); - - ResetStoppingCancellationTokenSource(); - + StoppingTokenSource.Dispose(); return Task.CompletedTask; } From 7efe0a4a628a19fa08fbf304538784064dade56f Mon Sep 17 00:00:00 2001 From: machonky Date: Wed, 12 Jun 2024 11:52:11 +0800 Subject: [PATCH 3/3] BackgroundWorkerBase - Whitespace change --- .../Volo/Abp/BackgroundWorkers/BackgroundWorkerBase.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/BackgroundWorkerBase.cs b/framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/BackgroundWorkerBase.cs index 10718662104..0d70ae6aa9d 100644 --- a/framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/BackgroundWorkerBase.cs +++ b/framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/BackgroundWorkerBase.cs @@ -42,7 +42,7 @@ public virtual Task StopAsync(CancellationToken cancellationToken = default) { Logger.LogDebug("Stopped background worker: " + ToString()); StoppingTokenSource.Cancel(); - StoppingTokenSource.Dispose(); + StoppingTokenSource.Dispose(); return Task.CompletedTask; }