Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if hangfire recurring job is already running

I have an web app that is leveraging Hangfire for background tasks and jobs.

I have a recurring job where 2 instances of this job cannot be executing at any one time. Lets say the job takes longer to execute.

I would like to check if the job is currently running, and if so, do not re run.

I have looked around and cannot find any information on this.

like image 916
Wesley Skeen Avatar asked Jan 05 '18 10:01

Wesley Skeen


1 Answers

There are multiple places where parts of this are answered, but for me to get this working completely, I had to do TWO things:

  1. Know the attributes
  2. Put them on the Interface, not the implementation method

So if you are using interfaces (for example with dependency injection) to kick off the Hangfire job put these on the Interface definition

[DisableConcurrentExecution(10)]
[AutomaticRetry(Attempts = 0, LogEvents = false, OnAttemptsExceeded = AttemptsExceededAction.Delete)]

If you don't do the AutomaticRetry attribute this way, the default is that the job will get added to the Scheduled job list to run when the first one finishes.

This works the way I wanted it to:

Run the job every 5 minutes. If during the next 5 minute interval, the previous job is still running, do not queue a new instance of it.

If you put these on the method that implements the interface, it does nothing.

like image 187
TechSavvySam Avatar answered Oct 12 '22 09:10

TechSavvySam