HangFire recurring task data

I am encoding an MVC 5 web application and using HangFire for repetitive tasks.

If I have a monthly recurring task, how can I get the next runtime value?

Here is my code for the repetitive task:

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

I can get the job data as follows:

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

However, I am not sure what to do next.

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

Thanks in advance.

+3
source share
1 answer

You're close I'm not sure if there is a better or more direct way to get this data, but the way the Hangfire dashboard does is to use an extension method (add using Hangfire.Storage; to your import) 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 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:

  • It returns all duplicate jobs, and you need to select the appropriate entry from the result
  • When you first create a task, NextExecution time is not yet available (it will be null). I believe that the server, when it connects, periodically checks the recurring tasks that should be scheduled, and does this; they are not displayed immediately after creation using RecurringJob.AddOrUpdate(...) or other similar methods. If you need to get the NextExecution value right after creation, I'm not sure what you can do. Ultimately, it will be populated.
+12
source

Source: https://habr.com/ru/post/1258872/


All Articles