Set an “on demand” only job in HangFire

前端 未结 4 852
粉色の甜心
粉色の甜心 2021-02-13 17:18

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.A         


        
相关标签:
4条回答
  • 2021-02-13 17:41

    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

    0 讨论(0)
  • 2021-02-13 17:48

    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"

    0 讨论(0)
  • 2021-02-13 17:57

    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?)

    0 讨论(0)
  • 2021-02-13 18:05

    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

    0 讨论(0)
提交回复
热议问题