HangFire 循环任务数据 [英] HangFire recurring task data

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

问题描述

我正在编写一个 MVC 5 互联网应用程序,并使用 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 仪表板的做法是使用扩展方法(将 using Hangfire.Storage; 添加到您的导入)称为 GetRecurringJobs():

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天全站免登陆