-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BackgroundWorkers should be restartable to permit changes in Timer interval #19986
BackgroundWorkers should be restartable to permit changes in Timer interval #19986
Conversation
…terval As BackgroundWorkers are a Singleton, they should be restartable to permit changes in Timer interval.
hi
Can you share some code to show this case? The |
When the Host changes a system-wide polling interval setting a local message instructs the PerTenantBackgroundWorker to change as shown - Example code: public class PerTenantBackgroundWorker : AsyncPeriodicBackgroundWorkerBase
{
...
public async Task ChangePollingInterval(int newPollingInterval)
{
var oldValue = Timer.Period / Minutes;
logger.LogInformation($"{ToString()} changing polling interval from {oldValue} to {newPollingInterval} (mins)");
await StopAsync(StoppingToken);
Timer.Period = newPollingInterval * Minutes;
await StartAsync();
logger.LogInformation($"{ToString()} Changed polling interval to: {newPollingInterval}");
}
...
} If the The change makes it routine to change the polling interval without needing an application restart, |
ok, We can make these properties to
We can drop the What do you think? |
Yes. Since StopAsync is overridable, this will work too. |
Can you update your PR's code? Thanks |
Updated: 9e051f6 |
Thanks! |
As BackgroundWorkers are a Singleton, they should be restartable to permit changes in Timer interval without an application restart.
Description
When StopAsync is invoked followed by a StartAsync on a BackgroundWorker the StoppingTokenSource is not renewed and will throw if the BackgroundWorker is stopped a second time.
The change assigns a new CancellationTokenSource to the StoppingTokenSource property and assigns a new token from the new source.
Checklist