cron 1.0.5 → 1.0.9
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/.travis.yml +1 -2
- package/README.md +0 -4
- package/lib/cron.js +451 -424
- package/package.json +7 -3
- package/test.js +37 -20
- package/tests/test-cron.js +445 -397
- package/tests/test-crontime.js +51 -7
package/lib/cron.js
CHANGED
|
@@ -1,451 +1,478 @@
|
|
|
1
|
-
var
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
try {
|
|
7
|
-
CronDate = require("time").Date;
|
|
8
|
-
exports = module.exports;
|
|
9
|
-
loadedTimeModule = true;
|
|
10
|
-
} catch (e) {
|
|
11
|
-
CronDate = Date;
|
|
12
|
-
}
|
|
1
|
+
var exports,
|
|
2
|
+
timeUnits = ['second', 'minute', 'hour', 'dayOfMonth', 'month', 'dayOfWeek'],
|
|
3
|
+
spawn = require('child_process').spawn,
|
|
4
|
+
moment = require('moment-timezone');
|
|
13
5
|
|
|
14
6
|
function CronTime(source, zone) {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
7
|
+
this.source = source;
|
|
8
|
+
this.zone = zone;
|
|
9
|
+
|
|
10
|
+
var that = this;
|
|
11
|
+
timeUnits.map(function(timeUnit){
|
|
12
|
+
that[timeUnit] = {};
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
if (this.source instanceof Date) {
|
|
16
|
+
this.source = moment(this.source);
|
|
17
|
+
this.realDate = true;
|
|
18
|
+
} else {
|
|
19
|
+
this._parse();
|
|
20
|
+
if (!this._verifyParse())
|
|
21
|
+
throw Error("Could not verify a valid date. Please verify your parameters.");
|
|
22
|
+
}
|
|
29
23
|
}
|
|
30
24
|
|
|
31
25
|
CronTime.constraints = [
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
];
|
|
39
|
-
CronTime.
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
26
|
+
[0, 59],
|
|
27
|
+
[0, 59],
|
|
28
|
+
[0, 23],
|
|
29
|
+
[1, 31],
|
|
30
|
+
[0, 11],
|
|
31
|
+
[0, 6]
|
|
32
|
+
];
|
|
33
|
+
CronTime.monthConstraints = [
|
|
34
|
+
31,
|
|
35
|
+
29, //support leap year...not perfect
|
|
36
|
+
31,
|
|
37
|
+
30,
|
|
38
|
+
31,
|
|
39
|
+
30,
|
|
40
|
+
31,
|
|
41
|
+
31,
|
|
42
|
+
30,
|
|
43
|
+
31,
|
|
44
|
+
30,
|
|
45
|
+
31
|
|
46
|
+
];
|
|
47
|
+
CronTime.parseDefaults = ['0', '*', '*', '*', '*', '*'];
|
|
48
|
+
CronTime.aliases = {
|
|
49
|
+
jan: 0,
|
|
50
|
+
feb: 1,
|
|
51
|
+
mar: 2,
|
|
52
|
+
apr: 3,
|
|
53
|
+
may: 4,
|
|
54
|
+
jun: 5,
|
|
55
|
+
jul: 6,
|
|
56
|
+
aug: 7,
|
|
57
|
+
sep: 8,
|
|
58
|
+
oct: 9,
|
|
59
|
+
nov: 10,
|
|
60
|
+
dec: 11,
|
|
61
|
+
sun: 0,
|
|
62
|
+
mon: 1,
|
|
63
|
+
tue: 2,
|
|
64
|
+
wed: 3,
|
|
65
|
+
thu: 4,
|
|
66
|
+
fri: 5,
|
|
67
|
+
sat: 6
|
|
68
|
+
};
|
|
61
69
|
|
|
62
70
|
|
|
63
71
|
CronTime.prototype = {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
72
|
+
_verifyParse: function() {
|
|
73
|
+
var months = Object.keys(this.month);
|
|
74
|
+
for (var i = 0; i < months.length; i++) {
|
|
75
|
+
var m = months[i];
|
|
76
|
+
var con = CronTime.monthConstraints[parseInt(m, 10)];
|
|
77
|
+
var ok = false;
|
|
78
|
+
var dsom = Object.keys(this.dayOfMonth);
|
|
79
|
+
for (var j = 0; j < dsom.length; j++) {
|
|
80
|
+
var dom = dsom[j];
|
|
81
|
+
if (dom <= con)
|
|
82
|
+
ok = true;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (!ok) {
|
|
86
|
+
console.error("Month '" + m + "' is limited to '" + con + "' days.");
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return true;
|
|
92
|
+
},
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* calculates the next send time
|
|
96
|
+
*/
|
|
97
|
+
sendAt: function() {
|
|
98
|
+
var date = this.realDate ? this.source : moment();
|
|
99
|
+
// Set the timezone if given (http://momentjs.com/timezone/docs/#/using-timezones/parsing-in-zone/)
|
|
100
|
+
if (this.zone)
|
|
101
|
+
date = date.tz(this.zone);
|
|
102
|
+
|
|
103
|
+
if (this.realDate)
|
|
104
|
+
return date;
|
|
105
|
+
|
|
106
|
+
//add 1 second so next time isn't now (can cause timeout to be 0 or negative number)
|
|
107
|
+
var now = new Date();
|
|
108
|
+
var targetSecond = date.seconds();
|
|
109
|
+
var diff = Math.abs(targetSecond - now.getSeconds())
|
|
110
|
+
// there was a problem when `date` is 1424777177999 and `now` is 1424777178000
|
|
111
|
+
// 1 ms diff but this is another second...
|
|
112
|
+
if ( diff == 0 || (diff == 1 && now.getMilliseconds() <= date.milliseconds() ) ) {
|
|
113
|
+
//console.log('add 1 second?');
|
|
114
|
+
date = date.add(1, 's');
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
date = this._getNextDateFrom(date);
|
|
118
|
+
|
|
119
|
+
return date;
|
|
120
|
+
},
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Get the number of milliseconds in the future at which to fire our callbacks.
|
|
124
|
+
*/
|
|
125
|
+
getTimeout: function() {
|
|
126
|
+
return Math.max(-1, this.sendAt() - moment());
|
|
127
|
+
},
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* writes out a cron string
|
|
131
|
+
*/
|
|
132
|
+
toString: function() {
|
|
133
|
+
return this.toJSON().join(' ');
|
|
134
|
+
},
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Json representation of the parsed cron syntax.
|
|
138
|
+
*/
|
|
139
|
+
toJSON: function() {
|
|
140
|
+
var self = this;
|
|
141
|
+
return timeUnits.map(function(timeName){
|
|
142
|
+
return self._wcOrAll(timeName);
|
|
143
|
+
});
|
|
144
|
+
},
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* get next date that matches parsed cron time
|
|
148
|
+
*/
|
|
149
|
+
_getNextDateFrom: function(start) {
|
|
150
|
+
var date = moment(start);
|
|
151
|
+
if (date.toString() == 'Invalid date') {
|
|
152
|
+
console.log("ERROR: You specified an invalid date.");
|
|
153
|
+
return date;
|
|
154
|
+
}
|
|
155
|
+
if (this.realDate && start < new Date())
|
|
156
|
+
console.log("WARNING: Date in past. Will never be fired.");
|
|
157
|
+
if (this.realDate) return date;
|
|
158
|
+
|
|
159
|
+
//sanity check
|
|
160
|
+
while (1) {
|
|
161
|
+
var diff = date - start,
|
|
162
|
+
origDate = new Date(date);
|
|
163
|
+
|
|
164
|
+
if (!(date.month() in this.month)) {
|
|
165
|
+
date.add(1, 'M');
|
|
166
|
+
date.date(1);
|
|
167
|
+
date.hours(0);
|
|
168
|
+
date.minutes(0);
|
|
169
|
+
date.seconds(0);
|
|
170
|
+
continue;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (!(date.date() in this.dayOfMonth)) {
|
|
174
|
+
date.add(1, 'd');
|
|
175
|
+
date.hours(0);
|
|
176
|
+
date.minutes(0);
|
|
177
|
+
date.seconds(0);
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
if (!(date.day() in this.dayOfWeek)) {
|
|
182
|
+
date.add(1, 'd');
|
|
183
|
+
date.hours(0);
|
|
184
|
+
date.minutes(0);
|
|
185
|
+
date.seconds(0);
|
|
186
|
+
if (date <= origDate) {
|
|
187
|
+
date = this._findDST(origDate);
|
|
188
|
+
}
|
|
189
|
+
continue;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
if (!(date.hours() in this.hour)) {
|
|
193
|
+
origDate = moment(date);
|
|
194
|
+
date.hours(date.hours() == 23 && diff > 86400000 ? 0 : date.hours() + 1);
|
|
195
|
+
date.minutes(0);
|
|
196
|
+
date.seconds(0);
|
|
197
|
+
if (date <= origDate) {
|
|
198
|
+
date = this._findDST(origDate);
|
|
199
|
+
}
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
if (!(date.minutes() in this.minute)) {
|
|
204
|
+
origDate = moment(date);
|
|
205
|
+
date.minutes(date.minutes() == 59 && diff > 60 * 60 * 1000 ? 0 : date.minutes() + 1);
|
|
206
|
+
date.seconds(0);
|
|
207
|
+
if (date <= origDate) {
|
|
208
|
+
date = this._findDST(origDate);
|
|
209
|
+
}
|
|
210
|
+
continue;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
if (!(date.seconds() in this.second)) {
|
|
214
|
+
origDate = moment(date);
|
|
215
|
+
date.seconds(date.seconds() == 59 && diff > 60 * 1000 ? 0 : date.seconds() + 1);
|
|
216
|
+
if (date <= origDate) {
|
|
217
|
+
date = this._findDST(origDate);
|
|
218
|
+
}
|
|
219
|
+
continue;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
break;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
return date;
|
|
226
|
+
},
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* get next date that is a valid DST date
|
|
230
|
+
*/
|
|
231
|
+
_findDST: function(date) {
|
|
232
|
+
var newDate = moment(date),
|
|
233
|
+
addSeconds = 1;
|
|
234
|
+
while (newDate <= date)
|
|
235
|
+
newDate.add(1, 's');
|
|
236
|
+
|
|
237
|
+
return newDate;
|
|
238
|
+
},
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* wildcard, or all params in array (for to string)
|
|
242
|
+
*/
|
|
243
|
+
_wcOrAll: function(type) {
|
|
244
|
+
if (this._hasAll(type)) return '*';
|
|
245
|
+
|
|
246
|
+
var all = [];
|
|
247
|
+
for (var time in this[type]) {
|
|
248
|
+
all.push(time);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
return all.join(',');
|
|
252
|
+
},
|
|
253
|
+
|
|
254
|
+
_hasAll: function(type) {
|
|
255
|
+
var constrain = CronTime.constraints[timeUnits.indexOf(type)];
|
|
256
|
+
|
|
257
|
+
for (var i = constrain[0], n = constrain[1]; i < n; i++) {
|
|
258
|
+
if (!(i in this[type])) return false;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
return true;
|
|
262
|
+
},
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
_parse: function() {
|
|
266
|
+
var aliases = CronTime.aliases,
|
|
267
|
+
source = this.source.replace(/[a-z]{1,3}/ig, function(alias) {
|
|
268
|
+
alias = alias.toLowerCase();
|
|
269
|
+
|
|
270
|
+
if (alias in aliases) {
|
|
271
|
+
return aliases[alias];
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
throw new Error('Unknown alias: ' + alias);
|
|
275
|
+
}),
|
|
276
|
+
split = source.replace(/^\s\s*|\s\s*$/g, '').split(/\s+/),
|
|
277
|
+
cur, i = 0,
|
|
278
|
+
len = timeUnits.length;
|
|
279
|
+
|
|
280
|
+
for (; i < timeUnits.length; i++) {
|
|
281
|
+
// If the split source string doesn't contain all digits,
|
|
282
|
+
// assume defaults for first n missing digits.
|
|
283
|
+
// This adds support for 5-digit standard cron syntax
|
|
284
|
+
cur = split[i - (len - split.length)] || CronTime.parseDefaults[i];
|
|
285
|
+
this._parseField(cur, timeUnits[i], CronTime.constraints[i]);
|
|
286
|
+
}
|
|
287
|
+
},
|
|
288
|
+
|
|
289
|
+
_parseField: function(field, type, constraints) {
|
|
290
|
+
var rangePattern = /^(\d+)(?:-(\d+))?(?:\/(\d+))?$/g,
|
|
291
|
+
typeObj = this[type],
|
|
292
|
+
diff, pointer,
|
|
293
|
+
low = constraints[0],
|
|
294
|
+
high = constraints[1];
|
|
295
|
+
|
|
296
|
+
// * is a shortcut to [lower-upper] range
|
|
297
|
+
field = field.replace(/\*/g, low + '-' + high);
|
|
298
|
+
|
|
299
|
+
//commas separate information, so split based on those
|
|
300
|
+
var allRanges = field.split(',');
|
|
301
|
+
|
|
302
|
+
for (var i = 0; i < allRanges.length; i++) {
|
|
303
|
+
if (allRanges[i].match(rangePattern)) {
|
|
304
|
+
allRanges[i].replace(rangePattern, function($0, lower, upper, step) {
|
|
305
|
+
step = parseInt(step) || 1;
|
|
306
|
+
// Positive integer higher than constraints[0]
|
|
307
|
+
lower = Math.min(Math.max(low, ~~Math.abs(lower)), high);
|
|
308
|
+
|
|
309
|
+
// Positive integer lower than constraints[1]
|
|
310
|
+
upper = upper ? Math.min(high, ~~Math.abs(upper)) : lower;
|
|
311
|
+
|
|
312
|
+
// Count from the lower barrier to the upper
|
|
313
|
+
pointer = lower;
|
|
314
|
+
|
|
315
|
+
do {
|
|
316
|
+
typeObj[pointer] = true
|
|
317
|
+
pointer += step;
|
|
318
|
+
} while (pointer <= upper);
|
|
319
|
+
});
|
|
320
|
+
} else {
|
|
321
|
+
throw new Error('Field (' + field + ') cannot be parsed');
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
}
|
|
294
325
|
};
|
|
295
326
|
|
|
327
|
+
function command2function(cmd) {
|
|
328
|
+
switch (typeof cmd) {
|
|
329
|
+
case 'string':
|
|
330
|
+
var args = cmd.split(' ');
|
|
331
|
+
var command = args.shift();
|
|
332
|
+
|
|
333
|
+
cmd = spawn.bind(undefined, command, args);
|
|
334
|
+
break;
|
|
335
|
+
case 'object':
|
|
336
|
+
var command = cmd && cmd.command;
|
|
337
|
+
if (command) {
|
|
338
|
+
var args = cmd.args;
|
|
339
|
+
var options = cmd.options;
|
|
340
|
+
|
|
341
|
+
cmd = spawn.bind(undefined, command, args, options);
|
|
342
|
+
}
|
|
343
|
+
break;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
return cmd
|
|
347
|
+
}
|
|
296
348
|
|
|
349
|
+
function CronJob(cronTime, onTick, onComplete, startNow, timeZone, context) {
|
|
350
|
+
if (typeof cronTime != "string" && arguments.length == 1) {
|
|
351
|
+
//crontime is an object...
|
|
352
|
+
onTick = cronTime.onTick;
|
|
353
|
+
onComplete = cronTime.onComplete;
|
|
354
|
+
context = cronTime.context;
|
|
355
|
+
startNow = cronTime.start || cronTime.startNow;
|
|
356
|
+
timeZone = cronTime.timeZone;
|
|
357
|
+
cronTime = cronTime.cronTime;
|
|
358
|
+
}
|
|
297
359
|
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
onComplete = cronTime.onComplete;
|
|
303
|
-
context = cronTime.context;
|
|
304
|
-
start = cronTime.start;
|
|
305
|
-
timeZone = cronTime.timeZone;
|
|
306
|
-
cronTime = cronTime.cronTime;
|
|
307
|
-
}
|
|
360
|
+
this.context = (context || this);
|
|
361
|
+
this._callbacks = [];
|
|
362
|
+
this.onComplete = command2function(onComplete);
|
|
363
|
+
this.cronTime = new CronTime(cronTime, timeZone);
|
|
308
364
|
|
|
309
|
-
|
|
365
|
+
addCallback.call(this, command2function(onTick));
|
|
310
366
|
|
|
311
|
-
|
|
312
|
-
this._callbacks = [];
|
|
313
|
-
this.onComplete = onComplete;
|
|
314
|
-
this.cronTime = new CronTime(cronTime, timeZone);
|
|
367
|
+
if (startNow) start.call(this);
|
|
315
368
|
|
|
316
|
-
|
|
369
|
+
return this;
|
|
370
|
+
}
|
|
317
371
|
|
|
318
|
-
|
|
372
|
+
var addCallback = function(callback) {
|
|
373
|
+
if (typeof callback == 'function') this._callbacks.push(callback);
|
|
374
|
+
}
|
|
375
|
+
CronJob.prototype.addCallback = addCallback;
|
|
319
376
|
|
|
320
|
-
|
|
377
|
+
CronJob.prototype.setTime = function(time) {
|
|
378
|
+
if (!(time instanceof CronTime)) throw '\'time\' must be an instance of CronTime.';
|
|
379
|
+
this.stop();
|
|
380
|
+
this.cronTime = time;
|
|
321
381
|
}
|
|
322
382
|
|
|
323
|
-
CronJob.prototype = {
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
var remaining = 0;
|
|
369
|
-
|
|
370
|
-
if (this.cronTime.realDate) this.runOnce = true;
|
|
371
|
-
|
|
372
|
-
// The callback wrapper checks if it needs to sleep another period or not
|
|
373
|
-
// and does the real callback logic when it's time.
|
|
374
|
-
|
|
375
|
-
function callbackWrapper() {
|
|
376
|
-
|
|
377
|
-
// If there is sleep time remaining, calculate how long and go to sleep
|
|
378
|
-
// again. This processing might make us miss the deadline by a few ms
|
|
379
|
-
// times the number of sleep sessions. Given a MAXDELAY of almost a
|
|
380
|
-
// month, this should be no issue.
|
|
381
|
-
|
|
382
|
-
if (remaining) {
|
|
383
|
-
if (remaining > MAXDELAY) {
|
|
384
|
-
remaining -= MAXDELAY;
|
|
385
|
-
timeout = MAXDELAY;
|
|
386
|
-
} else {
|
|
387
|
-
timeout = remaining;
|
|
388
|
-
remaining = 0;
|
|
389
|
-
}
|
|
390
|
-
|
|
391
|
-
self._timeout = setTimeout(callbackWrapper, timeout);
|
|
392
|
-
} else {
|
|
393
|
-
|
|
394
|
-
// We have arrived at the correct point in time.
|
|
395
|
-
|
|
396
|
-
self.running = false;
|
|
397
|
-
|
|
398
|
-
//start before calling back so the callbacks have the ability to stop the cron job
|
|
399
|
-
if (!(self.runOnce)) self.start();
|
|
400
|
-
|
|
401
|
-
self._callback();
|
|
402
|
-
}
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
if (timeout >= 0) {
|
|
406
|
-
this.running = true;
|
|
407
|
-
|
|
408
|
-
// Don't try to sleep more than MAXDELAY ms at a time.
|
|
409
|
-
|
|
410
|
-
if (timeout > MAXDELAY) {
|
|
411
|
-
remaining = timeout - MAXDELAY;
|
|
412
|
-
timeout = MAXDELAY;
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
this._timeout = setTimeout(callbackWrapper, timeout);
|
|
416
|
-
} else {
|
|
417
|
-
this.stop();
|
|
383
|
+
CronJob.prototype.nextDate = function() {
|
|
384
|
+
return this.cronTime.sendAt();
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
var start = function() {
|
|
388
|
+
if (this.running) return;
|
|
389
|
+
|
|
390
|
+
var MAXDELAY = 2147483647; // The maximum number of milliseconds setTimeout will wait.
|
|
391
|
+
var self = this;
|
|
392
|
+
var timeout = this.cronTime.getTimeout();
|
|
393
|
+
var remaining = 0;
|
|
394
|
+
|
|
395
|
+
if (this.cronTime.realDate) this.runOnce = true;
|
|
396
|
+
|
|
397
|
+
// The callback wrapper checks if it needs to sleep another period or not
|
|
398
|
+
// and does the real callback logic when it's time.
|
|
399
|
+
|
|
400
|
+
function callbackWrapper() {
|
|
401
|
+
|
|
402
|
+
// If there is sleep time remaining, calculate how long and go to sleep
|
|
403
|
+
// again. This processing might make us miss the deadline by a few ms
|
|
404
|
+
// times the number of sleep sessions. Given a MAXDELAY of almost a
|
|
405
|
+
// month, this should be no issue.
|
|
406
|
+
|
|
407
|
+
if (remaining) {
|
|
408
|
+
if (remaining > MAXDELAY) {
|
|
409
|
+
remaining -= MAXDELAY;
|
|
410
|
+
timeout = MAXDELAY;
|
|
411
|
+
} else {
|
|
412
|
+
timeout = remaining;
|
|
413
|
+
remaining = 0;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
self._timeout = setTimeout(callbackWrapper, timeout);
|
|
417
|
+
} else {
|
|
418
|
+
|
|
419
|
+
// We have arrived at the correct point in time.
|
|
420
|
+
|
|
421
|
+
self.running = false;
|
|
422
|
+
|
|
423
|
+
//start before calling back so the callbacks have the ability to stop the cron job
|
|
424
|
+
if (!(self.runOnce)) self.start();
|
|
425
|
+
|
|
426
|
+
for (var i = (self._callbacks.length - 1); i >= 0; i--)
|
|
427
|
+
self._callbacks[i].call(self.context, self.onComplete);
|
|
418
428
|
}
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
if (timeout >= 0) {
|
|
432
|
+
this.running = true;
|
|
433
|
+
|
|
434
|
+
// Don't try to sleep more than MAXDELAY ms at a time.
|
|
435
|
+
|
|
436
|
+
if (timeout > MAXDELAY) {
|
|
437
|
+
remaining = timeout - MAXDELAY;
|
|
438
|
+
timeout = MAXDELAY;
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
this._timeout = setTimeout(callbackWrapper, timeout);
|
|
442
|
+
} else {
|
|
443
|
+
this.stop();
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
CronJob.prototype.start = start;
|
|
448
|
+
|
|
449
|
+
/**
|
|
450
|
+
* Stop the cronjob.
|
|
451
|
+
*/
|
|
452
|
+
CronJob.prototype.stop = function() {
|
|
453
|
+
if (this._timeout)
|
|
454
|
+
clearTimeout(this._timeout);
|
|
455
|
+
this.running = false;
|
|
456
|
+
if (typeof this.onComplete == 'function') this.onComplete();
|
|
457
|
+
}
|
|
431
458
|
|
|
432
459
|
if (exports) {
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
460
|
+
exports.job = function(cronTime, onTick, onComplete) {
|
|
461
|
+
return new CronJob(cronTime, onTick, onComplete);
|
|
462
|
+
}
|
|
436
463
|
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
464
|
+
exports.time = function(cronTime, timeZone) {
|
|
465
|
+
return new CronTime(cronTime, timeZone);
|
|
466
|
+
}
|
|
440
467
|
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
468
|
+
exports.sendAt = function(cronTime) {
|
|
469
|
+
return exports.time(cronTime).sendAt();
|
|
470
|
+
}
|
|
444
471
|
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
472
|
+
exports.timeout = function(cronTime) {
|
|
473
|
+
return exports.time(cronTime).getTimeout();
|
|
474
|
+
}
|
|
448
475
|
|
|
449
|
-
|
|
450
|
-
|
|
476
|
+
exports.CronJob = CronJob;
|
|
477
|
+
exports.CronTime = CronTime;
|
|
451
478
|
}
|