【问题标题】:HangFire recurring task dataHangFire 重复任务数据
【发布时间】:2015-02-11 06:07:36
【问题描述】:

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

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

这是我的重复任务代码:

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

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

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

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

是否可以获得循环任务的下一次执行时间?

提前致谢。

【问题讨论】:

    标签: c# cron asp.net-mvc-5 recurring hangfire


    【解决方案1】:

    你已经接近了。我不确定是否有更好或更直接的方法来获取这些详细信息,但 Hangfire 仪表板的方式是使用名为 GetRecurringJobs() 的扩展方法(将 using Hangfire.Storage; 添加到您的导入中):

    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 个 mvc 应用程序和 1 个控制台应用程序。在我的主要方法的控制台应用程序中,我有配置 Hangfire 服务器并有 1 个 while 循环,以便我的控制台应用程序继续运行。在控制台应用程序中我有 1 个执行后台处理的方法。我想在控制台应用程序中进行后台处理,因此我已经在控制台应用程序中配置了 HF 服务器。但是我没有得到如何从 mvc 排队我的控制台应用程序的执行方法
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-08
    • 1970-01-01
    • 1970-01-01
    • 2021-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多