How do I schedule a job to be run at a specific date in Hangfire

拜拜、爱过 提交于 2021-01-27 07:22:57

问题


Hangfire.io supports making a CRON-like scheduling of recurring jobs. But how do I specify, that a specific job should be run once, at a specific date/time, e.g. that a job should be run June 4th 2016, at 16:22 - and only at that specific point in time?

A similar way to ask the same question could be: how large a subset of the CRON expression described here, is supported by Hangfire? (The described CRON expression supports a "Year"-field which could be used).

Also, do you think Hangfire is the best choice to schedule one-off batch jobs in the first place, provided that I use Hangfire for job processing?


回答1:


You can use BackgroundJob.Schedule(Expression> methodCall, DateTimeOffsetdt) method.

BackgroundJob.Schedule(methodCall, enqueueAt);



回答2:


Cron expression with Year is not supported by Hangfire.

To run a job at specific point in time, use following schedule method overload from BackgroundJob class.

public static string Schedule([InstantHandle] Expression<Action> methodCall, DateTimeOffset enqueueAt);

BackgroundJob.Schedule(() => Console.Write("test"), new DateTime(2016, 6, 4, 16, 22, 0));



回答3:


In one of my application we schedule a job for only once run in a particular date time. Look into below code

  public string Schedule(Expression<Action> methodToCall, DateTimeOffset enqueueAt)
    {
        return BackgroundJob.Schedule(methodToCall, enqueueAt);
    }

Where enqueueAt is the date time when you want to run the job.



来源:https://stackoverflow.com/questions/35698361/how-do-i-schedule-a-job-to-be-run-at-a-specific-date-in-hangfire

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