node-schedule
Advanced tools
Comparing version 0.2.3 to 0.2.4
@@ -6,3 +6,3 @@ 'use strict'; | ||
A cron-like and not-cron-like job scheduler for Node. | ||
This file adds convenience functions for performing incremental date math | ||
@@ -9,0 +9,0 @@ in the Gregorian calendar. |
@@ -11,3 +11,3 @@ 'use strict'; | ||
increment = require('./increment.js'), | ||
cron_parser = require('cron-parser'), | ||
cronParser = require('cron-parser'), | ||
lt = require('long-timeout'); | ||
@@ -25,12 +25,11 @@ | ||
arg = arguments[i]; | ||
if (typeof(arg) == 'string' || arg instanceof String) | ||
if (typeof arg == 'string' || arg instanceof String) { | ||
name = arg; | ||
// else if (typeof(arg) == 'function') | ||
else | ||
} else { | ||
this.job = arg; | ||
} | ||
} | ||
// give us a random name if one wasn't provided | ||
if (name == null) | ||
name = '<Anonymous Job ' + (++anonJobCounter) + '>'; | ||
if (name == null) name = '<Anonymous Job ' + (++anonJobCounter) + '>'; | ||
@@ -67,8 +66,8 @@ // setup a private pendingInvocations variable | ||
this.cancel = function(reschedule) { | ||
reschedule = (typeof(reschedule) == 'boolean') ? reschedule : false; | ||
reschedule = (typeof reschedule == 'boolean') ? reschedule : false; | ||
var inv, newInv; | ||
var newInvs = []; | ||
for (var i = 0; i < pendingInvocations.length; i++) { | ||
inv = pendingInvocations[i]; | ||
for (var j = 0; j < pendingInvocations.length; j++) { | ||
inv = pendingInvocations[j]; | ||
@@ -79,4 +78,3 @@ cancelInvocation(inv); | ||
newInv = scheduleNextRecurrence(inv.recurrenceRule, this, inv.fireDate); | ||
if (newInv !== null) | ||
newInvs.push(newInv); | ||
if (newInv !== null) newInvs.push(newInv); | ||
} | ||
@@ -87,4 +85,5 @@ } | ||
for (var i = 0; i < newInvs.length; i++) | ||
this.trackInvocation(newInvs[i]); | ||
for (var k = 0; k < newInvs.length; k++) { | ||
this.trackInvocation(newInvs[k]); | ||
} | ||
@@ -94,6 +93,5 @@ return true; | ||
this.cancelNext = function(reschedule) { | ||
reschedule = (typeof(reschedule) == 'boolean') ? reschedule : true; | ||
reschedule = (typeof reschedule == 'boolean') ? reschedule : true; | ||
if (pendingInvocations.length == 0) | ||
return false; | ||
if (!pendingInvocations.length) return false; | ||
@@ -107,4 +105,3 @@ var newInv; | ||
newInv = scheduleNextRecurrence(nextInv.recurrenceRule, this, nextInv.fireDate); | ||
if (newInv !== null) | ||
this.trackInvocation(newInv); | ||
if (newInv !== null) this.trackInvocation(newInv); | ||
} | ||
@@ -115,4 +112,3 @@ | ||
this.nextInvocation = function() { | ||
if (pendingInvocations.length == 0) | ||
return null; | ||
if (!pendingInvocations.length) return null; | ||
return pendingInvocations[0].fireDate; | ||
@@ -128,3 +124,3 @@ }; | ||
Job.prototype.invoke = function() { | ||
if (typeof(this.job) == 'function') { | ||
if (typeof this.job == 'function') { | ||
this.job(); | ||
@@ -143,16 +139,13 @@ } else { | ||
var success = false; | ||
var inv; | ||
try { | ||
var res = cron_parser.parseExpression(spec); | ||
var inv = scheduleNextRecurrence(res, self); | ||
if (inv !== null) { | ||
success = self.trackInvocation(inv); | ||
} | ||
var res = cronParser.parseExpression(spec); | ||
inv = scheduleNextRecurrence(res, self); | ||
if (inv !== null) success = self.trackInvocation(inv); | ||
} catch (err) { | ||
var type = typeof(spec); | ||
if (type === 'string') { | ||
spec = new Date(spec); | ||
} | ||
var type = typeof spec; | ||
if (type === 'string') spec = new Date(spec); | ||
if (spec instanceof Date) { | ||
var inv = new Invocation(self, spec); | ||
inv = new Invocation(self, spec); | ||
scheduleInvocation(inv); | ||
@@ -163,16 +156,9 @@ success = self.trackInvocation(inv); | ||
var r = new RecurrenceRule(); | ||
if ('year' in spec) | ||
r.year = spec.year; | ||
if ('month' in spec) | ||
r.month = spec.month; | ||
if ('date' in spec) | ||
r.date = spec.date; | ||
if ('dayOfWeek' in spec) | ||
r.dayOfWeek = spec.dayOfWeek; | ||
if ('hour' in spec) | ||
r.hour = spec.hour; | ||
if ('minute' in spec) | ||
r.minute = spec.minute; | ||
if ('second' in spec) | ||
r.second = spec.second; | ||
if ('year' in spec) r.year = spec.year; | ||
if ('month' in spec) r.month = spec.month; | ||
if ('date' in spec) r.date = spec.date; | ||
if ('dayOfWeek' in spec) r.dayOfWeek = spec.dayOfWeek; | ||
if ('hour' in spec) r.hour = spec.hour; | ||
if ('minute' in spec) r.minute = spec.minute; | ||
if ('second' in spec) r.second = spec.second; | ||
@@ -182,5 +168,4 @@ spec = r; | ||
var inv = scheduleNextRecurrence(spec, self); | ||
if (inv !== null) | ||
success = self.trackInvocation(inv); | ||
inv = scheduleNextRecurrence(spec, self); | ||
if (inv !== null) success = self.trackInvocation(inv); | ||
} | ||
@@ -204,2 +189,6 @@ } | ||
/* DoesntRecur rule */ | ||
var DoesntRecur = new RecurrenceRule(); | ||
DoesntRecur.recurs = false; | ||
/* Invocation object */ | ||
@@ -226,8 +215,7 @@ function Invocation(job, fireDate, recurrenceRule) { | ||
Range.prototype.contains = function(val) { | ||
if (this.step === null || this.step === 1) | ||
if (this.step === null || this.step === 1) { | ||
return (val >= this.start && val <= this.end); | ||
else { | ||
} else { | ||
for (var i = this.start; i < this.end; i += this.step) { | ||
if (i == val) | ||
return true; | ||
if (i === val) return true; | ||
} | ||
@@ -269,9 +257,7 @@ | ||
increment.addDateConvenienceMethods(base); | ||
if (!this.recurs) | ||
return null; | ||
if (!this.recurs) return null; | ||
var now = new Date(); | ||
increment.addDateConvenienceMethods(now); | ||
if (this.year !== null && (typeof(this.year) == 'number') && this.year < now.getFullYear()) | ||
return null; | ||
if (this.year !== null && (typeof this.year == 'number') && this.year < now.getFullYear()) return null; | ||
@@ -337,13 +323,11 @@ var next = new Date(base.getTime()); | ||
function recurMatch(val, matcher) { | ||
if (matcher == null) | ||
return true; | ||
if (matcher == null) return true; | ||
if (typeof(matcher) == 'number' || typeof(matcher) == 'string') | ||
return (val == matcher); | ||
else if (typeof(matcher) == 'object' && matcher instanceof Range) | ||
if (typeof matcher === 'number' || typeof matcher === 'string') { | ||
return (val === matcher); | ||
} else if (typeof matcher === 'object' && matcher instanceof Range) { | ||
return matcher.contains(val); | ||
else if (Array.isArray(matcher) || (typeof(matcher) == 'object' && matcher instanceof Array)) { | ||
} else if (Array.isArray(matcher) || (typeof matcher === 'object' && matcher instanceof Array)) { | ||
for (var i = 0; i < matcher.length; i++) { | ||
if (recurMatch(val, matcher[i])) | ||
return true; | ||
if (recurMatch(val, matcher[i])) return true; | ||
} | ||
@@ -356,6 +340,2 @@ return false; | ||
/* DoesntRecur rule */ | ||
var DoesntRecur = new RecurrenceRule(); | ||
DoesntRecur.recurs = false; | ||
/* Date-based scheduler */ | ||
@@ -367,3 +347,2 @@ function runOnDate(date, job) { | ||
if (then < now) { | ||
// if (now - then < 1000) | ||
process.nextTick(job); | ||
@@ -387,3 +366,3 @@ return null; | ||
function prepareNextInvocation() { | ||
if (invocations.length > 0 && currentInvocation != invocations[0]) { | ||
if (invocations.length > 0 && currentInvocation !== invocations[0]) { | ||
if (currentInvocation !== null) { | ||
@@ -404,4 +383,3 @@ lt.clearTimeout(currentInvocation.timerID); | ||
var inv = scheduleNextRecurrence(cinv.recurrenceRule, cinv.job, cinv.fireDate); | ||
if (inv !== null) | ||
inv.job.trackInvocation(inv); | ||
if (inv !== null) inv.job.trackInvocation(inv); | ||
} | ||
@@ -427,7 +405,5 @@ | ||
invocations.splice(idx, 1); | ||
if (invocation.timerID !== null) | ||
lt.clearTimeout(invocation.timerID); | ||
if (invocation.timerID !== null) lt.clearTimeout(invocation.timerID); | ||
if (currentInvocation == invocation) | ||
currentInvocation = null; | ||
if (currentInvocation === invocation) currentInvocation = null; | ||
@@ -444,4 +420,3 @@ invocation.job.emit('canceled', invocation.fireDate); | ||
var date = (rule instanceof RecurrenceRule) ? rule.nextInvocationDate(prevDate) : rule.next(); | ||
if (date === null) | ||
return null; | ||
if (date === null) return null; | ||
@@ -458,5 +433,3 @@ var inv = new Invocation(job, date, rule); | ||
function scheduleJob() { | ||
if (arguments.length < 2) { | ||
return null; | ||
} | ||
if (arguments.length < 2) return null; | ||
@@ -468,5 +441,5 @@ var name = (arguments.length >= 3) ? arguments[0] : null; | ||
var job = new Job(name, method); | ||
if (job.schedule(spec)) { | ||
scheduledJobs[job.name] = job; | ||
return job; | ||
@@ -485,4 +458,4 @@ } | ||
if (scheduledJobs.hasOwnProperty(name)) { | ||
if (scheduledJobs[name] == job) { | ||
delete scheduledJobs[name]; | ||
if (scheduledJobs[name] === job) { | ||
scheduledJobs[name] = null; | ||
break; | ||
@@ -493,7 +466,6 @@ } | ||
} | ||
} else if (typeof(job) == 'string' || job instanceof String) { | ||
} else if (typeof job == 'string' || job instanceof String) { | ||
if (job in scheduledJobs && scheduledJobs.hasOwnProperty(job)) { | ||
success = scheduledJobs[job].cancel(); | ||
if (success) | ||
delete scheduledJobs[job]; | ||
if (success) scheduledJobs[job] = null; | ||
} | ||
@@ -500,0 +472,0 @@ } |
{ | ||
"name": "node-schedule", | ||
"version": "0.2.3", | ||
"version": "0.2.4", | ||
"description": "A cron-like and not-cron-like job scheduler for Node.", | ||
"keywords": ["schedule", "task", "job", "cron"], | ||
"main": "./lib/schedule.js", | ||
"scripts": { | ||
"test": "./node_modules/.bin/nodeunit test", | ||
"lint": "./node_modules/.bin/eslint lib" | ||
}, | ||
"author": { | ||
@@ -11,18 +16,15 @@ "name": "Matt Patenaude", | ||
}, | ||
"main": "./lib/schedule.js", | ||
"scripts": { | ||
"test": "./node_modules/.bin/nodeunit test" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/mattpat/node-schedule.git" | ||
"url": "https://github.com/node-schedule/node-schedule.git" | ||
}, | ||
"dependencies": { | ||
"cron-parser": "~0.6.1", | ||
"long-timeout": "~0.0.1" | ||
"cron-parser": "0.6.1", | ||
"long-timeout": "0.0.1" | ||
}, | ||
"devDependencies": { | ||
"nodeunit": "~0.8.1", | ||
"sinon": "~1.12.2" | ||
"eslint": "0.15.1", | ||
"nodeunit": "0.8.1", | ||
"sinon": "1.12.2" | ||
} | ||
} |
@@ -1,6 +0,6 @@ | ||
# node-schedule | ||
# node-schedule [](https://www.npmjs.com/package/node-schedule) [](https://www.npmjs.com/package/node-schedule) [](https://travis-ci.org/node-schedule/node-schedule) | ||
[](https://nodei.co/npm/node-schedule/) | ||
node-schedule is a flexible both cron-like and not-cron-like job scheduler for Node.js. It allows you to schedule jobs (arbitrary functions) for execution at specific dates, with optional recurrence rules. It only uses a single timer at any given time (rather than reevaluating upcoming jobs every second/minute). | ||
node-schedule is a flexible cron-like and not-cron-like job scheduler for Node.js. It allows you to schedule jobs (arbitrary functions) for execution at specific dates, with optional recurrence rules. It only uses a single timer at any given time (rather than reevaluating upcoming jobs every second/minute). | ||
@@ -18,3 +18,3 @@ Read more about the module's core functions on the [About](https://github.com/mattpat/node-schedule/wiki/About) page of the wiki. | ||
Before jumping in, check out our **[Contributing] [contributing]** page on the wiki! | ||
Before jumping in, check out our [Contributing](https://github.com/mattpat/node-schedule/wiki/Contributing) page on the wiki! | ||
@@ -26,3 +26,3 @@ | ||
**Tejas Manohar** | ||
**Tejas Manohar - Maintainer** | ||
@@ -32,7 +32,11 @@ - <https://twitter.com/tejasmanohar> | ||
**Santiago Gimeno** | ||
**Santiago Gimeno - Collaborator** | ||
- <https://github.com/santigimeno> | ||
**Jon Hester - Collaborator** | ||
- <https://github.com/jonhester> | ||
## Copyright and license | ||
@@ -42,5 +46,5 @@ | ||
Licensed under the **[MIT License] [license]**; | ||
you may only use this software in compliance with the License. | ||
Licensed under the **[MIT License] [license]**. | ||
[cron]: http://unixhelp.ed.ac.uk/CGI/man-cgi?crontab+5 | ||
@@ -47,0 +51,0 @@ [wiki]: https://github.com/mattpat/node-schedule/wiki |
@@ -49,2 +49,33 @@ var sinon = require('sinon'); | ||
}, | ||
"Cancel next job before it runs": function(test) { | ||
test.expect(1); | ||
var job = new schedule.Job(function() { | ||
test.ok(true); | ||
}); | ||
job.schedule(new Date(Date.now() + 1500)); | ||
job.schedule(new Date(Date.now() + 3000)); | ||
job.cancelNext(); | ||
setTimeout(function() { | ||
test.done(); | ||
}, 3250); | ||
clock.tick(3250); | ||
}, | ||
"Run job on specified date": function(test) { | ||
test.expect(1); | ||
var job = new schedule.Job(function() { | ||
test.ok(true); | ||
}); | ||
job.runOnDate(new Date(Date.now() + 3000)); | ||
setTimeout(function() { | ||
test.done(); | ||
}, 3250); | ||
clock.tick(3250); | ||
}, | ||
/* No jobs will run after this test for some reason - hide for now | ||
@@ -51,0 +82,0 @@ "Won't run job if scheduled in the past": function(test) { |
@@ -126,3 +126,2 @@ var main = require('../package.json').main; | ||
}, | ||
/* | ||
"in the year 2040": function(test) { | ||
@@ -137,3 +136,11 @@ var rule = new schedule.RecurrenceRule(); | ||
}, | ||
*/ | ||
"using past year": function(test) { | ||
var rule = new schedule.RecurrenceRule(); | ||
rule.year = 2000; | ||
var next = rule.nextInvocationDate(base); | ||
test.equal(null, next); | ||
test.done(); | ||
}, | ||
"using mixed time components": function(test) { | ||
@@ -140,0 +147,0 @@ var rule = new schedule.RecurrenceRule(); |
@@ -96,5 +96,5 @@ var sinon = require('sinon'); | ||
"Runs job every month": function(test) { | ||
test.expect(3); | ||
test.expect(48); | ||
var timeout = 3 * 29.53 * 24 * 60 * 60 * 1000; | ||
var timeout = 4 * 365.25 * 24 * 60 * 60 * 1000; | ||
@@ -111,7 +111,8 @@ var job = schedule.scheduleJob('0 0 0 1 * *', function() { | ||
clock.tick(timeout); | ||
}, | ||
"Runs job every year": function(test) { | ||
test.expect(3); | ||
test.expect(4); | ||
var timeout = 3 * 365.25 * 24 * 60 * 60 * 1000; | ||
var timeout = 4 * 365.25 * 24 * 60 * 60 * 1000; | ||
@@ -118,0 +119,0 @@ var job = schedule.scheduleJob('0 0 0 1 1 *', function() { |
44835
14
1314
51
3
+ Addedcron-parser@0.6.1(transitive)
+ Addedlong-timeout@0.0.1(transitive)
- Removedcron-parser@0.6.4(transitive)
- Removedlong-timeout@0.0.2(transitive)
Updatedcron-parser@0.6.1
Updatedlong-timeout@0.0.1