Hangfire icon indicating copy to clipboard operation
Hangfire copied to clipboard

Not launching a scheduled job if the previous one didn't complete

Open ollie10 opened this issue 2 years ago • 2 comments

Good Morning everyone,

I have a simple use case that I couldn't solve, and I'm sure there should be a way to do it. I have looked into DisableConcurrentExecution, but it seems not doing what I expect. Besides, this I have read that not always works especially with mongo as storage provider.

The case is simple: I have a recurring Job 1 scheduled each X time (let's say 1 day). This job launches thousands of other jobs and sometimes the time it takes to execute all the jobs tree, it is more than 1 day.

What I would like to achieve is that if the Job 1 tree didn't finish, hangfire skips completely the next execution and wait for another time window, or two depending on when the Job 1 finishes. If for example it takes 3 days, the Job 1 scheduled for the second day and third day should be skipped, avoiding creating a perpetual queue, where more and more jobs can be added than the actual process can handle.

Is there a way to do something like this? I'm sure yes Many thanks for the help

ollie10 avatar Jun 08 '22 21:06 ollie10

Please try a background job filter from this gist – https://gist.github.com/odinserj/a6ad7ba6686076c9b9b2e03fcf6bf74e

odinserj avatar Jun 10 '22 04:06 odinserj

Hello @odinserj, this seems doing the trick even it seems jobs are only "parked" and executed when the other execution finishes. Since I'm running long time jobs I'm not sure about this but for now this did the trick. I just needed to changed the serialization functions of your gist because they are marked as obsolete.

Many thanks for now and stay strong with the situation

ollie10 avatar Jul 05 '22 18:07 ollie10

We this feature as well

yahorsi avatar Mar 12 '23 11:03 yahorsi