Recurring job doesn't wait last triggered job to finish

Hi,

I have a recurring job configured to run every minute. But I need that, if it’s time to trigger the job again and the last one haven’t finished yet, Hangfire waits it to finish before trigger the new one.

There is some way to do that?

1 Like

I just found this project, and need to do something similar. I need a recurring process that runs every minute. However, I do not want more than one running at any one time. I’ve been playing around with this solution involving two jobs: a single recurring job (“Should_I_queue_worker”); and another job that runs immediately(“Worker”):

  1. Create a recurring job, “Should_I_queue_worker” that runs every minute.
  2. When processing, the “Should_I_queue_worker” job checks the processing queue to see if the second job, “Worker” is currently being processed. If no “Worker” jobs are currently being processed, Enqueue one. Otherwise, do nothing.
  3. The “Worker” job can run as long as necessary. As long as it shows in the processing queue, another will not be queued.

It certainly isn’t bulletproof, but in my testing it fulfills my needs - until this feature becomes available.

There are a couple of other methods:

  1. Use the DisableConcurrentExecution attribute which creates a distributed lock. The second job will be blocked until the first completes. Please note that this blocked job will “consume” a worker.

  2. Write a custom attribute / state change handler similar to the DisableConcurrentExecution attribute which upon failing to get a distributed lock after an instant timeout, sets the job to Succeeded. (Or failed)

See:

Thanks, yngndrw. I knew there had to be a more elegant (and robust) way to handle long-running recurring jobs. I’ll play around with your suggestions and post my results.

Hello Martin,
Did that solution work with your case?