-1

I have a reccuring job in Hangfire which is triggered by CronTemplate. I need to get next execution time during the job execution.

I came up with an idea to inject CronTemplate into the job and calculate that date using some of existing libraries based on the template, but I have some design concerns about this solution and I think a solution based on some kind of JobExecutionContext would be better, but so far I couldn't find any way to get it.

Does something similar exist in Hangfire? If so, how can I access it?

Any idea would be appreciated.

Alex Riabov
  • 8,655
  • 5
  • 47
  • 48

1 Answers1

3

Alex here is some pseudo code that might just answer your question.

private DateTime? GetNextExecutionTime(MethodInfo methodInfo)
{
    try
    {
        var job = JobStorage.Current.GetConnection().GetRecurringJobs().Single(x => x.Job.Method == methodInfo);
        return job == null ? null : job.NextExecution;
    }
    catch (Exception)
    {
        return null;
    }
}
Lauren Rutledge
  • 1,195
  • 5
  • 18
  • 27
Pieter Alberts
  • 829
  • 1
  • 7
  • 23
  • 1
    Thank you for your suggestion, it works. I also modified it to search by `JobId .Single(x => x.Id == jobId);` – Alex Riabov Aug 08 '18 at 19:31