node-schedule 1.3.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +27 -0
- package/README.md +87 -57
- package/UPGRADING.md +20 -0
- package/index.js +16 -0
- package/lib/Invocation.js +339 -0
- package/lib/Job.js +284 -0
- package/lib/schedule.js +22 -588
- package/lib/utils/dateUtils.js +11 -0
- package/package.json +35 -13
- package/.eslintrc +0 -17
- package/.travis.yml +0 -15
- package/CONTRIBUTING.md +0 -31
- package/test/.eslintrc +0 -5
- package/test/cancel-long-running-jobs.js +0 -24
- package/test/convenience-method-test.js +0 -713
- package/test/date-convenience-methods-test.js +0 -59
- package/test/es6/job-test.js +0 -33
- package/test/job-test.js +0 -517
- package/test/range-test.js +0 -77
- package/test/recurrence-rule-test.js +0 -375
- package/test/schedule-cron-jobs.js +0 -134
- package/test/start-end-test.js +0 -327
package/lib/schedule.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
|
|
2
1
|
'use strict';
|
|
3
2
|
|
|
4
3
|
/*
|
|
@@ -6,272 +5,7 @@
|
|
|
6
5
|
A cron-like and not-cron-like job scheduler for Node.
|
|
7
6
|
*/
|
|
8
7
|
|
|
9
|
-
|
|
10
|
-
util = require('util'),
|
|
11
|
-
cronParser = require('cron-parser'),
|
|
12
|
-
CronDate = require('cron-parser/lib/date'),
|
|
13
|
-
lt = require('long-timeout'),
|
|
14
|
-
sorted = require('sorted-array-functions');
|
|
15
|
-
|
|
16
|
-
/* Job object */
|
|
17
|
-
var anonJobCounter = 0;
|
|
18
|
-
var scheduledJobs = {};
|
|
19
|
-
|
|
20
|
-
function isValidDate(date) {
|
|
21
|
-
// Taken from http://stackoverflow.com/a/12372720/1562178
|
|
22
|
-
// If getTime() returns NaN it'll return false anyway
|
|
23
|
-
return date.getTime() === date.getTime();
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
function Job(name, job, callback) {
|
|
27
|
-
// setup a private pendingInvocations variable
|
|
28
|
-
var pendingInvocations = [];
|
|
29
|
-
|
|
30
|
-
//setup a private number of invocations variable
|
|
31
|
-
var triggeredJobs = 0;
|
|
32
|
-
|
|
33
|
-
// Set scope vars
|
|
34
|
-
var jobName = name && typeof name === 'string' ? name : '<Anonymous Job ' + (++anonJobCounter) + '>';
|
|
35
|
-
this.job = name && typeof name === 'function' ? name : job;
|
|
36
|
-
|
|
37
|
-
// Make sure callback is actually a callback
|
|
38
|
-
if (this.job === name) {
|
|
39
|
-
// Name wasn't provided and maybe a callback is there
|
|
40
|
-
this.callback = typeof job === 'function' ? job : false;
|
|
41
|
-
} else {
|
|
42
|
-
// Name was provided, and maybe a callback is there
|
|
43
|
-
this.callback = typeof callback === 'function' ? callback : false;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
// Check for generator
|
|
47
|
-
if (typeof this.job === 'function' &&
|
|
48
|
-
this.job.prototype &&
|
|
49
|
-
this.job.prototype.next) {
|
|
50
|
-
this.job = function() {
|
|
51
|
-
return this.next().value;
|
|
52
|
-
}.bind(this.job.call(this));
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
// define properties
|
|
56
|
-
Object.defineProperty(this, 'name', {
|
|
57
|
-
value: jobName,
|
|
58
|
-
writable: false,
|
|
59
|
-
enumerable: true
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
// method that require private access
|
|
63
|
-
this.trackInvocation = function(invocation) {
|
|
64
|
-
// add to our invocation list
|
|
65
|
-
sorted.add(pendingInvocations, invocation, sorter);
|
|
66
|
-
return true;
|
|
67
|
-
};
|
|
68
|
-
this.stopTrackingInvocation = function(invocation) {
|
|
69
|
-
var invIdx = pendingInvocations.indexOf(invocation);
|
|
70
|
-
if (invIdx > -1) {
|
|
71
|
-
pendingInvocations.splice(invIdx, 1);
|
|
72
|
-
return true;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
return false;
|
|
76
|
-
};
|
|
77
|
-
this.triggeredJobs = function() {
|
|
78
|
-
return triggeredJobs;
|
|
79
|
-
};
|
|
80
|
-
this.setTriggeredJobs = function(triggeredJob) {
|
|
81
|
-
triggeredJobs = triggeredJob;
|
|
82
|
-
};
|
|
83
|
-
this.cancel = function(reschedule) {
|
|
84
|
-
reschedule = (typeof reschedule == 'boolean') ? reschedule : false;
|
|
85
|
-
|
|
86
|
-
var inv, newInv;
|
|
87
|
-
var newInvs = [];
|
|
88
|
-
for (var j = 0; j < pendingInvocations.length; j++) {
|
|
89
|
-
inv = pendingInvocations[j];
|
|
90
|
-
|
|
91
|
-
cancelInvocation(inv);
|
|
92
|
-
|
|
93
|
-
if (reschedule && inv.recurrenceRule.recurs) {
|
|
94
|
-
newInv = scheduleNextRecurrence(inv.recurrenceRule, this, inv.fireDate, inv.endDate);
|
|
95
|
-
if (newInv !== null) {
|
|
96
|
-
newInvs.push(newInv);
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
pendingInvocations = [];
|
|
102
|
-
|
|
103
|
-
for (var k = 0; k < newInvs.length; k++) {
|
|
104
|
-
this.trackInvocation(newInvs[k]);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
// remove from scheduledJobs if reschedule === false
|
|
108
|
-
if (!reschedule) {
|
|
109
|
-
if (this.name) {
|
|
110
|
-
delete scheduledJobs[this.name];
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
return true;
|
|
115
|
-
};
|
|
116
|
-
this.cancelNext = function(reschedule) {
|
|
117
|
-
reschedule = (typeof reschedule == 'boolean') ? reschedule : true;
|
|
118
|
-
|
|
119
|
-
if (!pendingInvocations.length) {
|
|
120
|
-
return false;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
var newInv;
|
|
124
|
-
var nextInv = pendingInvocations.shift();
|
|
125
|
-
|
|
126
|
-
cancelInvocation(nextInv);
|
|
127
|
-
|
|
128
|
-
if (reschedule && nextInv.recurrenceRule.recurs) {
|
|
129
|
-
newInv = scheduleNextRecurrence(nextInv.recurrenceRule, this, nextInv.fireDate, nextInv.endDate);
|
|
130
|
-
if (newInv !== null) {
|
|
131
|
-
this.trackInvocation(newInv);
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
return true;
|
|
136
|
-
};
|
|
137
|
-
this.reschedule = function(spec) {
|
|
138
|
-
var inv;
|
|
139
|
-
var cInvs = pendingInvocations.slice();
|
|
140
|
-
|
|
141
|
-
for (var j = 0; j < cInvs.length; j++) {
|
|
142
|
-
inv = cInvs[j];
|
|
143
|
-
|
|
144
|
-
cancelInvocation(inv);
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
pendingInvocations = [];
|
|
148
|
-
|
|
149
|
-
if (this.schedule(spec)) {
|
|
150
|
-
this.setTriggeredJobs(0);
|
|
151
|
-
return true;
|
|
152
|
-
} else {
|
|
153
|
-
pendingInvocations = cInvs;
|
|
154
|
-
return false;
|
|
155
|
-
}
|
|
156
|
-
};
|
|
157
|
-
this.nextInvocation = function() {
|
|
158
|
-
if (!pendingInvocations.length) {
|
|
159
|
-
return null;
|
|
160
|
-
}
|
|
161
|
-
return pendingInvocations[0].fireDate;
|
|
162
|
-
};
|
|
163
|
-
this.pendingInvocations = function() {
|
|
164
|
-
return pendingInvocations;
|
|
165
|
-
};
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
util.inherits(Job, events.EventEmitter);
|
|
169
|
-
|
|
170
|
-
Job.prototype.invoke = function(fireDate) {
|
|
171
|
-
if (typeof this.job == 'function') {
|
|
172
|
-
this.setTriggeredJobs(this.triggeredJobs() + 1);
|
|
173
|
-
this.job(fireDate);
|
|
174
|
-
} else {
|
|
175
|
-
this.job.execute(fireDate);
|
|
176
|
-
}
|
|
177
|
-
};
|
|
178
|
-
|
|
179
|
-
Job.prototype.runOnDate = function(date) {
|
|
180
|
-
return this.schedule(date);
|
|
181
|
-
};
|
|
182
|
-
|
|
183
|
-
Job.prototype.schedule = function(spec) {
|
|
184
|
-
var self = this;
|
|
185
|
-
var success = false;
|
|
186
|
-
var inv;
|
|
187
|
-
var start;
|
|
188
|
-
var end;
|
|
189
|
-
var tz;
|
|
190
|
-
|
|
191
|
-
if (typeof spec === 'object' && spec.rule) {
|
|
192
|
-
start = spec.start || undefined;
|
|
193
|
-
end = spec.end || undefined;
|
|
194
|
-
tz = spec.tz;
|
|
195
|
-
spec = spec.rule;
|
|
196
|
-
|
|
197
|
-
if (start) {
|
|
198
|
-
if (!(start instanceof Date)) {
|
|
199
|
-
start = new Date(start);
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
start = new CronDate(start, tz);
|
|
203
|
-
if (!isValidDate(start) || start.getTime() < Date.now()) {
|
|
204
|
-
start = undefined;
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
if (end && !(end instanceof Date) && !isValidDate(end = new Date(end))) {
|
|
209
|
-
end = undefined;
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
if (end) {
|
|
213
|
-
end = new CronDate(end, tz);
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
try {
|
|
218
|
-
var res = cronParser.parseExpression(spec, { currentDate: start, tz: tz });
|
|
219
|
-
inv = scheduleNextRecurrence(res, self, start, end);
|
|
220
|
-
if (inv !== null) {
|
|
221
|
-
success = self.trackInvocation(inv);
|
|
222
|
-
}
|
|
223
|
-
} catch (err) {
|
|
224
|
-
var type = typeof spec;
|
|
225
|
-
if ((type === 'string') || (type === 'number')) {
|
|
226
|
-
spec = new Date(spec);
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
if ((spec instanceof Date) && (isValidDate(spec))) {
|
|
230
|
-
spec = new CronDate(spec);
|
|
231
|
-
if (spec.getTime() >= Date.now()) {
|
|
232
|
-
inv = new Invocation(self, spec);
|
|
233
|
-
scheduleInvocation(inv);
|
|
234
|
-
success = self.trackInvocation(inv);
|
|
235
|
-
}
|
|
236
|
-
} else if (type === 'object') {
|
|
237
|
-
if (!(spec instanceof RecurrenceRule)) {
|
|
238
|
-
var r = new RecurrenceRule();
|
|
239
|
-
if ('year' in spec) {
|
|
240
|
-
r.year = spec.year;
|
|
241
|
-
}
|
|
242
|
-
if ('month' in spec) {
|
|
243
|
-
r.month = spec.month;
|
|
244
|
-
}
|
|
245
|
-
if ('date' in spec) {
|
|
246
|
-
r.date = spec.date;
|
|
247
|
-
}
|
|
248
|
-
if ('dayOfWeek' in spec) {
|
|
249
|
-
r.dayOfWeek = spec.dayOfWeek;
|
|
250
|
-
}
|
|
251
|
-
if ('hour' in spec) {
|
|
252
|
-
r.hour = spec.hour;
|
|
253
|
-
}
|
|
254
|
-
if ('minute' in spec) {
|
|
255
|
-
r.minute = spec.minute;
|
|
256
|
-
}
|
|
257
|
-
if ('second' in spec) {
|
|
258
|
-
r.second = spec.second;
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
spec = r;
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
spec.tz = tz;
|
|
265
|
-
inv = scheduleNextRecurrence(spec, self, start, end);
|
|
266
|
-
if (inv !== null) {
|
|
267
|
-
success = self.trackInvocation(inv);
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
scheduledJobs[this.name] = this;
|
|
273
|
-
return success;
|
|
274
|
-
};
|
|
8
|
+
const { Job, scheduledJobs } = require('./Job')
|
|
275
9
|
|
|
276
10
|
/* API
|
|
277
11
|
invoke()
|
|
@@ -285,322 +19,22 @@ Job.prototype.schedule = function(spec) {
|
|
|
285
19
|
job: readwrite
|
|
286
20
|
*/
|
|
287
21
|
|
|
288
|
-
/* DoesntRecur rule */
|
|
289
|
-
var DoesntRecur = new RecurrenceRule();
|
|
290
|
-
DoesntRecur.recurs = false;
|
|
291
|
-
|
|
292
|
-
/* Invocation object */
|
|
293
|
-
function Invocation(job, fireDate, recurrenceRule, endDate) {
|
|
294
|
-
this.job = job;
|
|
295
|
-
this.fireDate = fireDate;
|
|
296
|
-
this.endDate = endDate;
|
|
297
|
-
this.recurrenceRule = recurrenceRule || DoesntRecur;
|
|
298
|
-
|
|
299
|
-
this.timerID = null;
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
function sorter(a, b) {
|
|
303
|
-
return (a.fireDate.getTime() - b.fireDate.getTime());
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
/* Range object */
|
|
307
|
-
function Range(start, end, step) {
|
|
308
|
-
this.start = start || 0;
|
|
309
|
-
this.end = end || 60;
|
|
310
|
-
this.step = step || 1;
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
Range.prototype.contains = function(val) {
|
|
314
|
-
if (this.step === null || this.step === 1) {
|
|
315
|
-
return (val >= this.start && val <= this.end);
|
|
316
|
-
} else {
|
|
317
|
-
for (var i = this.start; i < this.end; i += this.step) {
|
|
318
|
-
if (i === val) {
|
|
319
|
-
return true;
|
|
320
|
-
}
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
return false;
|
|
324
|
-
}
|
|
325
|
-
};
|
|
326
|
-
|
|
327
|
-
/* RecurrenceRule object */
|
|
328
|
-
/*
|
|
329
|
-
Interpreting each property:
|
|
330
|
-
null - any value is valid
|
|
331
|
-
number - fixed value
|
|
332
|
-
Range - value must fall in range
|
|
333
|
-
array - value must validate against any item in list
|
|
334
|
-
|
|
335
|
-
NOTE: Cron months are 1-based, but RecurrenceRule months are 0-based.
|
|
336
|
-
*/
|
|
337
|
-
function RecurrenceRule(year, month, date, dayOfWeek, hour, minute, second) {
|
|
338
|
-
this.recurs = true;
|
|
339
|
-
|
|
340
|
-
this.year = (year == null) ? null : year;
|
|
341
|
-
this.month = (month == null) ? null : month;
|
|
342
|
-
this.date = (date == null) ? null : date;
|
|
343
|
-
this.dayOfWeek = (dayOfWeek == null) ? null : dayOfWeek;
|
|
344
|
-
this.hour = (hour == null) ? null : hour;
|
|
345
|
-
this.minute = (minute == null) ? null : minute;
|
|
346
|
-
this.second = (second == null) ? 0 : second;
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
RecurrenceRule.prototype.isValid = function() {
|
|
350
|
-
function isValidType(num) {
|
|
351
|
-
if (Array.isArray(num) || (num instanceof Array)) {
|
|
352
|
-
return num.every(function(e) {
|
|
353
|
-
return isValidType(e);
|
|
354
|
-
});
|
|
355
|
-
}
|
|
356
|
-
return !(Number.isNaN(Number(num)) && !(num instanceof Range));
|
|
357
|
-
}
|
|
358
|
-
if (this.month !== null && (this.month < 0 || this.month > 11 || !isValidType(this.month))) {
|
|
359
|
-
return false;
|
|
360
|
-
}
|
|
361
|
-
if (this.dayOfWeek !== null && (this.dayOfWeek < 0 || this.dayOfWeek > 6 || !isValidType(this.dayOfWeek))) {
|
|
362
|
-
return false;
|
|
363
|
-
}
|
|
364
|
-
if (this.hour !== null && (this.hour < 0 || this.hour > 23 || !isValidType(this.hour))) {
|
|
365
|
-
return false;
|
|
366
|
-
}
|
|
367
|
-
if (this.minute !== null && (this.minute < 0 || this.minute > 59 || !isValidType(this.minute))) {
|
|
368
|
-
return false;
|
|
369
|
-
}
|
|
370
|
-
if (this.second !== null && (this.second < 0 || this.second > 59 || !isValidType(this.second))) {
|
|
371
|
-
return false;
|
|
372
|
-
}
|
|
373
|
-
if (this.date !== null) {
|
|
374
|
-
if(!isValidType(this.date)) {
|
|
375
|
-
return false;
|
|
376
|
-
}
|
|
377
|
-
switch (this.month) {
|
|
378
|
-
case 3:
|
|
379
|
-
case 5:
|
|
380
|
-
case 8:
|
|
381
|
-
case 10:
|
|
382
|
-
if (this.date < 1 || this. date > 30) {
|
|
383
|
-
return false;
|
|
384
|
-
}
|
|
385
|
-
break;
|
|
386
|
-
case 1:
|
|
387
|
-
if (this.date < 1 || this. date > 29) {
|
|
388
|
-
return false;
|
|
389
|
-
}
|
|
390
|
-
break;
|
|
391
|
-
default:
|
|
392
|
-
if (this.date < 1 || this. date > 31) {
|
|
393
|
-
return false;
|
|
394
|
-
}
|
|
395
|
-
}
|
|
396
|
-
}
|
|
397
|
-
return true;
|
|
398
|
-
};
|
|
399
|
-
|
|
400
|
-
RecurrenceRule.prototype.nextInvocationDate = function(base) {
|
|
401
|
-
base = ((base instanceof CronDate) || (base instanceof Date)) ? base : (new Date());
|
|
402
|
-
if (!this.recurs) {
|
|
403
|
-
return null;
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
if(!this.isValid()) {
|
|
407
|
-
return null;
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
var now = new CronDate(Date.now(), this.tz);
|
|
411
|
-
var fullYear = now.getFullYear();
|
|
412
|
-
if ((this.year !== null) &&
|
|
413
|
-
(typeof this.year == 'number') &&
|
|
414
|
-
(this.year < fullYear)) {
|
|
415
|
-
return null;
|
|
416
|
-
}
|
|
417
|
-
|
|
418
|
-
var next = new CronDate(base.getTime(), this.tz);
|
|
419
|
-
next.addSecond();
|
|
420
|
-
|
|
421
|
-
while (true) {
|
|
422
|
-
if (this.year !== null) {
|
|
423
|
-
fullYear = next.getFullYear();
|
|
424
|
-
if ((typeof this.year == 'number') && (this.year < fullYear)) {
|
|
425
|
-
next = null;
|
|
426
|
-
break;
|
|
427
|
-
}
|
|
428
|
-
|
|
429
|
-
if (!recurMatch(fullYear, this.year)) {
|
|
430
|
-
next.addYear();
|
|
431
|
-
next.setMonth(0);
|
|
432
|
-
next.setDate(1);
|
|
433
|
-
next.setHours(0);
|
|
434
|
-
next.setMinutes(0);
|
|
435
|
-
next.setSeconds(0);
|
|
436
|
-
continue;
|
|
437
|
-
}
|
|
438
|
-
}
|
|
439
|
-
if (this.month != null && !recurMatch(next.getMonth(), this.month)) {
|
|
440
|
-
next.addMonth();
|
|
441
|
-
continue;
|
|
442
|
-
}
|
|
443
|
-
if (this.date != null && !recurMatch(next.getDate(), this.date)) {
|
|
444
|
-
next.addDay();
|
|
445
|
-
continue;
|
|
446
|
-
}
|
|
447
|
-
if (this.dayOfWeek != null && !recurMatch(next.getDay(), this.dayOfWeek)) {
|
|
448
|
-
next.addDay();
|
|
449
|
-
continue;
|
|
450
|
-
}
|
|
451
|
-
if (this.hour != null && !recurMatch(next.getHours(), this.hour)) {
|
|
452
|
-
next.addHour();
|
|
453
|
-
continue;
|
|
454
|
-
}
|
|
455
|
-
if (this.minute != null && !recurMatch(next.getMinutes(), this.minute)) {
|
|
456
|
-
next.addMinute();
|
|
457
|
-
continue;
|
|
458
|
-
}
|
|
459
|
-
if (this.second != null && !recurMatch(next.getSeconds(), this.second)) {
|
|
460
|
-
next.addSecond();
|
|
461
|
-
continue;
|
|
462
|
-
}
|
|
463
|
-
|
|
464
|
-
break;
|
|
465
|
-
}
|
|
466
|
-
|
|
467
|
-
return next ? next.toDate() : null;
|
|
468
|
-
};
|
|
469
|
-
|
|
470
|
-
function recurMatch(val, matcher) {
|
|
471
|
-
if (matcher == null) {
|
|
472
|
-
return true;
|
|
473
|
-
}
|
|
474
|
-
|
|
475
|
-
if (typeof matcher === 'number') {
|
|
476
|
-
return (val === matcher);
|
|
477
|
-
} else if(typeof matcher === 'string') {
|
|
478
|
-
return (val === Number(matcher));
|
|
479
|
-
} else if (matcher instanceof Range) {
|
|
480
|
-
return matcher.contains(val);
|
|
481
|
-
} else if (Array.isArray(matcher) || (matcher instanceof Array)) {
|
|
482
|
-
for (var i = 0; i < matcher.length; i++) {
|
|
483
|
-
if (recurMatch(val, matcher[i])) {
|
|
484
|
-
return true;
|
|
485
|
-
}
|
|
486
|
-
}
|
|
487
|
-
}
|
|
488
|
-
|
|
489
|
-
return false;
|
|
490
|
-
}
|
|
491
|
-
|
|
492
|
-
/* Date-based scheduler */
|
|
493
|
-
function runOnDate(date, job) {
|
|
494
|
-
var now = Date.now();
|
|
495
|
-
var then = date.getTime();
|
|
496
|
-
|
|
497
|
-
return lt.setTimeout(function() {
|
|
498
|
-
if (then > Date.now())
|
|
499
|
-
runOnDate(date, job);
|
|
500
|
-
else
|
|
501
|
-
job();
|
|
502
|
-
}, (then < now ? 0 : then - now));
|
|
503
|
-
}
|
|
504
|
-
|
|
505
|
-
var invocations = [];
|
|
506
|
-
var currentInvocation = null;
|
|
507
|
-
|
|
508
|
-
function scheduleInvocation(invocation) {
|
|
509
|
-
sorted.add(invocations, invocation, sorter);
|
|
510
|
-
prepareNextInvocation();
|
|
511
|
-
var date = invocation.fireDate instanceof CronDate ? invocation.fireDate.toDate() : invocation.fireDate;
|
|
512
|
-
invocation.job.emit('scheduled', date);
|
|
513
|
-
}
|
|
514
|
-
|
|
515
|
-
function prepareNextInvocation() {
|
|
516
|
-
if (invocations.length > 0 && currentInvocation !== invocations[0]) {
|
|
517
|
-
if (currentInvocation !== null) {
|
|
518
|
-
lt.clearTimeout(currentInvocation.timerID);
|
|
519
|
-
currentInvocation.timerID = null;
|
|
520
|
-
currentInvocation = null;
|
|
521
|
-
}
|
|
522
|
-
|
|
523
|
-
currentInvocation = invocations[0];
|
|
524
|
-
|
|
525
|
-
var job = currentInvocation.job;
|
|
526
|
-
var cinv = currentInvocation;
|
|
527
|
-
currentInvocation.timerID = runOnDate(currentInvocation.fireDate, function() {
|
|
528
|
-
currentInvocationFinished();
|
|
529
|
-
|
|
530
|
-
if (job.callback) {
|
|
531
|
-
job.callback();
|
|
532
|
-
}
|
|
533
|
-
|
|
534
|
-
if (cinv.recurrenceRule.recurs || cinv.recurrenceRule._endDate === null) {
|
|
535
|
-
var inv = scheduleNextRecurrence(cinv.recurrenceRule, cinv.job, cinv.fireDate, cinv.endDate);
|
|
536
|
-
if (inv !== null) {
|
|
537
|
-
inv.job.trackInvocation(inv);
|
|
538
|
-
}
|
|
539
|
-
}
|
|
540
|
-
|
|
541
|
-
job.stopTrackingInvocation(cinv);
|
|
542
|
-
|
|
543
|
-
job.invoke(cinv.fireDate instanceof CronDate ? cinv.fireDate.toDate() : cinv.fireDate);
|
|
544
|
-
job.emit('run');
|
|
545
|
-
});
|
|
546
|
-
}
|
|
547
|
-
}
|
|
548
|
-
|
|
549
|
-
function currentInvocationFinished() {
|
|
550
|
-
invocations.shift();
|
|
551
|
-
currentInvocation = null;
|
|
552
|
-
prepareNextInvocation();
|
|
553
|
-
}
|
|
554
|
-
|
|
555
|
-
function cancelInvocation(invocation) {
|
|
556
|
-
var idx = invocations.indexOf(invocation);
|
|
557
|
-
if (idx > -1) {
|
|
558
|
-
invocations.splice(idx, 1);
|
|
559
|
-
if (invocation.timerID !== null) {
|
|
560
|
-
lt.clearTimeout(invocation.timerID);
|
|
561
|
-
}
|
|
562
|
-
|
|
563
|
-
if (currentInvocation === invocation) {
|
|
564
|
-
currentInvocation = null;
|
|
565
|
-
}
|
|
566
|
-
|
|
567
|
-
invocation.job.emit('canceled', invocation.fireDate);
|
|
568
|
-
prepareNextInvocation();
|
|
569
|
-
}
|
|
570
|
-
}
|
|
571
|
-
|
|
572
|
-
/* Recurrence scheduler */
|
|
573
|
-
function scheduleNextRecurrence(rule, job, prevDate, endDate) {
|
|
574
|
-
|
|
575
|
-
prevDate = (prevDate instanceof CronDate) ? prevDate : new CronDate();
|
|
576
|
-
|
|
577
|
-
var date = (rule instanceof RecurrenceRule) ? rule.nextInvocationDate(prevDate) : rule.next();
|
|
578
|
-
if (date === null) {
|
|
579
|
-
return null;
|
|
580
|
-
}
|
|
581
|
-
|
|
582
|
-
if ((endDate instanceof CronDate) && date.getTime() > endDate.getTime()) {
|
|
583
|
-
return null;
|
|
584
|
-
}
|
|
585
|
-
|
|
586
|
-
var inv = new Invocation(job, date, rule, endDate);
|
|
587
|
-
scheduleInvocation(inv);
|
|
588
|
-
|
|
589
|
-
return inv;
|
|
590
|
-
}
|
|
591
|
-
|
|
592
22
|
/* Convenience methods */
|
|
593
23
|
function scheduleJob() {
|
|
594
24
|
if (arguments.length < 2) {
|
|
595
|
-
|
|
25
|
+
throw new RangeError('Invalid number of arguments');
|
|
596
26
|
}
|
|
597
27
|
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
28
|
+
const name = (arguments.length >= 3 && typeof arguments[0] === 'string') ? arguments[0] : null;
|
|
29
|
+
const spec = name ? arguments[1] : arguments[0];
|
|
30
|
+
const method = name ? arguments[2] : arguments[1];
|
|
31
|
+
const callback = name ? arguments[3] : arguments[2];
|
|
32
|
+
|
|
33
|
+
if (typeof method !== 'function') {
|
|
34
|
+
throw new RangeError('The job method must be a function.');
|
|
35
|
+
}
|
|
602
36
|
|
|
603
|
-
|
|
37
|
+
const job = new Job(name, method, callback);
|
|
604
38
|
|
|
605
39
|
if (job.schedule(spec)) {
|
|
606
40
|
return job;
|
|
@@ -614,18 +48,20 @@ function rescheduleJob(job, spec) {
|
|
|
614
48
|
if (job.reschedule(spec)) {
|
|
615
49
|
return job;
|
|
616
50
|
}
|
|
617
|
-
} else if (typeof job
|
|
618
|
-
if (
|
|
51
|
+
} else if (typeof job === 'string') {
|
|
52
|
+
if (scheduledJobs.hasOwnProperty(job)) {
|
|
619
53
|
if (scheduledJobs[job].reschedule(spec)) {
|
|
620
54
|
return scheduledJobs[job];
|
|
621
55
|
}
|
|
56
|
+
} else {
|
|
57
|
+
throw new Error('Cannot reschedule one-off job by name, pass job reference instead')
|
|
622
58
|
}
|
|
623
59
|
}
|
|
624
60
|
return null;
|
|
625
61
|
}
|
|
626
62
|
|
|
627
63
|
function cancelJob(job) {
|
|
628
|
-
|
|
64
|
+
let success = false;
|
|
629
65
|
if (job instanceof Job) {
|
|
630
66
|
success = job.cancel();
|
|
631
67
|
} else if (typeof job == 'string' || job instanceof String) {
|
|
@@ -638,11 +74,9 @@ function cancelJob(job) {
|
|
|
638
74
|
}
|
|
639
75
|
|
|
640
76
|
/* Public API */
|
|
641
|
-
module.exports
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
module.exports.scheduledJobs = scheduledJobs;
|
|
648
|
-
module.exports.cancelJob = cancelJob;
|
|
77
|
+
module.exports = {
|
|
78
|
+
scheduleJob,
|
|
79
|
+
rescheduleJob,
|
|
80
|
+
scheduledJobs,
|
|
81
|
+
cancelJob
|
|
82
|
+
}
|