cron
Advanced tools
Comparing version 1.0.1 to 1.0.2
239
lib/cron.js
@@ -1,19 +0,22 @@ | ||
var CronDate = Date; | ||
var CronDate; | ||
var exports; | ||
try { | ||
CronDate = require("time").Date; | ||
} catch(e) { | ||
//no time module...leave CronDate alone. :) | ||
exports = module.exports; | ||
} catch (e) { | ||
CronDate = Date; | ||
exports = null; | ||
} | ||
function CronTime(source, zone) { | ||
this.source = source; | ||
this.zone = zone; | ||
this.zone = zone; | ||
this.second = {}; | ||
this.minute = {}; | ||
this.hour = {}; | ||
this.dayOfWeek = {}; | ||
this.second = {}; | ||
this.minute = {}; | ||
this.hour = {}; | ||
this.dayOfWeek = {}; | ||
this.dayOfMonth = {}; | ||
this.month = {}; | ||
this.month = {}; | ||
@@ -26,10 +29,34 @@ if ((this.source instanceof Date) || (this.source instanceof CronDate)) { | ||
} | ||
}; | ||
} | ||
CronTime.map = ['second', 'minute', 'hour', 'dayOfMonth', 'month', 'dayOfWeek']; | ||
CronTime.constraints = [ [0, 59], [0, 59], [0, 23], [1, 31], [0, 11], [0, 6] ]; | ||
CronTime.parseDefaults = [ '0', '*', '*', '*', '*', '*' ]; | ||
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:0, mon:1, tue:2, wed:3, thu:4, fri:5, sat:6 | ||
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: 0, | ||
mon: 1, | ||
tue: 2, | ||
wed: 3, | ||
thu: 4, | ||
fri: 5, | ||
sat: 6 | ||
}; | ||
@@ -46,6 +73,6 @@ | ||
date.setTimezone(this.zone); | ||
//add 1 second so next time isn't now (can cause timeout to be 0) | ||
if (!(this.realDate)) date.setSeconds(date.getSeconds() + 1); | ||
if (this.realDate) { | ||
@@ -58,3 +85,3 @@ return date; | ||
/** | ||
* Get the number of seconds in the future at which to fire our callbacks. | ||
* Get the number of milliseconds in the future at which to fire our callbacks. | ||
*/ | ||
@@ -65,3 +92,3 @@ getTimeout: function() { | ||
/** | ||
/** | ||
* writes out a cron string | ||
@@ -78,7 +105,7 @@ */ | ||
return [ | ||
this._wcOrAll('second'), | ||
this._wcOrAll('minute'), | ||
this._wcOrAll('hour'), | ||
this._wcOrAll('dayOfMonth'), | ||
this._wcOrAll('month'), | ||
this._wcOrAll('second'), | ||
this._wcOrAll('minute'), | ||
this._wcOrAll('hour'), | ||
this._wcOrAll('dayOfMonth'), | ||
this._wcOrAll('month'), | ||
this._wcOrAll('dayOfWeek') | ||
@@ -93,12 +120,17 @@ ]; | ||
var date = new CronDate(start); | ||
//console.log("d: " + date); | ||
if (this.zone && date.setTimezone) | ||
date.setTimezone(start.getTimezone()); | ||
if (this.realDate && start < new Date()) | ||
console.log("WARNING: Date in past. Will never be fired."); | ||
if (this.realDate) return date; | ||
//sanity check | ||
var i = 1000; | ||
while(--i) { | ||
//var i = 1000; | ||
while (1) { | ||
var diff = date - start; | ||
if (!(date.getMonth() in this.month)) { | ||
date.setMonth(date.getMonth()+1); | ||
date.setMonth(date.getMonth() + 1); | ||
date.setDate(1); | ||
@@ -111,3 +143,3 @@ date.setHours(0); | ||
if (!(date.getDate() in this.dayOfMonth)) { | ||
date.setDate(date.getDate()+1); | ||
date.setDate(date.getDate() + 1); | ||
date.setHours(0); | ||
@@ -119,3 +151,3 @@ date.setMinutes(0); | ||
if (!(date.getDay() in this.dayOfWeek)) { | ||
date.setDate(date.getDate()+1); | ||
date.setDate(date.getDate() + 1); | ||
date.setHours(0); | ||
@@ -125,23 +157,23 @@ date.setMinutes(0); | ||
} | ||
if (!(date.getHours() in this.hour)) { | ||
date.setHours(date.getHours() == 23 && diff > 24*60*60*1000 ? 0 : date.getHours()+1); | ||
date.setHours(date.getHours() == 23 && diff > 24 * 60 * 60 * 1000 ? 0 : date.getHours() + 1); | ||
date.setMinutes(0); | ||
continue; | ||
} | ||
if (!(date.getMinutes() in this.minute)) { | ||
date.setMinutes(date.getMinutes() == 59 && diff > 60*60*1000 ? 0 : date.getMinutes()+1); | ||
date.setMinutes(date.getMinutes() == 59 && diff > 60 * 60 * 1000 ? 0 : date.getMinutes() + 1); | ||
date.setSeconds(0); | ||
continue; | ||
} | ||
if (!(date.getSeconds() in this.second)) { | ||
date.setSeconds(date.getSeconds() == 59 && diff > 60*1000 ? 0 : date.getSeconds()+1); | ||
date.setSeconds(date.getSeconds() == 59 && diff > 60 * 1000 ? 0 : date.getSeconds() + 1); | ||
continue; | ||
} | ||
break; | ||
} | ||
return date; | ||
@@ -154,6 +186,6 @@ }, | ||
_wcOrAll: function(type) { | ||
if(this._hasAll(type)) return '*'; | ||
if (this._hasAll(type)) return '*'; | ||
var all = []; | ||
for(var time in this[type]) { | ||
for (var time in this[type]) { | ||
all.push(time); | ||
@@ -170,4 +202,4 @@ } | ||
for(var i = constrain[0], n = constrain[1]; i < n; i++) { | ||
if(!(i in this[type])) return false; | ||
for (var i = constrain[0], n = constrain[1]; i < n; i++) { | ||
if (!(i in this[type])) return false; | ||
} | ||
@@ -184,16 +216,17 @@ | ||
var aliases = CronTime.aliases, | ||
source = this.source.replace(/[a-z]{1,3}/ig, function(alias){ | ||
alias = alias.toLowerCase(); | ||
source = this.source.replace(/[a-z]{1,3}/ig, function(alias) { | ||
alias = alias.toLowerCase(); | ||
if (alias in aliases) { | ||
return aliases[alias]; | ||
} | ||
if (alias in aliases) { | ||
return aliases[alias]; | ||
} | ||
throw new Error('Unknown alias: ' + alias); | ||
}), | ||
split = source.replace(/^\s\s*|\s\s*$/g, '').split(/\s+/), | ||
cur, i = 0, len = CronTime.map.length; | ||
throw new Error('Unknown alias: ' + alias); | ||
}), | ||
split = source.replace(/^\s\s*|\s\s*$/g, '').split(/\s+/), | ||
cur, i = 0, | ||
len = CronTime.map.length; | ||
for (; i < CronTime.map.length; i++) { | ||
// If the split source string doesn't contain all digits, | ||
// If the split source string doesn't contain all digits, | ||
// assume defaults for first n missing digits. | ||
@@ -210,32 +243,38 @@ // This adds support for 5-digit standard cron syntax | ||
_parseField: function(field, type, constraints) { | ||
var rangePattern = /(\d+?)(?:-(\d+?))?(?:\/(\d+?))?(?:,|$)/g, | ||
typeObj = this[type], | ||
diff, pointer, | ||
low = constraints[0], | ||
high = constraints[1]; | ||
//var rangePattern = /^(\*)(?:\/(\d+))?$|(\d+)(?:-(\d+))?(?:\/(\d+))?(?:,|$)/g | ||
var rangePattern = /^(\d+)(?:-(\d+))?(?:\/(\d+))?$/g, | ||
typeObj = this[type], | ||
diff, pointer, | ||
low = constraints[0], | ||
high = constraints[1]; | ||
// * is a shortcut to [lower-upper] range | ||
field = field.replace(/\*/g, low + '-' + high); | ||
field = field.replace(/\*/g, low + '-' + high); | ||
if (field.match(rangePattern)) { | ||
field.replace(rangePattern, function($0, lower, upper, step) { | ||
step = parseInt(step) || 1; | ||
//commas separate information, so split based on those | ||
var allRanges = field.split(','); | ||
// Positive integer higher than constraints[0] | ||
lower = Math.max(low, ~~Math.abs(lower)); | ||
for (var i = 0; i < allRanges.length; i++) { | ||
if (allRanges[i].match(rangePattern)) { | ||
allRanges[i].replace(rangePattern, function($0, lower, upper, step) { | ||
step = parseInt(step) || 1; | ||
// Positive integer higher than constraints[0] | ||
lower = Math.max(low, ~~Math.abs(lower)); | ||
// Positive integer lower than constraints[1] | ||
upper = upper ? Math.min(high, ~~Math.abs(upper)) : lower; | ||
// Positive integer lower than constraints[1] | ||
upper = upper ? Math.min(high, ~~Math.abs(upper)) : lower; | ||
// Count from the lower barrier to the upper | ||
pointer = lower; | ||
// Count from the lower barrier to the upper | ||
pointer = lower; | ||
do { | ||
typeObj[pointer] = true | ||
pointer += step; | ||
} while(pointer <= upper); | ||
do { | ||
typeObj[pointer] = true | ||
pointer += step; | ||
} while (pointer <= upper); | ||
}); | ||
} else { | ||
throw new Error('Field (' + field + ') cannot be parsed'); | ||
}); | ||
} else { | ||
throw new Error('Field (' + field + ') cannot be parsed'); | ||
} | ||
} | ||
@@ -263,3 +302,3 @@ } | ||
this.onComplete = onComplete; | ||
this.cronTime = new CronTime(cronTime, timeZone); | ||
this.cronTime = new CronTime(cronTime, timeZone); | ||
@@ -279,3 +318,3 @@ this.addCallback(onTick); | ||
//only functions | ||
if(typeof callback == 'function') this._callbacks.push(callback); | ||
if (typeof callback == 'function') this._callbacks.push(callback); | ||
}, | ||
@@ -287,3 +326,3 @@ | ||
_callback: function() { | ||
for (var i = (this._callbacks.length - 1); i >= 0; i--) { | ||
for (var i = (this._callbacks.length - 1); i >= 0; i--) { | ||
@@ -304,3 +343,9 @@ //send this so the callback can call this.stop(); | ||
/** | ||
* Return the next scheduled date for a job | ||
*/ | ||
nextDate: function() { | ||
return this.cronTime.sendAt(); | ||
}, | ||
/** | ||
@@ -310,3 +355,3 @@ * Start the cronjob. | ||
start: function() { | ||
if(this.running) return; | ||
if (this.running) return; | ||
@@ -372,4 +417,3 @@ var MAXDELAY = 2147483647; // The maximum number of milliseconds setTimeout will wait. | ||
*/ | ||
stop: function() | ||
{ | ||
stop: function() { | ||
clearTimeout(this._timeout); | ||
@@ -381,22 +425,21 @@ this.running = false; | ||
if (exports) { | ||
exports.job = function(cronTime, onTick, onComplete) { | ||
return new CronJob(cronTime, onTick, onComplete); | ||
} | ||
exports.job = function(cronTime, onTick, onComplete) { | ||
return new CronJob(cronTime, onTick, onComplete); | ||
} | ||
exports.time = function(cronTime, timeZone) { | ||
return new CronTime(cronTime, timeZone); | ||
} | ||
exports.time = function(cronTime, timeZone) { | ||
return new CronTime(cronTime, timeZone); | ||
} | ||
exports.sendAt = function(cronTime) { | ||
return exports.time(cronTime).sendAt(); | ||
} | ||
exports.sendAt = function(cronTime) { | ||
return exports.time(cronTime).sendAt(); | ||
} | ||
exports.timeout = function(cronTime) { | ||
return exports.time(cronTime).getTimeout(); | ||
} | ||
exports.timeout = function(cronTime) { | ||
return exports.time(cronTime).getTimeout(); | ||
exports.CronJob = CronJob; | ||
exports.CronTime = CronTime; | ||
} | ||
exports.CronJob = CronJob; | ||
exports.CronTime = CronTime; | ||
{ | ||
"name": "cron", | ||
"description": "CronJob's for your node", | ||
"version": "1.0.1", | ||
"description": "Cron jobs for your node", | ||
"version": "1.0.2", | ||
"author": "Nick Campbell <nicholas.j.campbell@gmail.com> (http://github.com/ncb000gt)", | ||
@@ -36,4 +36,5 @@ "bugs" : { | ||
"Dan Bear <daniel@hulu.com> (https://github.com/danhbear)", | ||
"Vadim Baryshev <vadimbaryshev@gmail.com> (https://github.com/baryshev)" | ||
"Vadim Baryshev <vadimbaryshev@gmail.com> (https://github.com/baryshev)", | ||
"Leandro Ferrari <lfthomaz@gmail.com> (https://github.com/lfthomaz)" | ||
] | ||
} |
node-cron | ||
========= | ||
[![Build Status](https://secure.travis-ci.org/ncb000gt/node-cron.png)](http://travis-ci.org/#!/ncb000gt/node-cron) | ||
[![Build Status](https://secure.travis-ci.org/ncb000gt/node-cron.png)](http://travis-ci.org/#!/ncb000gt/node-cron) | ||
@@ -40,4 +40,4 @@ Originally this project was a NodeJS fork of [James Padolsey's][jamespadolsey] [cron.js](http://github.com/padolsey/cron.js). | ||
}, null, true, "America/Los_Angeles"); | ||
Available Cron patterns: | ||
@@ -49,3 +49,3 @@ ========== | ||
Steps. E.g. */2 | ||
[Read up on cron patterns here](http://crontab.org). | ||
@@ -63,3 +63,3 @@ | ||
// This function is executed when the job stops | ||
}, | ||
}, | ||
true /* Start the job right now */, | ||
@@ -77,3 +77,3 @@ timeZone /* Time zone of this job. */ | ||
// This function is executed when the job stops | ||
}, | ||
}, | ||
true /* Start the job right now */, | ||
@@ -120,3 +120,3 @@ timeZone /* Time zone of this job. */ | ||
`npm install time` | ||
npm install time | ||
@@ -136,3 +136,3 @@ | ||
* `start` - [OPTIONAL] - Specifies whether to start the job after just before exiting the constructor. | ||
* `timezone` - [OPTIONAL] - Specify the timezone for the execution. This will modify the actual time relative to your timezone. | ||
* `timeZone` - [OPTIONAL] - Specify the timezone for the execution. This will modify the actual time relative to your timezone. | ||
* `context` - [OPTIONAL] - The context within which to execute the onTick method. This defaults to the cronjob itself allowing you to call `this.stop()`. However, if you change this you'll have access to the functions and values within your context object. | ||
@@ -139,0 +139,0 @@ * `start` - Runs your job. |
@@ -287,3 +287,3 @@ var testCase = require('nodeunit').testCase, | ||
if ((58 - t.getSeconds()) <= 0) { | ||
if ((56 - t.getSeconds()) <= 0) { | ||
setTimeout(testRun, (60000 - (t.getSeconds()*1000)) + 1000); | ||
@@ -295,3 +295,3 @@ } else { | ||
function testRun() { | ||
var s = d.getSeconds()+1; | ||
var s = d.getSeconds()+2; | ||
d.setSeconds(s); | ||
@@ -298,0 +298,0 @@ var c = new cron.CronJob(d, function() { |
@@ -151,3 +151,31 @@ var testCase = require('nodeunit').testCase, | ||
assert.done(); | ||
} | ||
}, | ||
'test illegal repetition syntax': function(assert){ | ||
assert.throws(function(){ | ||
new cron.CronTime('* * /4 * * *'); | ||
}); | ||
assert.done(); | ||
}, | ||
'test next date': function(assert) { | ||
assert.expect(2); | ||
var ct = new cron.CronTime('0 0 */4 * * *'); | ||
var nextDate = new Date(); | ||
nextDate.setHours(23); | ||
var nextdt = ct._getNextDateFrom(nextDate); | ||
assert.ok(nextdt > nextDate); | ||
assert.ok(nextdt.getHours() % 4 === 0); | ||
assert.done(); | ||
}, | ||
'test next real date': function(assert) { | ||
assert.expect(2); | ||
var ct = new cron.CronTime(new Date()); | ||
var nextDate = new Date(); | ||
nextDate.setMonth(nextDate.getMonth()+1); | ||
assert.ok(nextDate > ct.source); | ||
var nextdt = ct._getNextDateFrom(nextDate); | ||
assert.deepEqual(nextdt, nextDate); | ||
assert.done(); | ||
} | ||
}); |
Non-existent author
Supply chain riskThe package was published by an npm account that no longer exists.
Found 1 instance in 1 package
35674
9
937
0