Set an "on demand" only job in HangFire
Asked Answered
O

5

20

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?

Oliguria answered 2/6, 2016 at 8:59 Comment(4)
What I do is have a controller for triggering jobs. Make an admin page with a button that calls an action that enqueues the job.Puga
@Rob The "Feb 31" solution doesn't work, unfortunately. Hangfire throws an exception apparently while trying to find the next actual calendar date that matches the expression (and ends up running past year 9999 while looking for such a date). (As of Hangfire v1.6.4)Inexistent
@JonSchneider That's unfortunate! I don't believe I tested it, merely believed it might be worth a short since it uses cron tab notation. I'll remove the comment so as to not lead people astray in the futureHangup
@Oliguria , in my case trigger now button triggering same job 2 times randomly, do you have any idea on this?Cosset
G
24

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

Giorgi answered 3/5, 2019 at 13:13 Comment(0)
A
12

In newer versions of Hangfire, you can simply use this:

RecurringJob.AddOrUpdate(() => ..., Cron.Never());
Anselma answered 8/8, 2022 at 13:43 Comment(1)
Cron.Never() return the 31st of February return Yearly(2, 31); and thus will never be executed by hangfire. It's also convenient to call this method in order to avoid magic strings.Guanajuato
A
9

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"

Adel answered 6/4, 2017 at 13:0 Comment(2)
Actually, this one "0 0 29 2/12000 MON" will occur four years later (in year 2044). One could argue that few systems made today will still run at this date. Though those that will might need a fix by then, so worth thinking how critical it is to unwillingly run the task at that date (because you can't decently assume that someone will remember to update it in time before it happens). That's going to be the famous "monday february 29" bug, and people proficient in the antique C# language at that time will be paid fortunes to fix it.Equable
By the way, it seems hangfire ignores the /12000 part. It just plans to trigger on the indicated date mon feb 29.Equable
T
6

Manual or "on demand" have not yet been implemented in the main trunk. However you can use this fork to have that option.

Currently this is marked as Work in Progress in Hangfire's repo and you can use it as this:

BackgroundJob.Stash(() => Console.WriteLine("STASHING"));

A different approach mentioned in one of the comments is to have somewhere in your site a button that enqueues the job:

BackgroundJob.Enqueue(() => Console.WriteLine("Simple!"));

EDIT: 2017/12/28:

Apparently the previous fork wont be merged back since this extension already supports to manually queue jobs:

Hangfire.Core.Dashboard.Management

enter image description here

Thyroid answered 30/5, 2017 at 17:32 Comment(0)
A
3

Yes, on demand jobs are entirely possible with hangfire. You can trigger fire-and-forget jobs using the following:

BackgroundJob.Enqueue(
() => Console.WriteLine("Simple!"));

This however won't appear in the hangfire dashboard, but you can use the above snippet in your code (Perhaps after a button click?)

Aberration answered 7/7, 2016 at 11:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.