HangFire 重复任务数据

2023-11-29

我正在编写 MVC 5 互联网应用程序并正在使用HangFire用于重复性任务。

如果我有每月重复任务,如何获取下一次执行时间的值?

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

RecurringJob.AddOrUpdate("AccountMonthlyActionExtendPaymentSubscription", () => accountService.AccountMonthlyActionExtendPaymentSubscription(), Cron.Monthly);

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

using (var connection = JobStorage.Current.GetConnection())
{
    var recurringJob = connection.GetJobData("AccountMonthlyActionExtendPaymentSubscription");
}

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

是否可以获取重复任务的下一次执行时间?

提前致谢。


你很接近了。我不确定是否有更好或更直接的方法来获取这些详细信息,但 Hangfire 仪表板的做法是使用扩展方法(添加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);
   }
}

有两个问题:

  1. 它返回所有重复作业,您需要从结果中选择适当的记录
  2. 当您第一次创建作业时,NextExecution时间尚不可用(将为空)。我相信服务器一旦连接,就会定期检查需要安排的重复任务并执行此操作;它们似乎不会在创建后立即使用RecurringJob.AddOrUpdate(...)或其他类似的方法。如果你需要得到那个NextExecution创建后立即值,我不确定你能做什么。不过,它最终会被填充。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

HangFire 重复任务数据 的相关文章

随机推荐