Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop Hangfire job from enqueuing if already enqueued

Tags:

c#

hangfire

Is there a simple way of stopping a hangfire.io job from enqueuing if one is already enqueued?

Looking at the jobfilterattribute, nothing stands out as how to get the state of anything on the server. Can I use the connection objects and query the store?

Thanks

like image 384
William Avatar asked Jun 13 '16 14:06

William


People also ask

How do you stop a Hangfire recurring job?

Each iteration of your job would check that value, and if it indicates a Stop Request, then exit the loop. You can achieve this by using IMonitorinApi and fetch jobs that are processing. Then you can delete them by using IBackgroundJobClient. You need to pass in a cancellationtoken though.

How can we stop Hangfire jobs?

There is no “Cancel” button for jobs in the built-in dashboard, only a “Delete” button. Using that Delete button will remove the Job from the running jobs in the built-in dashboard (near) instantly.

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.

How do I schedule a Hangfire job?

Schedule( () => Console. WriteLine("Hello, world"), TimeSpan. FromDays(1)); Hangfire Server periodically checks the schedule to enqueue scheduled jobs to their queues, allowing workers to execute them.


1 Answers

Have a look at the following gist by the library owner https://gist.github.com/odinserj/a8332a3f486773baa009

This should prevent the same job from being en-queued more than once by querying the fingerprint.

You can activate it per background job by decorating the method with the attribute [DisableMultipleQueuedItemsFilter].

Or you can enable it globally GlobalJobFilters.Filters.Add(new DisableMultipleQueuedItemsFilter());

like image 167
tjackadams Avatar answered Nov 15 '22 10:11

tjackadams