Cron jobs kicked off at wrong time for Daylight Savings Time
Sunday morning the Cron job that we had set up to go off at 9am and 9pm every day decided to start kicking off jobs at 1 am local time. It ran as many times as it could from 09:00 to 09:01 UTC. This job had worked flawlessly up until that day.
The times are Pacific time. So they were 17:00 UTC and 05:00 UTC. And on Sunday morning they kicked off at 09:00 UTC. So perhaps node-cron got confused about the timezone? Or I should have set the timezone specifically or used UTC instead.
Here's the code that sets it up:
var job = new cronJob({
cronTime: '00 00 09,21 * * *',
onTick: function() {
logMessage('cron job running');
// Runs every weekday at 9:00 AM and 9:00 PM
runPerformanceTests();
},
start: true
});
Huh. Strange. Thanks.
I'll take a look tonight.
Indeed, going through the code right now to see if I can figure it out. Working on my JavaScript skills, or lack thereof. :)
After looking at the code, one additional bit of info that might help in debugging is that I'm not using CronDate.
Ok, found it. When we add time such that we get into the "dead zone" time wise (2:00am-2:59:59 doesn't exist in local time) we cannot add hours, minutes, seconds anymore.
Here's some repro code: You can turn on the debug to see what's happening in the code to get the next date.
var debug = false,
chronInfo = { source: '00 00 09,21 * * *',
zone: undefined,
second: { '0': true },
minute: { '0': true },
hour:
{ '9': true, '21': true },
dayOfWeek:
{ '0': true, '1': true, '2': true, '3': true, '4': true, '5': true, '6': true },
dayOfMonth:
{ '1': true, '2': true, '3': true, '4': true, '5': true, '6': true, '7': true, '8': true, '9': true, '10': true, '11': true, '12': true, '13': true, '14': true, '15': true, '16': true, '17': true, '18': true, '19': true, '20': true, '21': true, '22': true, '23': true, '24': true, '25': true, '26': true, '27': true, '28': true, '29': true, '30': true, '31': true },
month:
{ '0': true, '1': true, '2': true, '3': true, '4': true, '5': true, '6': true, '7': true, '8': true, '9': true, '10': true, '11': true } };
function consolelog(info) {
if (debug) console.log(info);
}
function _getNextDateFrom(start) {
consolelog('_getNextDateFrom: ' + start);
var date = new Date(start);
if (chronInfo.zone && date.setTimezone)
date.setTimezone(start.getTimezone());
//sanity check
var i = 1000;
while(--i) {
var diff = date - start;
consolelog('_getNextDateFrom diff: ' + diff);
if (!(date.getMonth() in chronInfo.month)) {
date.setMonth(date.getMonth()+1);
date.setDate(1);
date.setHours(0);
date.setMinutes(0);
consolelog('_getNextDateFrom month: ' + date);
continue;
}
if (!(date.getDate() in chronInfo.dayOfMonth)) {
date.setDate(date.getDate()+1);
date.setHours(0);
date.setMinutes(0);
consolelog('_getNextDateFrom dayofmonth: ' + date);
continue;
}
if (!(date.getDay() in chronInfo.dayOfWeek)) {
date.setDate(date.getDate()+1);
date.setHours(0);
date.setMinutes(0);
consolelog('_getNextDateFrom dayofweek: ' + date);
continue;
}
if (!(date.getHours() in chronInfo.hour)) {
date.setHours(date.getHours() == 23 && diff > 24*60*60*1000 ? 0 : date.getHours()+1);
date.setMinutes(0);
consolelog('_getNextDateFrom hour: ' + date);
continue;
}
if (!(date.getMinutes() in chronInfo.minute)) {
date.setMinutes(date.getMinutes() == 59 && diff > 60*60*1000 ? 0 : date.getMinutes()+1);
date.setSeconds(0);
consolelog('_getNextDateFrom minute: ' + date);
continue;
}
if (!(date.getSeconds() in chronInfo.second)) {
date.setSeconds(date.getSeconds() == 59 && diff > 60*1000 ? 0 : date.getSeconds()+1);
consolelog('_getNextDateFrom seconds: ' + date);
continue;
}
break;
}
consolelog('_getNextDateFrom final: ' + date);
return date;
}
var dtPST = new Date(Date.parse("2013-03-09 21:00:01"));
console.log(dtPST);
var nextdt = _getNextDateFrom(dtPST);
console.log(nextdt);
var dtPST2 = new Date(Date.parse("2013-03-10 01:00:01"));
console.log(dtPST2);
var nextdt2 = _getNextDateFrom(dtPST2);
console.log(nextdt2);
The fix I have no idea about, I am a tester so I break things rather than fix things. ;D
I was able to reproduce this a few days ago but haven't had time to get into a fix. I'll post more when I do.
uuuuu, my systems just ran into the same bug tonight .....
would be very much appreciated if you would have time to fix this
Same here, all jobs stopped working tonight, I had to restart the app.
Strange, I didn't hit it last night. And I'm not aware of any time changes that happened that might cause the same issue I hit to happen.
So now the tester in me is curious. :) What are the Cron settings you're calling it with? Are you using the timeZone setting? What time zone are you in? Is it a long running node job that's been going for a while?
We are running NodeJS 0.8.22 on FreeBSD 9.0 in a local german timezone where the time changed from 1:59 am to 3:00 am.
All our cronjobs fired constantly from local 1:00am (thats midnight in GMT and one hour before the actual time change) for about 30 minutes, resulting in very high load and users receiving accidentally more than 1.000 mails :smile:. After that, all cronjobs stopped working completely. We are running simple cronjobs, no special timezone settings.
btw.: another app, running with process.env.TZ = 'GMT' on a similar server, didn't have this problem.
Ah, see that's my dumb US centric thinking. Didn't realize that other countries would change their time zones at a different date. LOL You hit the exact same issue I hit. Luckily for us it wasn't hitting users, it was just hitting some poor web service that we didn't own. And luckily it died after 1000 attempts to hit the service otherwise we might have gotten banned. :(
Yep, any server that is running GMT or a timezone that doesn't do daylight savings wouldn't hit it. So say if your server was in the US in Arizona where they keep the same time year around.
I'm thinking I might just switch to something simpler for our services and use that, didn't really need the abilities of a cronjob type thing but it seemed the simplest thing to use at the time.
same here in swissyland... cron jobs started to go mad at 2013-03-31 01:00:00
i used cron jobs with simple patterns, like 0 30 */4 * * * 0 30 23 * * * and they fired as fast as they could as far as i can reconstruct it ;)
the way i used your module: new cron.CronJob(pattern, function() { ... }
I'll see if I can carve out a little time today and/or tomorrow to take a look at this. Thanks for all the info!
So. I added some code, tests and a couple lib changes in 567e34449a8. I'll summarize below, but suffice to say, I doubt these will fix the problem you're seeing and now I'm not sure that I can correctly reproduce the issue (we'll get to this bit after).
What I found is that when using a realDate (new Date()) you can run into problems calling _getNextDateFrom() directly because it is trying to find the next date to fire at, based on pattern information. This isn't useful and isn't going to work. So, I've short circuited the function and return the date passed in here as that is the date you wanted to fire at. If this functionality doesn't make sense then please let me know. I'd like to know if I'm assuming too much.
If you are using a pattern, then things should function as they always have...find the next date/time value that matches the pattern after the Date supplied.
As for reproducibility, it seems to me that this is likely a DST issue. DST was March 10 in the US at 2am and March 31 in Europe at 2am. If I'm correct in thinking that this is the problem, it poses a slightly bigger change. The reason this poses a bigger change is that more abstractly, unix cron takes general time changes into consideration...not just DST. With node-cron, that isn't the case, once the timeout has been specified and set, it doesn't update internally. This could cause some issues with any time changes on the system.
I'll probably have to make some changes making this module function more like unix cron, updating jobs when the system date/time changes. I'll have to think a little more about this. I'm open to any thoughts you all may have on the subject. Also, please hit me over the head if my assumption on the DST bit is wrong (the bit about it jumping around where the timeout is already set (although I'm reasonably sure this is an issue anyway)).
Thanks!
To be clear, what I think is the issue is that once a time is set, it can be fired at seemingly random times when there is a time change on the system, until the timeout is reset. DST is just a basic manifestation of that.
Right, the issue is Daylight Savings Time (DST). I mentioned it here https://github.com/ncb000gt/node-cron/issues/56#issuecomment-14747264
But I didn't clearly state what the problem was. Sorry.
The problem is in how you are getting the next time based on the values set on the job. While you're fix looks better for those who set the time directly, now with the change to the while loop anyone who has a server running using the local time that uses DST will be stuck in an infinite loop trying to get the next time.
Here's why, when you try to get the next time you add a value to that time that ends up in the "Dead time" zone for that time zone if they respect DST. So for example if the time is 1AM Standard Time and you add one hour to that time you don't get 2 AM ST (or even DST) because there isn't such a time. What you get back again is 1AM Standard Time, which you then again add an hour to again and again. Before what would happen is that it would eventually end after 1000 tries and return 1AM, and it'd fire the job off, get the next time, 1AM, and so on.
This JS code demonstrates the issue (when your time zone is say Pacific). var dtPST = new Date(Date.parse("2013-03-10 01:00:00")); console.log(dtPST); //Sun Mar 10 2013 01:00:00 GMT-0800 (Pacific Standard Time) dtPST.setHours(dtPST.getHours()+1); console.log(dtPST); //Sun Mar 10 2013 01:00:00 GMT-0800 (Pacific Standard Time)
Eventually my node service died as it tried to hit a web service that limited the calls to 200 per day and returned error codes that my node service didn't expect to get back. Which I need to fix as well.
Here is some mention of how Cron handles this situation. Third section here: http://www.unix.com/tips-tutorials/35535-understanding-unix-timekeeping.html
Looking at your code I still can't figure out the best fix for how you determine the next time. Only part I can think of is when adding or setting a time if it equals the same time after the update then do something "special". What special is I'm not sure yet; perhaps adding some n time such that it returns a value that is now not the same time as before the time was added.
Some ways to test your code are to do something similar to like what I did in the comment I linked to. Call your code directly with a valid time (like say one hour before the DST time change) and Cron values set to see what, if anything, it returns. Right now with your changes the code will never return.
Another method to test would be to change the time zone of your computer and the time just before the time change (probably need to undo auto time updates for your OS) to a location/time that uses DST. And then run a Cron job where you'd hit this dead zone of time.
It looks like Mexico City will be the next to hit this issue. Sunday April 7th. http://www.timeanddate.com/worldclock/clockchange.html?n=155&year=2013
After that will be some countries in the Southern hemisphere around Sept/Oct.
Yep. I wasn't clear on what you meant by "dead time", but I am now. :)
An alternate way to solve this is to have code running in the background that constantly checks the time on the system and evaluates existing jobs to make sure the timeouts are appropriate, if not, adjust them and set job on the new timeout. I'm not sure this is the best strategy, I'll have to look closer at how cron deals with this, but I did happen to see the same link you mentioned last night.
Thanks for the clarification and help.
What's the status on this? Has this been fixed now?
Ugh. I haven't been able to get to this as I've been busy with other things. I'll update it when I get some time to work on it. I'd started on it but just never got enough time to finish it. Alternatively I'd be happy to accept a pull request for it too.
Thanks @ronkorving for checking in on it.
@ncb000gt may I remind you also? This library looks promising, but without a fix I can't use it. For your code to work you have to fix at least this line: https://github.com/ncb000gt/node-cron/blob/master/lib/cron.js#L59,
This is my idea Add a local variable to the cronjob: dst: "Winter || Summer" (or just a boolean). If the job hits the new DST-change, go and swap winter with summer value. If for example it's a DST-change-day and already winter, the code will continue normally. If it's still summer, set to winter and revert the hours.
Am I right if I conclude no jobs are affected when you run in the UTC time zone?