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.
There are multiple places where parts of this are answered, but for me to get this working completely, I had to do TWO things:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With