We are leaving Russia. We are against aggression and war with Ukraine. It is a tragedy for our nations, it is a nightmare

Newbie; How to get list of currently processing jobs and stop one

Hi,
I am trying to get the hang of using and working with Hangfire.
The dashboard doesn’t give me a way to stop an executing/processing job so I would like to create an ASP.NET Razor Page that will list all the running jobs and have a link I can click that will stop that job. (or at least send it a cancellation token)

I have trawled through the discussions and can’t see any way to list the jobs … its probably obvious to all the experts here so maybe you could just throw me a bone here?

I have seen this page in the docs… but I don’t understand how that allows me to stop a specific running job… don’t I need some sort of id for the job instance? Maybe someone could help me understand?

Thanks

If I’m understanding your question correctly, I think the Hangfire dashboard does allow stopping a job that is processing (or, more correctly, “requesting” that a job be stopped).

If you have a job that takes some time and is currently processing/running, in the Hangfire dashboard page, select the “Jobs” item in the top navigation bar. This shows entries for Enqueued, Scheduled, Processing, etc. Click the “Processing” entry, and you should see your currently processing job (or jobs). Check the checkbox at the beginning of the line showing the job that you want to stop, and then click the “Delete selected” button at the top. You will be prompted, asking whether you want to delete the job - click OK.

This will stop/cancel the executing job in Hangfire’s queue and move it from the “Processing” list to the “Deleted” list in the “Jobs” page. Note that if this was a recurring job, it will still be in the list of recurring jobs - the “delete” operation here only deletes (cancels) the single instance of the job from currently processing; it does not delete the job from the recurring list.

However, this does NOT actually stop the code that is running within the job. For that, you will need to use a cancellation token, and your code needs to be such that you have intermediate places where you can periodically check the state of the cancellation token.

This is briefly explained at: Using cancellation tokens — Hangfire Documentation and the following is my very cursory understanding of one way to use this process.

First, you need to add a CancellationToken parameter to your method that you are scheduling, such as:
public static string LongRunningJob(CancellationToken token)

Second, when you enqueue this job (either singly or recurring), pass a Null cancellation token to it, such as:
“=> LongRunningJob(CancellationToken.None)”
Hangfire will automatically replace this Null token with its own unique token for each instance of a job.

Third, at intervals within your code for the called method, you need to check the state of the cancellation token. For example, if you are performing database operations, you can check after each record update; if you are sending a batch of emails, you can check after each email, etc. Check if the job has been canceled with code like this:

if (token.IsCancellationRequested)
  {
   // Perform any cleanup required, or log the cancellation
   // Perhaps, keep track of the last item processed or similar
   return "CANCELED"; // Or similar
   }

Note: You can also (more simply) just cause/raise an exception in your long-running code if it has been cancelled - by periodically calling “token.ThrowIfCancellationRequested();”

Wow @philipd , thanks for taking the time to provide such a comprehensive answer.

Now that you have explained it, it makes sense. And its probably the right way for this to work. Especially for the ETL style jobs I am hoping to schedule. It would make sense to allow the job to shut itself down gracefully… end transactions or spit out a log message or something.

I’m still not clear on how to actually send the cancel message from code rather than from the dashboard. I know the dashboard has the Delete facility but my use case probably wouldn’t involve the dashboard at all really…

Just a simple canned list of job types and some way to start or stop them On Demand.

Some of the jobs might have fixed schedules (like start after 10pm tonight) or start after 10pm on friday and and stop at 6am Monday morning.
But that’d be phase two really…

Phase three would be to provide alerts about certain error conditions … like a REST call returning a 500 status or a REST call taking more than a certain time to return a response… so I suppose I’d be into looking at listeners etc.

Anyway, thanks very much for your explanation, its very helpful. :+1: