Changing CronTime in RecurringJob causes execution

Tags: #<Tag:0x00007f5500a2a060>

We are using Hangfire & HangFire.SqlServer 1.6.5 and encountered an interesting bug/feature that caused us some trouble since last change in Daylight Saving Time.

Our hangfire events come from user input and are stored in UTC times based on their localization. Most events are on a daily basis with CronTime (0 1 * * *), after Daylight Saving Time changed these events were all updated to (0 2 * * *) by using the following method:

RecurringJob.AddOrUpdate(<id>, () => <method>, <crontime>);

We expected the event would not run till the next occurence of (0 2 * * *), but unfortunately these events were all triggered immediately after the crontime was updated. Do events always trigger when crontimes get updated and how could we disable this functionality?

Hi, @Bonnoj.

Recurring jobs are never triggered when cronExpression is updated. I think your problem related to this issue https://github.com/HangfireIO/Hangfire/issues/567

It’s known problem if your cron jobs should be triggered at 2:00 a.m or 3:00 a.m they could be triggered unpredictable during Daylight saving time.
Related links:


http://blog.endpoint.com/2013/04/avoid-200-and-300-am-cron-jobs.html

Hi, thanks for the reply!

Unfortunately the issue you linked is not related to our issue, Daylight Saving Time was an indicator of the underlying issue it seems. I am able to reproduce the issue today by updating a recurring job manually.

Planned crontime (Should fire each day at UTC 01:00):

(0 1 * * * )

Updated to (Should fire each day at UTC 02:00):

(0 2 * * *)

Did the job update around 15:00 UTC and it causes an immediate execution of the job. The expected result would be a planned execution on the next occurrence 02:00 UTC.

I have a feeling the issue arises because the LastExecution lies outside of the new time:

(0 1 * * *) --> is from 10-Nov 01:00 to 11-Nov 01:00, last execution is 10-Nov 01:01

Updated to:

(0 2 * * *) --> is from 10-Nov 02:00 to 11-Nov 02:00, last execution is 10-Nov 01:01 (Notice LastExecution time being outside range of cronschedule).

Is this considered as the preferred behavior or would this be an issue?
It makes sense that a job gets executed immediately when its outside the scheduled time, but in my opinion this behavior should not trigger when a job gets updated by the AddOrUpdate method.

@Bonnoj I reproduced the bug and created issue on github

@aidmsu We also have experienced this same issue using Hangfire 1.6.6 and agree with your assessment, that “It makes sense that a job gets executed immediately when its outside the scheduled time”. However, the job should not get triggered to run when the job is updated by the AddOrUpdate method. I would also conclude this is a possible bug in the AddOrUpdate method.

Hi @aidmsu,

I think I have a similar issue here.
(Using Hangfire.Core and Hangfire.SqlServer 1.6.17)
I start with an empty hangfire database.
The HangfireServer runs in a windows Service (running in console window for test purposes).
The server Gathers some concurring Jobs and adds them like so:
RecurringJob.AddOrUpdate(job.Name, () => job.Execute(), cronExpression);

With a cron expression like this:"* 19 */1 * *" I expect the job to run at 19:00
So when I start the server at 14:00 the job should not run.
However, it starts right away.
I hope its me that’s doing something wrong…

Regards
Frans

Beware of time zones. Your cron expression is actually 19:00 UTC, which may be different from your local time.

I’ve tried a couple of different times. I am in timezone GMT+1 (If I’m not mistaken, also UTC + 1) so I reckon a couple of hours ahead should do the trick.
However, the job always starts right away…

Grtz
Frans

Found it.
Problem in front of computer :face_with_raised_eyebrow:
It turned out that I Enqueud the job aswell…
:flushed: