HangFire recurring task data

后端 未结 1 1177
梦谈多话
梦谈多话 2021-01-19 19:30

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 n

相关标签:
1条回答
  • 2021-01-19 19:41

    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);
       }
    }
    

    There are two catches:

    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.
    0 讨论(0)
提交回复
热议问题