HangFire定期任务数据 [英] HangFire recurring task data

查看:438
本文介绍了HangFire定期任务数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编码MVC 5 Internet应用程序,并使用HangFire执行重复任务.

I am coding a MVC 5 internet application and am using HangFire for recurring tasks.

如果我有一个每月定期任务,如何获得下一次执行时间的值?

If I have a Monthly recurring task, how can I get the value of the next execution time?

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

Here is my code for the recurring task:

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

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

I can retrieve the job data as follows:

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

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

However, I am not sure as to what to do next.

能否获得重复执行任务的下一次执行时间?

Is it possible to get the next execution time of a recurring task?

谢谢.

推荐答案

您已经关闭.我不确定是否有更好或更直接的方法来获取这些详细信息,但是Hangfire仪表板执行此操作的方法是使用称为GetRecurringJobs()的扩展方法(将using Hangfire.Storage;添加到您的导入中):

You're close. I'm not sure if there is a better or otherwise more direct way to get these details, but the way the Hangfire Dashboard does it is to use an extension method (add using Hangfire.Storage; to your imports) called 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值,我不确定您能做什么.不过,最终它将被填充.
  1. It returns all recurring jobs, and you'll need select the appropriate record out of the result
  2. When you first create the job, the NextExecution time is not available yet (it will be null). I believe the server, once one connects, periodically checks for recurring tasks that need to be scheduled and does so; they do not appear to be immediately scheduled upon creation using RecurringJob.AddOrUpdate(...) or other such similar methods. If you need to get that NextExecution value immediately after creation, I'm not sure what you can do. It will eventually be populated, though.

这篇关于HangFire定期任务数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆