HangFire重复出现的任务数据

use*_*648 4 c# cron recurring asp.net-mvc-5 hangfire

我正在编写一个MVC 5互联网应用程序,并正在使用HangFire重复性任务.

如果我有一个月度重复任务,我怎样才能获得下一个执行时间的值?

这是我的重复任务的代码:

RecurringJob.AddOrUpdate("AccountMonthlyActionExtendPaymentSubscription", () => accountService.AccountMonthlyActionExtendPaymentSubscription(), Cron.Monthly);
Run Code Online (Sandbox Code Playgroud)

我可以按如下方式检索作业数据:

using (var connection = JobStorage.Current.GetConnection())
{
    var recurringJob = connection.GetJobData("AccountMonthlyActionExtendPaymentSubscription");
}
Run Code Online (Sandbox Code Playgroud)

但是,我不确定下一步该做什么.

是否有可能获得重复任务的下一个执行时间?

提前致谢.

Hac*_*ese 14

你很亲密 我不确定是否有更好或更直接的方式来获取这些细节,但Hangfire Dashboard的方式是使用一个名为的扩展方法(添加using Hangfire.Storage;到您的导入)GetRecurringJobs():

using (var connection = JobStorage.Current.GetConnection())
{
   var recurring = connection.GetRecurringJobs().FirstOrDefault(p => p.Id == "AccountMonthlyActionExtendPaymentSubscription");

   if (recurring == null)
   {
       // recurring job not found
       Console.WriteLine("Job has not been created yet.");
   }
   else if (!recurring.NextExecution.HasValue)
   {
       // server has not had a chance yet to schedule the job's next execution time, I think.
       Console.WriteLine("Job has not been scheduled yet. Check again later.");
   }
   else
   {
       Console.WriteLine("Job is scheduled to execute at {0}.", recurring.NextExecution);
   }
}
Run Code Online (Sandbox Code Playgroud)

有两个捕获:

  1. 它返回所有定期作业,您需要从结果中选择适当的记录
  2. 首次创建作业时,NextExecution时间尚不可用(它将为空).我相信一旦连接,服务器会定期检查需要安排的重复任务,并且这样做; 它们似乎在使用RecurringJob.AddOrUpdate(...)或其他类似方法创建时不会立即安排.如果你需要NextExecution在创建后立即获得该值,我不确定你能做什么.不过,它最终会被填充.