Recurring Jobs overlapping -- still no solution?

My goal is preserve the same “jobId” for periodic execution AND make sure tasks of the same “jobId” never overlap. So we can have a nice persistent looking dashboard and don’t crash.

The only HangFire job type that allows specifying “jobId” is recurring job and those are “fire-and-forget”, in other words – if a 1 minute interval job takes 2 minutes to complete than we end up with a growing queue of job executions (and eventually crash) as the recurring job runs before it’s previous cycle completes.

[DisableConcurrentExecution] attribute can prevent overlap but it doesn’t discriminate which “jobId” tries to execute – no job but one gets into the method and we severely limit the throughput.

Finally, the trick of re-enqueuing as BackgroundJob.Schedule. This could work – BUT there’s no way to specify the “jobId” so now DashBoard is much less useful.

It’s a real deal breaker for us, sadly. Is there some sort of workaround or an extension out there to help?

Hi,

I created my own filter based on DisableConcurrentExecution attribute based on the fact that each recurring job has a unique custom string id (the one you send in RecurringJob.AddOrUpdate). You can get that id from the filter context in the OnPerforming method:

var recurringJobId = filterContext.GetJobParameter<string>("RecurringJobId");
var distributedLock = filterContext.Connection.AcquireDistributedLock(recurringJobId, timeout); // Timeout from constructor
filterContext.Items["DistributedLock"] = distributedLock;

And then, in the OnPerformed method you call:

var distributedLock = (IDisposable)filterContext.Items["DistributedLock"];
distributedLock.Dispose();

Sorry I cannot share my full filter as I have some custom logic there for my own stuff but the base should be what I pasted above.