2

I'm trying to migrate an existing ABP project backed by Quartz.NET to Hangfire.

I have a recurring job and I need to get the scheduled fire time inside the job. Note that I do not need the real execution time, but rather the expected one. Let’s suppose that I have a recurring job scheduled for every Monday at 10:00 am; When the job runs I need to obtain a DateTime indicating the current Monday at 10 am.

Am I missing some PerformContext or JobData property?

public class TestJob : BackgroundJob<PerformContext>, ITransientDependency
{
    private readonly IRepository<User, long> _usersRepository;

    public TestJob(IRepository<User, long> usersRepository)
    {
        _usersRepository = usersRepository;
    }

    public override void Execute(PerformContext context)
    {
        var jobData = context.Connection.GetJobData(context.BackgroundJob.Id);

        // How to get planned execution time for this job run?
    }
}

If my recurring job was scheduled to run every hour and my server is down for 5 hours, I would need indeed to run all 5 missing runs, and for each of them I would expect to reference the right planned date time. Do you have any suggestions on how to accomplish this?

The same information can be found in Quartz under the IJobExecutionContext.ScheduledFireTimeUtc

Hangfire Discuss Link

1 Answers1

0

I would use delayed job which may give more control. After the execution of job, put job itself in a delayed queue with a caculated delay interval.

Moreover, Hangfire monitor API may be a way to reach this too.

Zen
  • 621
  • 4
  • 11
  • It could be an idea, but what about resilency? What if the job dies before it schedules the next one? I took a look at the monitoring API, but I could not find anything to help me achieving this. Can you kindly provide some pointers? – Giacomo De Liberali Nov 09 '20 at 12:01
  • Accroding to the source code, they do expose `NextExecution`. https://github.com/HangfireIO/Hangfire/blob/master/src/Hangfire.Core/Storage/RecurringJobDto.cs#L29 . As for job failure, Hangfire provide retry features to try its best to complete jobs, you can use `continueWith` to register failure callback though. – Zen Nov 09 '20 at 13:30
  • They do expose the `NextExecution` property, but I'd rather prefer to avoid calculations inside the job run as it can get tricky when running in multiple instances. Moreover it wouldn't work when missed jobs are triggered manually after a server restart. For the moment using a scheduled job that enqueues itself seems the safer solution (explicitly passing the right fire datetime). – Giacomo De Liberali Nov 09 '20 at 14:02