Hangfire Shutdown Not Waiting to Kill Process

1.4k Views Asked by At

We have configured Hangfire to run as part of our web app using OWIN as provided in the tutorial.

We enqueue long running background process via an API we provide. The job we run initializes a R process in the background using the.Net Process class. The R code internally spawns a number of processes internally to finish the job faster. We get a number of Rscript process running in Task manager when the job runs.

On manually Recycling the app pool of our web app(to see how process restart works) the Rscript Processes are not killed. We have a custom kill strategy we to get rid of all the Rscript process within our code.

while (IsNotTimedOut())
{
    try
    {
        _token.ThrowIfCancellationRequested();
        Thread.Sleep(2000);
    }
    catch (OperationCanceledException)
    {
        Kill();
        throw;
    }
}

Within the kill method, we block using Process.WaitForExit() method.

When we do the manual recycle all the process are not killed. The current thread instead of blocking for the process to kill just dies after killing a couple of Rscript processes.

The hangfire code seems to just cancel the token and it doesn't seem to wait for the process to get killed that are listening on the cancellation token. Please, can someone suggest how do we get this working, Please let me know if more details are required?

0

There are 0 best solutions below