cron 1.8.2 → 2.1.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/.editorconfig +5 -0
- package/.eslintrc +9 -11
- package/.github/ISSUE_TEMPLATE/bug_issue.yaml +26 -0
- package/.github/ISSUE_TEMPLATE/feature.yaml +31 -0
- package/.github/workflows/nodejs.yml +39 -0
- package/.prettierrc +5 -2
- package/CHANGELOG.md +7 -0
- package/README.md +127 -133
- package/examples/basic.js +1 -1
- package/lib/cron.js +16 -703
- package/lib/job.js +214 -0
- package/lib/time.js +513 -0
- package/package.json +75 -59
- package/tests/cron.test.js +180 -86
- package/tests/crontime.test.js +195 -124
- package/.travis.yml +0 -11
package/lib/cron.js
CHANGED
|
@@ -1,705 +1,25 @@
|
|
|
1
|
-
(function(root, factory) {
|
|
1
|
+
(function (root, factory) {
|
|
2
2
|
if (typeof define === 'function' && define.amd) {
|
|
3
|
-
define(['
|
|
3
|
+
define(['luxon'], factory);
|
|
4
4
|
} else if (typeof exports === 'object') {
|
|
5
|
-
module.exports = factory(
|
|
6
|
-
require('moment-timezone'),
|
|
7
|
-
require('child_process')
|
|
8
|
-
);
|
|
5
|
+
module.exports = factory(require('luxon'), require('child_process'));
|
|
9
6
|
} else {
|
|
10
|
-
root.Cron = factory(root.
|
|
7
|
+
root.Cron = factory(root.luxon);
|
|
11
8
|
}
|
|
12
|
-
})(this, function(
|
|
9
|
+
})(this, function (luxon, childProcess) {
|
|
13
10
|
var exports = {};
|
|
14
|
-
var timeUnits = [
|
|
15
|
-
'second',
|
|
16
|
-
'minute',
|
|
17
|
-
'hour',
|
|
18
|
-
'dayOfMonth',
|
|
19
|
-
'month',
|
|
20
|
-
'dayOfWeek'
|
|
21
|
-
];
|
|
22
11
|
var spawn = childProcess && childProcess.spawn;
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
this.source = source;
|
|
26
|
-
|
|
27
|
-
if (zone) {
|
|
28
|
-
if (moment.tz.names().indexOf(zone) === -1) {
|
|
29
|
-
throw new Error('Invalid timezone.');
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
this.zone = zone;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
if (typeof utcOffset !== 'undefined') this.utcOffset = utcOffset;
|
|
36
|
-
|
|
37
|
-
var that = this;
|
|
38
|
-
timeUnits.map(function(timeUnit) {
|
|
39
|
-
that[timeUnit] = {};
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
if (this.source instanceof Date || this.source._isAMomentObject) {
|
|
43
|
-
this.source = moment(this.source);
|
|
44
|
-
this.realDate = true;
|
|
45
|
-
} else {
|
|
46
|
-
this._parse();
|
|
47
|
-
this._verifyParse();
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
CronTime.constraints = [[0, 59], [0, 59], [0, 23], [1, 31], [0, 11], [0, 6]];
|
|
52
|
-
CronTime.monthConstraints = [
|
|
53
|
-
31,
|
|
54
|
-
29, // support leap year...not perfect
|
|
55
|
-
31,
|
|
56
|
-
30,
|
|
57
|
-
31,
|
|
58
|
-
30,
|
|
59
|
-
31,
|
|
60
|
-
31,
|
|
61
|
-
30,
|
|
62
|
-
31,
|
|
63
|
-
30,
|
|
64
|
-
31
|
|
65
|
-
];
|
|
66
|
-
CronTime.parseDefaults = ['0', '*', '*', '*', '*', '*'];
|
|
67
|
-
CronTime.aliases = {
|
|
68
|
-
jan: 0,
|
|
69
|
-
feb: 1,
|
|
70
|
-
mar: 2,
|
|
71
|
-
apr: 3,
|
|
72
|
-
may: 4,
|
|
73
|
-
jun: 5,
|
|
74
|
-
jul: 6,
|
|
75
|
-
aug: 7,
|
|
76
|
-
sep: 8,
|
|
77
|
-
oct: 9,
|
|
78
|
-
nov: 10,
|
|
79
|
-
dec: 11,
|
|
80
|
-
sun: 0,
|
|
81
|
-
mon: 1,
|
|
82
|
-
tue: 2,
|
|
83
|
-
wed: 3,
|
|
84
|
-
thu: 4,
|
|
85
|
-
fri: 5,
|
|
86
|
-
sat: 6
|
|
87
|
-
};
|
|
88
|
-
|
|
89
|
-
CronTime.prototype = {
|
|
90
|
-
_verifyParse: function() {
|
|
91
|
-
var months = Object.keys(this.month);
|
|
92
|
-
var ok = false;
|
|
93
|
-
|
|
94
|
-
/* if a dayOfMonth is not found in all months, we only need to fix the last
|
|
95
|
-
wrong month to prevent infinite loop */
|
|
96
|
-
var lastWrongMonth = NaN;
|
|
97
|
-
for (var i = 0; i < months.length; i++) {
|
|
98
|
-
var m = months[i];
|
|
99
|
-
var con = CronTime.monthConstraints[parseInt(m, 10)];
|
|
100
|
-
var dsom = Object.keys(this.dayOfMonth);
|
|
101
|
-
|
|
102
|
-
for (var j = 0; j < dsom.length; j++) {
|
|
103
|
-
var dom = dsom[j];
|
|
104
|
-
if (dom <= con) {
|
|
105
|
-
ok = true;
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
if (!ok) {
|
|
110
|
-
// save the month in order to be fixed if all months fails (infinite loop)
|
|
111
|
-
lastWrongMonth = m;
|
|
112
|
-
console.warn("Month '" + m + "' is limited to '" + con + "' days.");
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
// infinite loop detected (dayOfMonth is not found in all months)
|
|
117
|
-
if (!ok) {
|
|
118
|
-
var con = CronTime.monthConstraints[parseInt(lastWrongMonth, 10)];
|
|
119
|
-
var dsom = Object.keys(this.dayOfMonth);
|
|
120
|
-
for (var k = 0; k < dsom.length; k++) {
|
|
121
|
-
var dom = dsom[k];
|
|
122
|
-
if (dom > con) {
|
|
123
|
-
delete this.dayOfMonth[dom];
|
|
124
|
-
var fixedDay = Number(dom) % con;
|
|
125
|
-
this.dayOfMonth[fixedDay] = true;
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
},
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* calculates the next send time
|
|
133
|
-
*/
|
|
134
|
-
sendAt: function(i) {
|
|
135
|
-
var date = this.realDate ? this.source : moment();
|
|
136
|
-
// Set the timezone if given (http://momentjs.com/timezone/docs/#/using-timezones/parsing-in-zone/)
|
|
137
|
-
if (this.zone) {
|
|
138
|
-
date = date.tz(this.zone);
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
if (typeof this.utcOffset !== 'undefined') {
|
|
142
|
-
date = date.utcOffset(this.utcOffset);
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
if (this.realDate) {
|
|
146
|
-
const diff = moment().diff(date, 's');
|
|
147
|
-
if (diff > 0) {
|
|
148
|
-
throw new Error('WARNING: Date in past. Will never be fired.');
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
return date;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
// If the i argument is not given, return the next send time
|
|
155
|
-
if (isNaN(i) || i < 0) {
|
|
156
|
-
date = this._getNextDateFrom(date);
|
|
157
|
-
|
|
158
|
-
return date;
|
|
159
|
-
} else {
|
|
160
|
-
// Else return the next i send times
|
|
161
|
-
var dates = [];
|
|
162
|
-
for (; i > 0; i--) {
|
|
163
|
-
date = this._getNextDateFrom(date);
|
|
164
|
-
dates.push(moment(date));
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
return dates;
|
|
168
|
-
}
|
|
169
|
-
},
|
|
170
|
-
|
|
171
|
-
/**
|
|
172
|
-
* Get the number of milliseconds in the future at which to fire our callbacks.
|
|
173
|
-
*/
|
|
174
|
-
getTimeout: function() {
|
|
175
|
-
return Math.max(-1, this.sendAt() - moment());
|
|
176
|
-
},
|
|
177
|
-
|
|
178
|
-
/**
|
|
179
|
-
* writes out a cron string
|
|
180
|
-
*/
|
|
181
|
-
toString: function() {
|
|
182
|
-
return this.toJSON().join(' ');
|
|
183
|
-
},
|
|
184
|
-
|
|
185
|
-
/**
|
|
186
|
-
* Json representation of the parsed cron syntax.
|
|
187
|
-
*/
|
|
188
|
-
toJSON: function() {
|
|
189
|
-
var self = this;
|
|
190
|
-
return timeUnits.map(function(timeName) {
|
|
191
|
-
return self._wcOrAll(timeName);
|
|
192
|
-
});
|
|
193
|
-
},
|
|
194
|
-
|
|
195
|
-
/**
|
|
196
|
-
* get next date that matches parsed cron time
|
|
197
|
-
*/
|
|
198
|
-
_getNextDateFrom: function(start, zone) {
|
|
199
|
-
var date;
|
|
200
|
-
var firstDate = moment(start).valueOf();
|
|
201
|
-
if (zone) {
|
|
202
|
-
date = moment(start).tz(zone);
|
|
203
|
-
} else {
|
|
204
|
-
date = moment(start);
|
|
205
|
-
}
|
|
206
|
-
if (!this.realDate) {
|
|
207
|
-
const milliseconds =
|
|
208
|
-
(start.milliseconds && start.milliseconds()) ||
|
|
209
|
-
(start.getMilliseconds && start.getMilliseconds()) ||
|
|
210
|
-
0;
|
|
211
|
-
if (milliseconds > 0) {
|
|
212
|
-
date.milliseconds(0);
|
|
213
|
-
date.seconds(date.seconds() + 1);
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
if (date.toString() === 'Invalid date') {
|
|
218
|
-
throw new Error('ERROR: You specified an invalid date.');
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
// it shouldn't take more than 5 seconds to find the next execution time
|
|
222
|
-
// being very generous with this. Throw error if it takes too long to find the next time to protect from
|
|
223
|
-
// infinite loop.
|
|
224
|
-
var timeout = Date.now() + 5000;
|
|
225
|
-
// determine next date
|
|
226
|
-
while (true) {
|
|
227
|
-
var diff = date - start;
|
|
228
|
-
var prevMonth = date.month();
|
|
229
|
-
var prevDay = date.days();
|
|
230
|
-
var prevMinute = date.minutes();
|
|
231
|
-
var prevSeconds = date.seconds();
|
|
232
|
-
var origDate = new Date(date);
|
|
233
|
-
|
|
234
|
-
if (Date.now() > timeout) {
|
|
235
|
-
throw new Error(
|
|
236
|
-
`Something went wrong. cron reached maximum iterations.
|
|
237
|
-
Please open an issue (https://github.com/kelektiv/node-cron/issues/new) and provide the following string
|
|
238
|
-
Time Zone: ${zone || '""'} - Cron String: ${this} - UTC offset: ${date.format(
|
|
239
|
-
'Z'
|
|
240
|
-
)} - current Date: ${moment().toString()}`
|
|
241
|
-
);
|
|
242
|
-
}
|
|
243
|
-
if (
|
|
244
|
-
!(date.month() in this.month) &&
|
|
245
|
-
Object.keys(this.month).length !== 12
|
|
246
|
-
) {
|
|
247
|
-
date.add(1, 'M');
|
|
248
|
-
if (date.month() === prevMonth) {
|
|
249
|
-
date.add(1, 'M');
|
|
250
|
-
}
|
|
251
|
-
date.date(1);
|
|
252
|
-
date.hours(0);
|
|
253
|
-
date.minutes(0);
|
|
254
|
-
date.seconds(0);
|
|
255
|
-
continue;
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
if (
|
|
259
|
-
!(date.date() in this.dayOfMonth) &&
|
|
260
|
-
Object.keys(this.dayOfMonth).length !== 31 &&
|
|
261
|
-
!(
|
|
262
|
-
date.day() in this.dayOfWeek &&
|
|
263
|
-
Object.keys(this.dayOfWeek).length !== 7
|
|
264
|
-
)
|
|
265
|
-
) {
|
|
266
|
-
date.add(1, 'd');
|
|
267
|
-
if (date.days() === prevDay) {
|
|
268
|
-
date.add(1, 'd');
|
|
269
|
-
}
|
|
270
|
-
date.hours(0);
|
|
271
|
-
date.minutes(0);
|
|
272
|
-
date.seconds(0);
|
|
273
|
-
continue;
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
if (
|
|
277
|
-
!(date.day() in this.dayOfWeek) &&
|
|
278
|
-
Object.keys(this.dayOfWeek).length !== 7 &&
|
|
279
|
-
!(
|
|
280
|
-
date.date() in this.dayOfMonth &&
|
|
281
|
-
Object.keys(this.dayOfMonth).length !== 31
|
|
282
|
-
)
|
|
283
|
-
) {
|
|
284
|
-
date.add(1, 'd');
|
|
285
|
-
if (date.days() === prevDay) {
|
|
286
|
-
date.add(1, 'd');
|
|
287
|
-
}
|
|
288
|
-
date.hours(0);
|
|
289
|
-
date.minutes(0);
|
|
290
|
-
date.seconds(0);
|
|
291
|
-
if (date <= origDate) {
|
|
292
|
-
date = this._findDST(origDate);
|
|
293
|
-
}
|
|
294
|
-
continue;
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
if (
|
|
298
|
-
!(date.hours() in this.hour) &&
|
|
299
|
-
Object.keys(this.hour).length !== 24
|
|
300
|
-
) {
|
|
301
|
-
origDate = moment(date);
|
|
302
|
-
var curHour = date.hours();
|
|
303
|
-
date.hours(
|
|
304
|
-
date.hours() === 23 && diff > 86400000 ? 0 : date.hours() + 1
|
|
305
|
-
);
|
|
306
|
-
/*
|
|
307
|
-
* Moment Date will not allow you to set the time to 2 AM if there is no 2 AM (on the day we change the clock)
|
|
308
|
-
* We will therefore jump to 3AM if time stayed at 1AM
|
|
309
|
-
*/
|
|
310
|
-
if (curHour === date.hours()) {
|
|
311
|
-
date.hours(date.hours() + 2);
|
|
312
|
-
}
|
|
313
|
-
date.minutes(0);
|
|
314
|
-
date.seconds(0);
|
|
315
|
-
if (date <= origDate) {
|
|
316
|
-
date = this._findDST(origDate);
|
|
317
|
-
}
|
|
318
|
-
continue;
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
if (
|
|
322
|
-
!(date.minutes() in this.minute) &&
|
|
323
|
-
Object.keys(this.minute).length !== 60
|
|
324
|
-
) {
|
|
325
|
-
origDate = moment(date);
|
|
326
|
-
date.minutes(
|
|
327
|
-
date.minutes() === 59 && diff > 60 * 60 * 1000
|
|
328
|
-
? 0
|
|
329
|
-
: date.minutes() + 1
|
|
330
|
-
);
|
|
331
|
-
date.seconds(0);
|
|
332
|
-
if (date <= origDate) {
|
|
333
|
-
date = this._findDST(origDate);
|
|
334
|
-
}
|
|
335
|
-
continue;
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
if (
|
|
339
|
-
!(date.seconds() in this.second) &&
|
|
340
|
-
Object.keys(this.second).length !== 60
|
|
341
|
-
) {
|
|
342
|
-
origDate = moment(date);
|
|
343
|
-
date.seconds(
|
|
344
|
-
date.seconds() === 59 && diff > 60 * 1000 ? 0 : date.seconds() + 1
|
|
345
|
-
);
|
|
346
|
-
if (date <= origDate) {
|
|
347
|
-
date = this._findDST(origDate);
|
|
348
|
-
}
|
|
349
|
-
continue;
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
if (date.valueOf() === firstDate) {
|
|
353
|
-
date.seconds(date.seconds() + 1);
|
|
354
|
-
continue;
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
break;
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
return date;
|
|
361
|
-
},
|
|
362
|
-
|
|
363
|
-
/**
|
|
364
|
-
* get next date that is a valid DST date
|
|
365
|
-
*/
|
|
366
|
-
_findDST: function(date) {
|
|
367
|
-
var newDate = moment(date);
|
|
368
|
-
while (newDate <= date) {
|
|
369
|
-
// eslint seems to trigger here, it is wrong
|
|
370
|
-
newDate.add(1, 's');
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
return newDate;
|
|
374
|
-
},
|
|
375
|
-
|
|
376
|
-
/**
|
|
377
|
-
* wildcard, or all params in array (for to string)
|
|
378
|
-
*/
|
|
379
|
-
_wcOrAll: function(type) {
|
|
380
|
-
if (this._hasAll(type)) return '*';
|
|
381
|
-
|
|
382
|
-
var all = [];
|
|
383
|
-
for (var time in this[type]) {
|
|
384
|
-
all.push(time);
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
return all.join(',');
|
|
388
|
-
},
|
|
389
|
-
|
|
390
|
-
_hasAll: function(type) {
|
|
391
|
-
var constrain = CronTime.constraints[timeUnits.indexOf(type)];
|
|
392
|
-
|
|
393
|
-
for (var i = constrain[0], n = constrain[1]; i < n; i++) {
|
|
394
|
-
if (!(i in this[type])) return false;
|
|
395
|
-
}
|
|
396
|
-
|
|
397
|
-
return true;
|
|
398
|
-
},
|
|
399
|
-
|
|
400
|
-
_parse: function() {
|
|
401
|
-
var aliases = CronTime.aliases;
|
|
402
|
-
var source = this.source.replace(/[a-z]{1,3}/gi, function(alias) {
|
|
403
|
-
alias = alias.toLowerCase();
|
|
404
|
-
|
|
405
|
-
if (alias in aliases) {
|
|
406
|
-
return aliases[alias];
|
|
407
|
-
}
|
|
408
|
-
|
|
409
|
-
throw new Error('Unknown alias: ' + alias);
|
|
410
|
-
});
|
|
411
|
-
var split = source.replace(/^\s\s*|\s\s*$/g, '').split(/\s+/);
|
|
412
|
-
var cur;
|
|
413
|
-
var i = 0;
|
|
414
|
-
var len = timeUnits.length;
|
|
415
|
-
|
|
416
|
-
// seconds are optional
|
|
417
|
-
if (split.length < timeUnits.length - 1) {
|
|
418
|
-
throw new Error('Too few fields');
|
|
419
|
-
}
|
|
420
|
-
if (split.length > timeUnits.length) {
|
|
421
|
-
throw new Error('Too many fields');
|
|
422
|
-
}
|
|
423
|
-
|
|
424
|
-
for (; i < timeUnits.length; i++) {
|
|
425
|
-
// If the split source string doesn't contain all digits,
|
|
426
|
-
// assume defaults for first n missing digits.
|
|
427
|
-
// This adds support for 5-digit standard cron syntax
|
|
428
|
-
cur = split[i - (len - split.length)] || CronTime.parseDefaults[i];
|
|
429
|
-
this._parseField(cur, timeUnits[i], CronTime.constraints[i]);
|
|
430
|
-
}
|
|
431
|
-
},
|
|
432
|
-
|
|
433
|
-
_parseField: function(field, type, constraints) {
|
|
434
|
-
var rangePattern = /^(\d+)(?:-(\d+))?(?:\/(\d+))?$/g;
|
|
435
|
-
var typeObj = this[type];
|
|
436
|
-
var pointer;
|
|
437
|
-
var low = constraints[0];
|
|
438
|
-
var high = constraints[1];
|
|
439
|
-
|
|
440
|
-
var fields = field.split(',');
|
|
441
|
-
fields.forEach(function(field) {
|
|
442
|
-
var wildcardIndex = field.indexOf('*');
|
|
443
|
-
if (wildcardIndex !== -1 && wildcardIndex !== 0) {
|
|
444
|
-
throw new Error('Field (' + field + ') has an invalid wildcard expression');
|
|
445
|
-
}
|
|
446
|
-
});
|
|
447
|
-
|
|
448
|
-
// * is a shortcut to [lower-upper] range
|
|
449
|
-
field = field.replace(/\*/g, low + '-' + high);
|
|
450
|
-
|
|
451
|
-
// commas separate information, so split based on those
|
|
452
|
-
var allRanges = field.split(',');
|
|
453
|
-
|
|
454
|
-
for (var i = 0; i < allRanges.length; i++) {
|
|
455
|
-
if (allRanges[i].match(rangePattern)) {
|
|
456
|
-
allRanges[i].replace(rangePattern, function($0, lower, upper, step) {
|
|
457
|
-
lower = parseInt(lower, 10);
|
|
458
|
-
upper = parseInt(upper, 10) || undefined;
|
|
459
|
-
|
|
460
|
-
const wasStepDefined = !isNaN(parseInt(step, 10));
|
|
461
|
-
if (step === '0') {
|
|
462
|
-
throw new Error('Field (' + field + ') has a step of zero');
|
|
463
|
-
}
|
|
464
|
-
step = parseInt(step, 10) || 1;
|
|
465
|
-
|
|
466
|
-
if (upper && lower > upper) {
|
|
467
|
-
throw new Error('Field (' + field + ') has an invalid range');
|
|
468
|
-
}
|
|
469
|
-
|
|
470
|
-
const outOfRangeError =
|
|
471
|
-
lower < low ||
|
|
472
|
-
(upper && upper > high) ||
|
|
473
|
-
(!upper && lower > high);
|
|
474
|
-
|
|
475
|
-
if (outOfRangeError) {
|
|
476
|
-
throw new Error('Field (' + field + ') value is out of range');
|
|
477
|
-
}
|
|
478
|
-
|
|
479
|
-
// Positive integer higher than constraints[0]
|
|
480
|
-
lower = Math.min(Math.max(low, ~~Math.abs(lower)), high);
|
|
481
|
-
|
|
482
|
-
// Positive integer lower than constraints[1]
|
|
483
|
-
if (upper) {
|
|
484
|
-
upper = Math.min(high, ~~Math.abs(upper));
|
|
485
|
-
} else {
|
|
486
|
-
// If step is provided, the default upper range is the highest value
|
|
487
|
-
upper = wasStepDefined ? high : lower;
|
|
488
|
-
}
|
|
489
|
-
|
|
490
|
-
// Count from the lower barrier to the upper
|
|
491
|
-
pointer = lower;
|
|
492
|
-
|
|
493
|
-
do {
|
|
494
|
-
typeObj[pointer] = true;
|
|
495
|
-
pointer += step;
|
|
496
|
-
} while (pointer <= upper);
|
|
497
|
-
});
|
|
498
|
-
} else {
|
|
499
|
-
throw new Error('Field (' + field + ') cannot be parsed');
|
|
500
|
-
}
|
|
501
|
-
}
|
|
502
|
-
}
|
|
503
|
-
};
|
|
504
|
-
|
|
505
|
-
function command2function(cmd) {
|
|
506
|
-
var command;
|
|
507
|
-
var args;
|
|
508
|
-
|
|
509
|
-
switch (typeof cmd) {
|
|
510
|
-
case 'string':
|
|
511
|
-
args = cmd.split(' ');
|
|
512
|
-
command = args.shift();
|
|
513
|
-
|
|
514
|
-
cmd = spawn.bind(undefined, command, args);
|
|
515
|
-
break;
|
|
516
|
-
|
|
517
|
-
case 'object':
|
|
518
|
-
command = cmd && cmd.command;
|
|
519
|
-
if (command) {
|
|
520
|
-
args = cmd.args;
|
|
521
|
-
var options = cmd.options;
|
|
522
|
-
|
|
523
|
-
cmd = spawn.bind(undefined, command, args, options);
|
|
524
|
-
}
|
|
525
|
-
break;
|
|
526
|
-
}
|
|
527
|
-
|
|
528
|
-
return cmd;
|
|
529
|
-
}
|
|
530
|
-
|
|
531
|
-
function CronJob(
|
|
532
|
-
cronTime,
|
|
533
|
-
onTick,
|
|
534
|
-
onComplete,
|
|
535
|
-
startNow,
|
|
536
|
-
timeZone,
|
|
537
|
-
context,
|
|
538
|
-
runOnInit,
|
|
539
|
-
utcOffset,
|
|
540
|
-
unrefTimeout
|
|
541
|
-
) {
|
|
542
|
-
var _cronTime = cronTime;
|
|
543
|
-
var argCount = 0;
|
|
544
|
-
for (var i = 0; i < arguments.length; i++) {
|
|
545
|
-
if (arguments[i] !== undefined) {
|
|
546
|
-
argCount++;
|
|
547
|
-
}
|
|
548
|
-
}
|
|
549
|
-
if (typeof cronTime !== 'string' && argCount === 1) {
|
|
550
|
-
// crontime is an object...
|
|
551
|
-
onTick = cronTime.onTick;
|
|
552
|
-
onComplete = cronTime.onComplete;
|
|
553
|
-
context = cronTime.context;
|
|
554
|
-
startNow = cronTime.start || cronTime.startNow || cronTime.startJob;
|
|
555
|
-
timeZone = cronTime.timeZone;
|
|
556
|
-
runOnInit = cronTime.runOnInit;
|
|
557
|
-
_cronTime = cronTime.cronTime;
|
|
558
|
-
utcOffset = cronTime.utcOffset;
|
|
559
|
-
unrefTimeout = cronTime.unrefTimeout;
|
|
560
|
-
}
|
|
561
|
-
|
|
562
|
-
this.context = context || this;
|
|
563
|
-
this._callbacks = [];
|
|
564
|
-
this.onComplete = command2function(onComplete);
|
|
565
|
-
this.cronTime = new CronTime(_cronTime, timeZone, utcOffset);
|
|
566
|
-
this.unrefTimeout = unrefTimeout;
|
|
567
|
-
|
|
568
|
-
addCallback.call(this, command2function(onTick));
|
|
569
|
-
|
|
570
|
-
if (runOnInit) {
|
|
571
|
-
this.lastExecution = new Date();
|
|
572
|
-
fireOnTick.call(this);
|
|
573
|
-
}
|
|
574
|
-
|
|
575
|
-
if (startNow) {
|
|
576
|
-
start.call(this);
|
|
577
|
-
}
|
|
578
|
-
|
|
579
|
-
return this;
|
|
580
|
-
}
|
|
581
|
-
|
|
582
|
-
var addCallback = function(callback) {
|
|
583
|
-
if (typeof callback === 'function') this._callbacks.push(callback);
|
|
584
|
-
};
|
|
585
|
-
CronJob.prototype.addCallback = addCallback;
|
|
586
|
-
|
|
587
|
-
CronJob.prototype.setTime = function(time) {
|
|
588
|
-
if (!(time instanceof CronTime))
|
|
589
|
-
throw new Error('time must be an instance of CronTime.');
|
|
590
|
-
this.stop();
|
|
591
|
-
this.cronTime = time;
|
|
592
|
-
};
|
|
593
|
-
|
|
594
|
-
CronJob.prototype.nextDate = function() {
|
|
595
|
-
return this.cronTime.sendAt();
|
|
596
|
-
};
|
|
597
|
-
|
|
598
|
-
var fireOnTick = function() {
|
|
599
|
-
for (var i = this._callbacks.length - 1; i >= 0; i--)
|
|
600
|
-
this._callbacks[i].call(this.context, this.onComplete);
|
|
601
|
-
};
|
|
602
|
-
CronJob.prototype.fireOnTick = fireOnTick;
|
|
603
|
-
|
|
604
|
-
CronJob.prototype.nextDates = function(i) {
|
|
605
|
-
return this.cronTime.sendAt(i);
|
|
606
|
-
};
|
|
607
|
-
|
|
608
|
-
var start = function() {
|
|
609
|
-
if (this.running) return;
|
|
610
|
-
|
|
611
|
-
var MAXDELAY = 2147483647; // The maximum number of milliseconds setTimeout will wait.
|
|
612
|
-
var self = this;
|
|
613
|
-
var timeout = this.cronTime.getTimeout();
|
|
614
|
-
var remaining = 0;
|
|
615
|
-
var startTime;
|
|
616
|
-
|
|
617
|
-
if (this.cronTime.realDate) this.runOnce = true;
|
|
618
|
-
|
|
619
|
-
function _setTimeout(timeout) {
|
|
620
|
-
startTime = Date.now();
|
|
621
|
-
self._timeout = setTimeout(callbackWrapper, timeout);
|
|
622
|
-
if (self.unrefTimeout && typeof self._timeout.unref === 'function') {
|
|
623
|
-
self._timeout.unref();
|
|
624
|
-
}
|
|
625
|
-
}
|
|
626
|
-
|
|
627
|
-
// The callback wrapper checks if it needs to sleep another period or not
|
|
628
|
-
// and does the real callback logic when it's time.
|
|
629
|
-
|
|
630
|
-
function callbackWrapper() {
|
|
631
|
-
var diff = startTime + timeout - Date.now();
|
|
632
|
-
|
|
633
|
-
if (diff > 0) {
|
|
634
|
-
var newTimeout = self.cronTime.getTimeout();
|
|
635
|
-
|
|
636
|
-
if (newTimeout > diff) {
|
|
637
|
-
newTimeout = diff;
|
|
638
|
-
}
|
|
639
|
-
|
|
640
|
-
remaining += newTimeout;
|
|
641
|
-
}
|
|
642
|
-
|
|
643
|
-
// If there is sleep time remaining, calculate how long and go to sleep
|
|
644
|
-
// again. This processing might make us miss the deadline by a few ms
|
|
645
|
-
// times the number of sleep sessions. Given a MAXDELAY of almost a
|
|
646
|
-
// month, this should be no issue.
|
|
647
|
-
|
|
648
|
-
self.lastExecution = new Date();
|
|
649
|
-
if (remaining) {
|
|
650
|
-
if (remaining > MAXDELAY) {
|
|
651
|
-
remaining -= MAXDELAY;
|
|
652
|
-
timeout = MAXDELAY;
|
|
653
|
-
} else {
|
|
654
|
-
timeout = remaining;
|
|
655
|
-
remaining = 0;
|
|
656
|
-
}
|
|
657
|
-
|
|
658
|
-
_setTimeout(timeout);
|
|
659
|
-
} else {
|
|
660
|
-
// We have arrived at the correct point in time.
|
|
661
|
-
|
|
662
|
-
self.running = false;
|
|
663
|
-
|
|
664
|
-
// start before calling back so the callbacks have the ability to stop the cron job
|
|
665
|
-
if (!self.runOnce) self.start();
|
|
666
|
-
|
|
667
|
-
self.fireOnTick();
|
|
668
|
-
}
|
|
669
|
-
}
|
|
670
|
-
|
|
671
|
-
if (timeout >= 0) {
|
|
672
|
-
this.running = true;
|
|
673
|
-
|
|
674
|
-
// Don't try to sleep more than MAXDELAY ms at a time.
|
|
675
|
-
|
|
676
|
-
if (timeout > MAXDELAY) {
|
|
677
|
-
remaining = timeout - MAXDELAY;
|
|
678
|
-
timeout = MAXDELAY;
|
|
679
|
-
}
|
|
680
|
-
|
|
681
|
-
_setTimeout(timeout);
|
|
682
|
-
} else {
|
|
683
|
-
this.stop();
|
|
684
|
-
}
|
|
685
|
-
};
|
|
686
|
-
|
|
687
|
-
CronJob.prototype.start = start;
|
|
688
|
-
|
|
689
|
-
CronJob.prototype.lastDate = function() {
|
|
690
|
-
return this.lastExecution;
|
|
691
|
-
};
|
|
12
|
+
const CronTime = require('./time')(luxon);
|
|
13
|
+
const CronJob = require('./job')(CronTime, spawn);
|
|
692
14
|
|
|
693
15
|
/**
|
|
694
|
-
*
|
|
16
|
+
* Extend Luxon DateTime
|
|
695
17
|
*/
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
this.running = false;
|
|
699
|
-
if (typeof this.onComplete === 'function') this.onComplete();
|
|
18
|
+
luxon.DateTime.prototype.getWeekDay = function () {
|
|
19
|
+
return this.weekday === 7 ? 0 : this.weekday;
|
|
700
20
|
};
|
|
701
21
|
|
|
702
|
-
exports.job =
|
|
22
|
+
exports.job = (
|
|
703
23
|
cronTime,
|
|
704
24
|
onTick,
|
|
705
25
|
onComplete,
|
|
@@ -709,8 +29,8 @@
|
|
|
709
29
|
runOnInit,
|
|
710
30
|
utcOffset,
|
|
711
31
|
unrefTimeout
|
|
712
|
-
)
|
|
713
|
-
|
|
32
|
+
) =>
|
|
33
|
+
new CronJob(
|
|
714
34
|
cronTime,
|
|
715
35
|
onTick,
|
|
716
36
|
onComplete,
|
|
@@ -721,19 +41,12 @@
|
|
|
721
41
|
utcOffset,
|
|
722
42
|
unrefTimeout
|
|
723
43
|
);
|
|
724
|
-
};
|
|
725
44
|
|
|
726
|
-
exports.time =
|
|
727
|
-
return new CronTime(cronTime, timeZone);
|
|
728
|
-
};
|
|
45
|
+
exports.time = (cronTime, timeZone) => new CronTime(cronTime, timeZone);
|
|
729
46
|
|
|
730
|
-
exports.sendAt =
|
|
731
|
-
return exports.time(cronTime).sendAt();
|
|
732
|
-
};
|
|
47
|
+
exports.sendAt = cronTime => exports.time(cronTime).sendAt();
|
|
733
48
|
|
|
734
|
-
exports.timeout =
|
|
735
|
-
return exports.time(cronTime).getTimeout();
|
|
736
|
-
};
|
|
49
|
+
exports.timeout = cronTime => exports.time(cronTime).getTimeout();
|
|
737
50
|
|
|
738
51
|
exports.CronJob = CronJob;
|
|
739
52
|
exports.CronTime = CronTime;
|