Set an “on demand” only job in HangFire

女生的网名这么多〃 提交于 2019-12-06 19:19:34

问题


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(..);

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?


回答1:


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"




回答2:


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




回答3:


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




回答4:


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



来源:https://stackoverflow.com/questions/37587167/set-an-on-demand-only-job-in-hangfire

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!