GH-36 Changes for cron syntax compat (dayOfWeek 0-6 and no-seconds syntax support) #41

Merged
merged 3 commits into from Aug 7, 2012
View
@@ -7,7 +7,7 @@ Originally this project was a NodeJS fork of [James Padolsey's][jamespadolsey] [
After [Craig Condon][crcn] made some updates and changes to the code base this has evolved to something that has a bit of both. The cron syntax parsing is mostly James' while using timeout instead of interval is Craig's.
-Additionally, this library goes beyond the basic cron syntax and allows you to supply a Date object. This will be used as the trigger for your callback. Cron syntax is still an acceptable CronTime format.
+Additionally, this library goes beyond the basic cron syntax and allows you to supply a Date object. This will be used as the trigger for your callback. Cron syntax is still an acceptable CronTime format. Although the Cron patterns suported here extend on the standard Unix format to support seconds digits, leaving it off will default to 0 and match the Unix behavior.
Usage (basic cron usage):
==========
@@ -25,13 +25,13 @@ Available Cron patterns:
Ranges. E.g. 1-3,5
Steps. E.g. */2
-[Read up on cron patterns here](http://help.sap.com/saphelp_xmii120/helpdata/en/44/89a17188cc6fb5e10000000a155369/content.htm).
+[Read up on cron patterns here](http://crontab.org).
Another cron example
==========
var cronJob = require('cron').CronJob;
- var job = new cronJob('00 30 11 * * 2-6', function(){
+ var job = new cronJob('00 30 11 * * 1-5', function(){
// Runs every weekday (Monday through Friday)
// at 11:30:00 AM. It does not run on Saturday
// or Sunday.
@@ -60,7 +60,7 @@ For good measure
var cronJob = require('cron').CronJob;
var job = new cronJob({
- cronTime: '00 30 11 * * 2-6',
+ cronTime: '00 30 11 * * 1-5',
onTick: function() {
// Runs every weekday (Monday through Friday)
// at 11:30:00 AM. It does not run on Saturday
View
@@ -26,10 +26,11 @@ function CronTime(source, zone) {
};
CronTime.map = ['second', 'minute', 'hour', 'dayOfMonth', 'month', 'dayOfWeek'];
-CronTime.constraints = [ [0, 59], [0, 59], [0, 23], [1, 31], [0, 11], [1, 7] ];
+CronTime.constraints = [ [0, 59], [0, 59], [0, 23], [1, 31], [0, 11], [0, 6] ];
+CronTime.parseDefaults = [ '0', '*', '*', '*', '*', '*' ];
CronTime.aliases = {
jan:0, feb:1, mar:2, apr:3, may:4, jun:5, jul:6, aug:7, sep:8, oct:9, nov:10, dec:11,
- sun:1, mon:2, tue:3, wed:4, thu:5, fri:6, sat:7
+ sun:0, mon:1, tue:2, wed:3, thu:4, fri:5, sat:6
};
@@ -107,7 +108,7 @@ CronTime.prototype = {
continue;
}
- if (!(date.getDay()+1 in this.dayOfWeek)) {
+ if (!(date.getDay() in this.dayOfWeek)) {
date.setDate(date.getDate()+1);
date.setHours(0);
date.setMinutes(0);
@@ -179,11 +180,14 @@ CronTime.prototype = {
throw new Error('Unknown alias: ' + alias);
}),
split = source.replace(/^\s\s*|\s\s*$/g, '').split(/\s+/),
- cur, len = 6;
-
- while (len--) {
- cur = split[len] || '*';
- this._parseField(cur, CronTime.map[len], CronTime.constraints[len]);
+ cur, i = 0, len = CronTime.map.length;
+
+ for (; i < CronTime.map.length; i++) {
+ // If the split source string doesn't contain all digits,
+ // assume defaults for first n missing digits.
+ // This adds support for 5-digit standard cron syntax
+ cur = split[i - (len - split.length)] || CronTime.parseDefaults[i];
+ this._parseField(cur, CronTime.map[i], CronTime.constraints[i]);
}
},
View
@@ -34,6 +34,26 @@ module.exports = testCase({
assert.done();
}, 5250);
},
+ 'test standard cron no-seconds syntax doesnt send on seconds (* * * * *)': function(assert) {
+ assert.expect(0);
+ // Delay test from running at minute boundary
+ var prepDate = new Date();
+ if (prepDate.getSeconds() >= 55) {
+ setTimeout(testRun, 5000);
+ } else {
+ testRun();
+ }
+
+ function testRun() {
+ var c = new cron.CronJob('* * * * *', function() {
+ assert.ok(true);
+ }, null, true);
+ setTimeout(function() {
+ c.stop();
+ assert.done();
+ }, 5250);
+ }
+ },
'test every second for 5 seconds with oncomplete (* * * * * *)': function(assert) {
assert.expect(6);
var c = new cron.CronJob('* * * * * *', function(done) {
View
@@ -37,6 +37,21 @@ module.exports = testCase({
});
assert.done();
},
+ 'test no second digit doesnt throw, i.e. standard cron format (8 8 8 8 5)': function(assert) {
+ assert.expect(1);
+ assert.doesNotThrow(function() {
+ new cron.CronTime('* * * * *');
+ });
+ assert.done();
+ },
+ 'test no second digit defaults to 0, i.e. standard cron format (8 8 8 8 5)': function(assert) {
+ assert.expect(1);
+ var now = new Date();
+ var standard = new cron.CronTime('8 8 8 8 5');
+ var extended = new cron.CronTime('0 8 8 8 8 5');
+ assert.ok(standard._getNextDateFrom(now).getTime() === extended._getNextDateFrom(now).getTime());
+ assert.done();
+ },
'test hyphen (0-10 * * * * *)': function(assert) {
assert.expect(1);
assert.doesNotThrow(function() {