CancellationToken grace period timeout?


#1

I’m trying to make long-lasting background jobs resilient to AppPool shutdowns/recycles.

For that, I am supposed to use a CancellationToken argument. The documentation states “Dedicated background process is watching for all the current background jobs that have a cancellation token parameter in a method and…”.

My first question: what if my job does not have a CancellationToken argument, but has a PerformContext argument ? Will it then also be watching that job ? (Knowing that PerformContext holds a CancellationToken).

Using a cancellation token, my job method is supposed to poll it (i.e. by regularly calling ThrowIfCancellationRequested()).

My second question: how long does my job get to gracefully shutdown when cancellation is requested ? For now I just see a ThreadAbortException being thrown, before polling the cancellation token even has a chance.

Can I configure a timeout for graceful shutdown ?
I tried setting StopTimeout, ServerTimeout and ShutdownTimeout on the BackgroundJobServerOptions, none of them seem to do anything.

I’m using Hangfire hosted in an ASP.NET application. Hangfire is configured using OWIN’s app.UseHangfireServer(options) call.

“Regular” ASP.NET IRegisteredObjects get about 30 seconds to gracefully stop. How can I have similar behaviour in Hangfire ?


#2

I can add to it that according to the docs: upgrading to 1.7, Upgrade Steps, 1, d) in:
https://docs.hangfire.io/en/latest/upgrade-guides/upgrading-to-hangfire-1.7.html

Setting the StopTimeout for background processing servers should give background jobs some time to be processed during the shutdown event, instead of instantly aborting them.

But that doesn’t seem to work. Or am I missing something ?