Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hangfire - Prevent multiples of the same job being enqueued

Scenario:

Job 1 is scheduled to run every 5 minutes, and takes ~1 minute to complete.

A lot of work piles up and Job 1 takes 15 minutes to run.

There are now three Job 1's being processed concurrently - I don't want this.


How do I prevent Job 1 being added to the queue again if it is already there?

Is there Hangfire setting, or do I need to poll job statuses manually?

like image 405
Jaywaa Avatar asked Jul 18 '17 10:07

Jaywaa


People also ask

What is the use of Hangfire?

Hangfire is an open-source framework that can be used to perform background processing in . Net and . Net Core applications. It is mainly used to perform background tasks such as batch/email notification, batch import of files, video/image processing, database maintaining, file purging, etc.

How do you stop a Hangfire job?

If anyone here is familiar with hangfire how can I stop the process through task manager? Then call <server>. WaitForShutdown() to wait until it's finished. This should allow any tasks currently processing to finish.

What can be used to create complex Hangfire workflows?

Batch jobs will help to build more complex workflows with Hangfire. They will give you the power of parallel processing and continuations, you can look at this code snippet: BatchJob . Create(x => { x.

How do you trigger a Hangfire manually?

The idea is, on a loading page, there is an email input field. Once the user writes his email and clicks the Get Email button, a background job should trigger. It will check if the transaction is complete, and once it is, it will send an email to the user.


1 Answers

You can use DisableConcurrentExecution attribute to prevent multiple executions of a method concurrently. Just put this attribute above your method -

[DisableConcurrentExecution(timeoutInSeconds: 10 * 60)]
public void Job1()
{
    // Metohd body
}
like image 97
Yogi Avatar answered Oct 19 '22 04:10

Yogi