Hangfire: Deleting jobs does not stop them

Created on 10 Nov 2018  路  5Comments  路  Source: HangfireIO/Hangfire

hi all. I have a long function in my .net code that runs through hangfire as a recurring job and through API call. I make this function log to a file. I made sure the function is running from the API call and through hangfire. I went to hangfire and deleted the running job. yet the job is still logging to the file and the process did not die.

the code for my function
filePath = @"c:\temp\logFile"
long counter = 0;
while(counter < 10000000){
counter ++;
File.WriteAllText(filePath, $"counter: {counter}");
}

the contents of my log file before deleting the job

counter: 100
counter: 1
counter: 101
counter: 2

apparently there are 2 threads executing.
I went to hangfire dashboard, deleted the job, but the counters are still increasing in the same manner.

is this the intended flow? one would think that deleting an executing job might mean to stop it and remove it from the queue not just remove it from the queue.

thanks

docs problem

Most helpful comment

When the Delete button is triggered, dashboard will find the corresponding background job and move it to the Deleted state. Background job execution itself will not be stopped, unless you are using IJobCancellationToken and check it in your background job as written here.

This is by design, because the only method of stopping unknown user code is to use the Thread.Abort method that's very dangerous to use by default, without knowing anything about your code as it can lead to cases (including deadlocks), where the only option is to kill the whole application.

I think we should add a description of this behavior to the corresponding dashboard page, or do anything else to make it more clear to understand.

All 5 comments

When the Delete button is triggered, dashboard will find the corresponding background job and move it to the Deleted state. Background job execution itself will not be stopped, unless you are using IJobCancellationToken and check it in your background job as written here.

This is by design, because the only method of stopping unknown user code is to use the Thread.Abort method that's very dangerous to use by default, without knowing anything about your code as it can lead to cases (including deadlocks), where the only option is to kill the whole application.

I think we should add a description of this behavior to the corresponding dashboard page, or do anything else to make it more clear to understand.

Hi.

I am facing this this issue too. The background job is marked as deleted in the database but the background job does not stop even though it has a check for token cancellation.

This is how my worker function looks like:

public async Task LiveTrackerWorker(TrackingServiceParam trackingParam, CancellationToken cancellationToken){
    //Infinite while loop with check for token cancellation
}

Please help.

Hi.

I am facing this this issue too. The background job is marked as deleted in the database but the background job does not stop even though it has a check for token cancellation.

This is how my worker function looks like:

public async Task LiveTrackerWorker(TrackingServiceParam trackingParam, CancellationToken cancellationToken){
    //Infinite while loop with check for token cancellation
}

Please help.

Did you find out a solution?

I replaced CancellationToken with Hangfire's IJobCancellationToken on the function input parameter. It worked. Thanks to @odinserj

public async Task LiveTrackerWorker(TrackingServiceParam trackingParam, IJobCancellationToken cancellationToken){
    //Infinite while loop with check for token cancellation
}

When the _Delete_ button is triggered, dashboard will find the corresponding background job and move it to the _Deleted_ state. Background job execution itself will not be stopped, unless you are using IJobCancellationToken and check it in your background job as written here.

This is by design, because the only method of stopping unknown user code is to use the Thread.Abort method that's very dangerous to use by default, without knowing anything about your code as it can lead to cases (including deadlocks), where the only option is to kill the whole application.

Is there a way to find via the Windows GUI or even Powershell to terminate the execution of the code? The background job is still running and I don't know how to delete them

Was this page helpful?
0 / 5 - 0 ratings

Related issues

osmanrahimi picture osmanrahimi  路  3Comments

jeffsugden picture jeffsugden  路  4Comments

odinserj picture odinserj  路  4Comments

cottsak picture cottsak  路  3Comments

nigel-dewar picture nigel-dewar  路  3Comments