Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set an "on demand" only job in HangFire

Tags:

c#

hangfire

In Hangfire, I have successfully set recurring jobs and am able to trigger manually if I want to, thanks to the Web UI and its "trigger" button.

RecurringJob.AddOrUpdate(..);

enter image description here

But I'm willing to set a job that is never fired automatically. Only on demand from the WebUi. Think of it as a set of maintenance task that are triggered only when needed. Manually.

I was thinking of adding a non-reccuring Job in the await state, but was not able to (and it sounds wrong).

Are "On demand only" jobs possible with Hangfire?

like image 598
Askolein Avatar asked Jun 02 '16 08:06

Askolein


People also ask

How do I create a recurring job on hangfire?

Recurring job registration is almost as simple as background job registration – you need to write a single line of code, but you also need to specify an identifier you can use to refer to your job later. The call to AddOrUpdate method will create a new recurring job or update existing job with the same identifier.

How do I stop a recurring job on hangfire?

You would use a similar method if you'd like to stop the job prematurely via user interaction. When the user requests it to stop, you set a value in your database (or wherever you want to store the Stop Request). Each iteration of your job would check that value, and if it indicates a Stop Request, then exit the loop.

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.


2 Answers

I used this (hangfire 1.7 on .net 4.6.2)

RecurringJob.AddOrUpdate(() => ..., "0 0 31 2 0");

It shows up in the dashboard as:

Next execution N/A

like image 114
Edwin Avatar answered Sep 20 '22 08:09

Edwin


I use this CRON expression as Never: 0 0 29 2/12000 WED At midnight, on a 29th of Feb that happens to be a Wednesday, every century.

As @youen found out, this expression yields a later date. "0 0 29 2/12000 MON"

like image 32
Hylaean Avatar answered Sep 20 '22 08:09

Hylaean