19

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?

Askolein
  • 3,250
  • 3
  • 28
  • 40
  • 3
    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. – aethercowboy Jun 08 '16 at 19:32
  • 1
    @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) – Jon Schneider Feb 09 '17 at 18:55
  • 1
    @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 future – Rob Feb 10 '17 at 04:35
  • @Askolein , in my case trigger now button triggering same job 2 times randomly, do you have any idea on this? – Dreamer Oct 14 '22 at 07:35

5 Answers5

23

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

Edwin
  • 733
  • 8
  • 20
8

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"

Hylaean
  • 1,237
  • 13
  • 19
  • 3
    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. – youen Apr 17 '18 at 08:13
  • By the way, it seems hangfire ignores the `/12000` part. It just plans to trigger on the indicated date `mon feb 29`. – youen Apr 17 '18 at 08:27
7

In newer Versions of Hangfire you can simply use this:

RecurringJob.AddOrUpdate(() => ..., Cron.Never());

I know thats basically the same soultion stated above, but stackoverflow won't let me comment since my reputation is yet still at nothing...

Canabale
  • 157
  • 1
  • 7
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

emmanuel
  • 765
  • 8
  • 15
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?)

Dan
  • 533
  • 2
  • 6
  • 21