Andy
Andy

Reputation: 46

hangfire recurring job: prevent parallel execution of the same job

I have implemented a recurring job which needs to run every minute. Now and then the job has a hickup as an API-Call is involved which can take a bit longer to response. So that the job is enqued a second time, even though it wasn't finished in the previous run.

My Question: How do I prevent a Hangfire job to run if another instance of the same job is already running?

Thank you!

Upvotes: 1

Views: 1676

Answers (1)

rahat
rahat

Reputation: 1

To prevent the same recurring job from running simultaneously in Hangfire, you can use distributed locks. Hangfire already provides built-in mechanisms for ensuring that recurring jobs don't overlap, but you can explicitly enforce this behavior by wrapping the job logic in a distributed lock.

public class MyRecurringJob
{
    [DisableConcurrentExecution(timeoutInSeconds: 300)] // Prevent overlapping for 5 minutes
    public void Execute()
    {
        
    }
}

Upvotes: 0

Related Questions