node-schedule
Advanced tools
Comparing version 0.1.1 to 0.1.2
@@ -32,5 +32,2 @@ /* | ||
var pendingInvocations = []; | ||
function sorter(a, b){ | ||
return (a.fireDate.getTime() - b.fireDate.getTime()); | ||
} | ||
@@ -72,3 +69,5 @@ // define properties | ||
inv = pendingInvocations[i]; | ||
clearTimeout(inv.timerID); | ||
cancelInvocation(inv); | ||
if (reschedule && inv.recurrenceRule.recurs) | ||
@@ -97,3 +96,5 @@ { | ||
var nextInv = pendingInvocations.shift(); | ||
clearTimeout(nextInv.timerID); | ||
cancelInvocation(nextInv); | ||
if (reschedule && nextInv.recurrenceRule.recurs) | ||
@@ -127,10 +128,6 @@ { | ||
{ | ||
var inv = new Invocation(null, spec); | ||
inv.timerID = runOnDate(spec, function(){ | ||
job.invoke(); | ||
job.stopTrackingInvocation(inv); | ||
}); | ||
if (inv.timerID !== false) | ||
success = this.trackInvocation(inv); | ||
var inv = new Invocation(this, spec); | ||
success = scheduleInvocation(inv); | ||
if (success) | ||
this.trackInvocation(inv); | ||
} | ||
@@ -190,8 +187,14 @@ else if (typeof(spec) == 'string' || (typeof(spect) == 'object' && spec instanceof String)) | ||
/* Invocation object */ | ||
function Invocation(timerID, fireDate, recurrenceRule){ | ||
this.timerID = timerID; | ||
function Invocation(job, fireDate, recurrenceRule){ | ||
this.job = job; | ||
this.fireDate = fireDate; | ||
this.recurrenceRule = recurrenceRule || DoesntRecur; | ||
this.timerID = null; | ||
} | ||
function sorter(a, b){ | ||
return (a.fireDate.getTime() - b.fireDate.getTime()); | ||
} | ||
/* Range object */ | ||
@@ -491,2 +494,65 @@ function Range(start, end, step){ | ||
var invocations = []; | ||
var currentInvocation = null; | ||
function scheduleInvocation(invocation){ | ||
invocations.push(invocation); | ||
invocations.sort(sorter); | ||
prepareNextInvocation(); | ||
invocation.job.emit('scheduled', invocation.fireDate); | ||
} | ||
function prepareNextInvocation(){ | ||
if (invocations.length > 0 && currentInvocation != invocations[0]) | ||
{ | ||
if (currentInvocation !== null) | ||
{ | ||
clearTimeout(currentInvocation.timerID); | ||
currentInvocation.timerID = null; | ||
currentInvocation = null; | ||
} | ||
currentInvocation = invocations[0]; | ||
var job = currentInvocation.job; | ||
var cinv = currentInvocation; | ||
currentInvocation.timerID = runOnDate(currentInvocation.fireDate, function(){ | ||
currentInvocationFinished(); | ||
if (cinv.recurrenceRule.recurs) | ||
{ | ||
var inv = scheduleNextRecurrence(cinv.recurrenceRule, cinv.job, cinv.fireDate); | ||
if (inv !== null) | ||
inv.job.trackInvocation(inv); | ||
} | ||
job.stopTrackingInvocation(cinv); | ||
job.invoke(); | ||
job.emit('run'); | ||
}); | ||
} | ||
} | ||
function currentInvocationFinished(){ | ||
invocations.shift(); | ||
currentInvocation = null; | ||
prepareNextInvocation(); | ||
} | ||
function cancelInvocation(invocation){ | ||
var idx = invocations.indexOf(invocation); | ||
if (idx > -1) | ||
{ | ||
invocations.splice(idx, 1); | ||
if (invocation.timerID !== null) | ||
clearTimeout(invocation.timerID); | ||
if (currentInvocation == invocation) | ||
currentInvocation = null; | ||
invocation.job.emit('canceled', invocation.fireDate); | ||
prepareNextInvocation(); | ||
} | ||
} | ||
/* Recurrence scheduler */ | ||
@@ -500,15 +566,5 @@ function scheduleNextRecurrence(rule, job, prevDate){ | ||
var inv = new Invocation(null, date, rule); | ||
inv.timerID = runOnDate(date, function(){ | ||
job.invoke(); | ||
job.stopTrackingInvocation(inv); | ||
var inv = scheduleNextRecurrence(rule, job, date); | ||
if (inv !== null) | ||
job.trackInvocation(inv); | ||
}); | ||
var inv = new Invocation(job, date, rule); | ||
scheduleInvocation(inv); | ||
if (inv.timerID === false); | ||
return null; | ||
return inv; | ||
@@ -515,0 +571,0 @@ } |
{ | ||
"name": "node-schedule", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "A cron-like and not-cron-like job scheduler for Node.", | ||
@@ -5,0 +5,0 @@ "keywords": ["schedule", "task", "job", "cron"], |
@@ -13,2 +13,4 @@ node-schedule | ||
`Job` objects are `EventEmitter`s, and emit a `run` event after each execution. They also emit a `scheduled` event each time they're scheduled to run, and a `canceled` event when an invocation is canceled before it's executed (both events receive a JavaScript date object as a parameter). Note that jobs are scheduled the first time immediately, so if you create a job using the `scheduleJob()` convenience method, you'll miss the first `scheduled` event. Also note that `canceled` is the single-L American spelling. | ||
Date-based Scheduling | ||
@@ -15,0 +17,0 @@ --------------------- |
21789
612
89