cron 1.2.1 → 1.3.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/README.md +37 -3
- package/lib/cron.js +11 -5
- package/package.json +6 -5
- package/.npmignore +0 -2
- package/test.js +0 -5
- package/tests/test-cron.js +0 -562
- package/tests/test-crontime.js +0 -193
package/README.md
CHANGED
|
@@ -9,7 +9,7 @@ Originally this project was a NodeJS fork of [James Padolsey's][jamespadolsey] [
|
|
|
9
9
|
|
|
10
10
|
After [Craig Condon][crcn] made some updates and changes to the code base this has evolved to something that has a bit of both. The cron syntax parsing is mostly James' while using timeout instead of interval is Craig's.
|
|
11
11
|
|
|
12
|
-
Additionally, this library goes beyond the basic cron syntax and allows you to supply a Date object. This will be used as the trigger for your callback. Cron syntax is still an acceptable CronTime format. Although the Cron patterns
|
|
12
|
+
Additionally, this library goes beyond the basic cron syntax and allows you to supply a Date object. This will be used as the trigger for your callback. Cron syntax is still an acceptable CronTime format. Although the Cron patterns supported here extend on the standard Unix format to support seconds digits, leaving it off will default to 0 and match the Unix behavior.
|
|
13
13
|
|
|
14
14
|
If You Are Submitting Bugs/Issues
|
|
15
15
|
=============
|
|
@@ -136,6 +136,40 @@ try {
|
|
|
136
136
|
```
|
|
137
137
|
|
|
138
138
|
|
|
139
|
+
How to check if a job is running
|
|
140
|
+
==========
|
|
141
|
+
|
|
142
|
+
```javascript
|
|
143
|
+
var cron = require('cron');
|
|
144
|
+
|
|
145
|
+
var job1 = new cron.CronJob({
|
|
146
|
+
cronTime: '* * * * *',
|
|
147
|
+
onTick: function() {
|
|
148
|
+
console.log('job 1 ticked');
|
|
149
|
+
},
|
|
150
|
+
start: false,
|
|
151
|
+
timeZone: 'America/Los_Angeles'
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
var job2 = new cron.CronJob({
|
|
155
|
+
cronTime: '* * * * *',
|
|
156
|
+
onTick: function() {
|
|
157
|
+
console.log('job 2 ticked');
|
|
158
|
+
},
|
|
159
|
+
start: false,
|
|
160
|
+
timeZone: 'America/Los_Angeles'
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
console.log('job1 status', job1.running); // job1 status undefined
|
|
164
|
+
console.log('job2 status', job2.running); // job2 status undefined
|
|
165
|
+
|
|
166
|
+
job1.start(); // job 1 started
|
|
167
|
+
|
|
168
|
+
console.log('job1 status', job1.running); // job1 status true
|
|
169
|
+
console.log('job2 status', job2.running); // job2 status undefined
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
|
|
139
173
|
Install
|
|
140
174
|
==========
|
|
141
175
|
|
|
@@ -155,9 +189,9 @@ Parameter Based
|
|
|
155
189
|
* `onTick` - [REQUIRED] - The function to fire at the specified time.
|
|
156
190
|
* `onComplete` - [OPTIONAL] - A function that will fire when the job is complete, when it is stopped.
|
|
157
191
|
* `start` - [OPTIONAL] - Specifies whether to start the job just before exiting the constructor. By default this is set to false. If left at default you will need to call `job.start()` in order to start the job (assuming `job` is the variable you set the cronjob to). This does not immediately fire your `onTick` function, it just gives you more control over the behavior of your jobs.
|
|
158
|
-
* `timeZone` - [OPTIONAL] - Specify the timezone for the execution. This will modify the actual time relative to your timezone. If the timezone is invalid, an error is thrown.
|
|
192
|
+
* `timeZone` - [OPTIONAL] - Specify the timezone for the execution. This will modify the actual time relative to your timezone. If the timezone is invalid, an error is thrown. You can check all timezones available at [Moment Timezone Website](http://momentjs.com/timezone/).
|
|
159
193
|
* `context` - [OPTIONAL] - The context within which to execute the onTick method. This defaults to the cronjob itself allowing you to call `this.stop()`. However, if you change this you'll have access to the functions and values within your context object.
|
|
160
|
-
* `runOnInit` - [OPTIONAL] - This will immediately fire your `onTick` function as soon as the requisit initialization has happened. This option is set to `false` by default for backwards
|
|
194
|
+
* `runOnInit` - [OPTIONAL] - This will immediately fire your `onTick` function as soon as the requisit initialization has happened. This option is set to `false` by default for backwards compatibility.
|
|
161
195
|
* `start` - Runs your job.
|
|
162
196
|
* `stop` - Stops your job.
|
|
163
197
|
|
package/lib/cron.js
CHANGED
|
@@ -12,7 +12,7 @@ var exports = {},
|
|
|
12
12
|
timeUnits = ['second', 'minute', 'hour', 'dayOfMonth', 'month', 'dayOfWeek'],
|
|
13
13
|
spawn = child_process && child_process.spawn;
|
|
14
14
|
|
|
15
|
-
function CronTime(source, zone) {
|
|
15
|
+
function CronTime(source, zone, utcOffset) {
|
|
16
16
|
this.source = source;
|
|
17
17
|
|
|
18
18
|
if (zone) {
|
|
@@ -23,6 +23,8 @@ function CronTime(source, zone) {
|
|
|
23
23
|
this.zone = zone;
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
+
if (utcOffset) this.utcOffset = utcOffset;
|
|
27
|
+
|
|
26
28
|
var that = this;
|
|
27
29
|
timeUnits.map(function(timeUnit){
|
|
28
30
|
that[timeUnit] = {};
|
|
@@ -112,6 +114,9 @@ CronTime.prototype = {
|
|
|
112
114
|
if (this.zone)
|
|
113
115
|
date = date.tz(this.zone);
|
|
114
116
|
|
|
117
|
+
if (this.utcOffset)
|
|
118
|
+
date = date.utcOffset(this.utcOffset);
|
|
119
|
+
|
|
115
120
|
if (this.realDate)
|
|
116
121
|
return date;
|
|
117
122
|
|
|
@@ -370,7 +375,7 @@ function command2function(cmd) {
|
|
|
370
375
|
return cmd
|
|
371
376
|
}
|
|
372
377
|
|
|
373
|
-
function CronJob(cronTime, onTick, onComplete, startNow, timeZone, context, runOnInit) {
|
|
378
|
+
function CronJob(cronTime, onTick, onComplete, startNow, timeZone, context, runOnInit, utcOffset) {
|
|
374
379
|
var _cronTime = cronTime;
|
|
375
380
|
if (typeof cronTime != 'string' && arguments.length == 1) {
|
|
376
381
|
//crontime is an object...
|
|
@@ -381,12 +386,13 @@ function CronJob(cronTime, onTick, onComplete, startNow, timeZone, context, runO
|
|
|
381
386
|
timeZone = cronTime.timeZone;
|
|
382
387
|
runOnInit = cronTime.runOnInit;
|
|
383
388
|
_cronTime = cronTime.cronTime;
|
|
389
|
+
utcOffset = cronTime.utcOffset;
|
|
384
390
|
}
|
|
385
391
|
|
|
386
392
|
this.context = (context || this);
|
|
387
393
|
this._callbacks = [];
|
|
388
394
|
this.onComplete = command2function(onComplete);
|
|
389
|
-
this.cronTime = new CronTime(_cronTime, timeZone);
|
|
395
|
+
this.cronTime = new CronTime(_cronTime, timeZone, utcOffset);
|
|
390
396
|
|
|
391
397
|
addCallback.call(this, command2function(onTick));
|
|
392
398
|
|
|
@@ -516,8 +522,8 @@ CronJob.prototype.stop = function() {
|
|
|
516
522
|
if (typeof this.onComplete == 'function') this.onComplete();
|
|
517
523
|
}
|
|
518
524
|
|
|
519
|
-
exports.job = function(cronTime, onTick, onComplete) {
|
|
520
|
-
return new CronJob(cronTime, onTick, onComplete);
|
|
525
|
+
exports.job = function(cronTime, onTick, onComplete, startNow, timeZone, context, runOnInit) {
|
|
526
|
+
return new CronJob(cronTime, onTick, onComplete, startNow, timeZone, context, runOnInit);
|
|
521
527
|
}
|
|
522
528
|
|
|
523
529
|
exports.time = function(cronTime, timeZone) {
|
package/package.json
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cron",
|
|
3
3
|
"description": "Cron jobs for your node",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.3.0",
|
|
5
5
|
"author": "Nick Campbell <nicholas.j.campbell@gmail.com> (http://github.com/ncb000gt)",
|
|
6
|
-
"bugs"
|
|
7
|
-
"url"
|
|
6
|
+
"bugs": {
|
|
7
|
+
"url": "http://github.com/kelektiv/node-cron/issues"
|
|
8
8
|
},
|
|
9
9
|
"repository": {
|
|
10
10
|
"type": "git",
|
|
11
|
-
"url": "http://github.com/
|
|
11
|
+
"url": "http://github.com/kelektiv/node-cron.git"
|
|
12
12
|
},
|
|
13
13
|
"main": "lib/cron",
|
|
14
14
|
"scripts": {
|
|
@@ -35,6 +35,7 @@
|
|
|
35
35
|
"Vadim Baryshev <vadimbaryshev@gmail.com> (https://github.com/baryshev)",
|
|
36
36
|
"Leandro Ferrari <lfthomaz@gmail.com> (https://github.com/lfthomaz)",
|
|
37
37
|
"Gregg Zigler <greggzigler@gmail.com> (https://github.com/greggzigler)",
|
|
38
|
-
"Jordan Abderrachid <jabderrachid@gmail.com> (https://github.com/jordanabderrachid)"
|
|
38
|
+
"Jordan Abderrachid <jabderrachid@gmail.com> (https://github.com/jordanabderrachid)",
|
|
39
|
+
"Masakazu Matsushita <matsukaz@gmail.com> (matsukaz)"
|
|
39
40
|
]
|
|
40
41
|
}
|
package/.npmignore
DELETED
package/test.js
DELETED
package/tests/test-cron.js
DELETED
|
@@ -1,562 +0,0 @@
|
|
|
1
|
-
var chai = require('chai'),
|
|
2
|
-
expect = chai.expect,
|
|
3
|
-
sinon = require('sinon'),
|
|
4
|
-
cron = require('../lib/cron');
|
|
5
|
-
|
|
6
|
-
describe('cron', function() {
|
|
7
|
-
it('should run every second (* * * * * *)', function() {
|
|
8
|
-
var clock = sinon.useFakeTimers();
|
|
9
|
-
var c = 0;
|
|
10
|
-
|
|
11
|
-
var job = new cron.CronJob('* * * * * *', function() {
|
|
12
|
-
c++;
|
|
13
|
-
}, null, true);
|
|
14
|
-
|
|
15
|
-
clock.tick(1000);
|
|
16
|
-
job.stop();
|
|
17
|
-
clock.restore();
|
|
18
|
-
|
|
19
|
-
expect(c).to.eql(1);
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
it('should run second with oncomplete (* * * * * *)', function(done) {
|
|
23
|
-
var clock = sinon.useFakeTimers();
|
|
24
|
-
var c = 0;
|
|
25
|
-
|
|
26
|
-
var job = new cron.CronJob('* * * * * *', function() {
|
|
27
|
-
c++;
|
|
28
|
-
}, function () {
|
|
29
|
-
expect(c).to.eql(1);
|
|
30
|
-
done();
|
|
31
|
-
}, true);
|
|
32
|
-
|
|
33
|
-
clock.tick(1000);
|
|
34
|
-
clock.restore();
|
|
35
|
-
job.stop();
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
it('should fire every 60 min', function () {
|
|
39
|
-
var m60 = 60 * 60 * 1000;
|
|
40
|
-
var clock = sinon.useFakeTimers();
|
|
41
|
-
var l = [];
|
|
42
|
-
var job = new cron.CronJob('00 30 * * * *', function () {
|
|
43
|
-
l.push(Math.floor(Date.now() / 60000));
|
|
44
|
-
},null,true);
|
|
45
|
-
|
|
46
|
-
clock.tick(m60 * 10);
|
|
47
|
-
|
|
48
|
-
expect(l.length).to.eql(10);
|
|
49
|
-
for (var i = 0; i < l.length; i++) {
|
|
50
|
-
expect(l[i] % 30).to.eql(0);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
job.stop();
|
|
54
|
-
clock.restore();
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
it('should use standard cron no-seconds syntax (* * * * *)', function() {
|
|
58
|
-
var clock = sinon.useFakeTimers();
|
|
59
|
-
var c = 0;
|
|
60
|
-
|
|
61
|
-
var job = new cron.CronJob('* * * * *', function() {
|
|
62
|
-
c++;
|
|
63
|
-
}, null, true);
|
|
64
|
-
|
|
65
|
-
clock.tick(1000); //tick second
|
|
66
|
-
|
|
67
|
-
clock.tick(59 * 1000); //tick minute
|
|
68
|
-
|
|
69
|
-
job.stop();
|
|
70
|
-
clock.restore();
|
|
71
|
-
|
|
72
|
-
expect(c).to.eql(1);
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
it('should run every second for 5 seconds (* * * * * *)', function() {
|
|
76
|
-
var clock = sinon.useFakeTimers();
|
|
77
|
-
var c = 0;
|
|
78
|
-
|
|
79
|
-
var job = new cron.CronJob('* * * * * *', function() {
|
|
80
|
-
c++;
|
|
81
|
-
}, null, true);
|
|
82
|
-
|
|
83
|
-
for (var i = 0; i < 5; i++)
|
|
84
|
-
clock.tick(1000);
|
|
85
|
-
|
|
86
|
-
clock.restore();
|
|
87
|
-
job.stop();
|
|
88
|
-
|
|
89
|
-
expect(c).to.eql(5);
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
it('should run every second for 5 seconds with oncomplete (* * * * * *)', function(done) {
|
|
93
|
-
var clock = sinon.useFakeTimers();
|
|
94
|
-
var c = 0;
|
|
95
|
-
|
|
96
|
-
var job = new cron.CronJob('* * * * * *', function() {
|
|
97
|
-
c++;
|
|
98
|
-
}, function() {
|
|
99
|
-
expect(c).to.eql(5);
|
|
100
|
-
done();
|
|
101
|
-
}, true);
|
|
102
|
-
|
|
103
|
-
for (var i = 0; i < 5; i++)
|
|
104
|
-
clock.tick(1000);
|
|
105
|
-
|
|
106
|
-
clock.restore();
|
|
107
|
-
job.stop();
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
it('should run every second for 5 seconds (*/1 * * * * *)', function() {
|
|
111
|
-
var clock = sinon.useFakeTimers();
|
|
112
|
-
var c = 0;
|
|
113
|
-
|
|
114
|
-
var job = new cron.CronJob('*/1 * * * * *', function() {
|
|
115
|
-
c++;
|
|
116
|
-
}, null, true);
|
|
117
|
-
|
|
118
|
-
for (var i = 0; i < 5; i++)
|
|
119
|
-
clock.tick(1000);
|
|
120
|
-
|
|
121
|
-
clock.restore();
|
|
122
|
-
job.stop();
|
|
123
|
-
expect(c).to.eql(5);
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
//ensure that this is running on the second second
|
|
127
|
-
it('should run every 2 seconds for 1 seconds (*/2 * * * * *)', function() {
|
|
128
|
-
var clock = sinon.useFakeTimers();
|
|
129
|
-
var c = 0;
|
|
130
|
-
|
|
131
|
-
var job = new cron.CronJob('*/2 * * * * *', function() {
|
|
132
|
-
c++;
|
|
133
|
-
}, null, true);
|
|
134
|
-
|
|
135
|
-
clock.tick(1000);
|
|
136
|
-
|
|
137
|
-
clock.restore();
|
|
138
|
-
job.stop();
|
|
139
|
-
expect(c).to.eql(0);
|
|
140
|
-
});
|
|
141
|
-
|
|
142
|
-
it('should run every 2 seconds for 5 seconds (*/2 * * * * *)', function() {
|
|
143
|
-
var clock = sinon.useFakeTimers();
|
|
144
|
-
var c = 0;
|
|
145
|
-
|
|
146
|
-
var job = new cron.CronJob('*/2 * * * * *', function() {
|
|
147
|
-
c++;
|
|
148
|
-
}, null, true);
|
|
149
|
-
|
|
150
|
-
for (var i = 0; i < 5; i++)
|
|
151
|
-
clock.tick(1000);
|
|
152
|
-
|
|
153
|
-
clock.restore();
|
|
154
|
-
job.stop();
|
|
155
|
-
expect(c).to.eql(2);
|
|
156
|
-
});
|
|
157
|
-
|
|
158
|
-
it('should run every second for 5 seconds with oncomplete (*/1 * * * * *)', function(done) {
|
|
159
|
-
var clock = sinon.useFakeTimers();
|
|
160
|
-
var c = 0;
|
|
161
|
-
|
|
162
|
-
var job = new cron.CronJob('*/1 * * * * *', function() {
|
|
163
|
-
c++
|
|
164
|
-
}, function() {
|
|
165
|
-
expect(c).to.eql(5);
|
|
166
|
-
done();
|
|
167
|
-
}, true);
|
|
168
|
-
|
|
169
|
-
for (var i = 0; i < 5; i++)
|
|
170
|
-
clock.tick(1000);
|
|
171
|
-
|
|
172
|
-
clock.restore();
|
|
173
|
-
job.stop();
|
|
174
|
-
});
|
|
175
|
-
|
|
176
|
-
it('should run every second for a range ([start]-[end] * * * * *)', function() {
|
|
177
|
-
var clock = sinon.useFakeTimers();
|
|
178
|
-
var c = 0;
|
|
179
|
-
|
|
180
|
-
var job = new cron.CronJob('0-8 * * * * *', function() {
|
|
181
|
-
c++;
|
|
182
|
-
}, null, true);
|
|
183
|
-
|
|
184
|
-
clock.tick(10000); //run for 10 seconds
|
|
185
|
-
|
|
186
|
-
clock.restore();
|
|
187
|
-
job.stop();
|
|
188
|
-
expect(c).to.eql(8);
|
|
189
|
-
});
|
|
190
|
-
|
|
191
|
-
it('should run every second for a range with oncomplete ([start]-[end] * * * * *)', function(done) {
|
|
192
|
-
var clock = sinon.useFakeTimers();
|
|
193
|
-
var c = 0;
|
|
194
|
-
|
|
195
|
-
var job = new cron.CronJob('0-8 * * * * *', function() {
|
|
196
|
-
c++;
|
|
197
|
-
}, function() {
|
|
198
|
-
expect(c).to.eql(8);
|
|
199
|
-
done();
|
|
200
|
-
}, true);
|
|
201
|
-
|
|
202
|
-
clock.tick(10000);
|
|
203
|
-
|
|
204
|
-
clock.restore();
|
|
205
|
-
job.stop();
|
|
206
|
-
});
|
|
207
|
-
|
|
208
|
-
it('should run every second (* * * * * *) using the object constructor', function() {
|
|
209
|
-
var clock = sinon.useFakeTimers();
|
|
210
|
-
var c = 0;
|
|
211
|
-
|
|
212
|
-
var job = new cron.CronJob({
|
|
213
|
-
cronTime: '* * * * * *',
|
|
214
|
-
onTick: function() {
|
|
215
|
-
c++;
|
|
216
|
-
},
|
|
217
|
-
start: true
|
|
218
|
-
});
|
|
219
|
-
|
|
220
|
-
clock.tick(1000);
|
|
221
|
-
|
|
222
|
-
clock.restore();
|
|
223
|
-
job.stop();
|
|
224
|
-
expect(c).to.eql(1);
|
|
225
|
-
});
|
|
226
|
-
|
|
227
|
-
it('should run every second with oncomplete (* * * * * *) using the object constructor', function(done) {
|
|
228
|
-
var clock = sinon.useFakeTimers();
|
|
229
|
-
var c = 0;
|
|
230
|
-
|
|
231
|
-
var job = new cron.CronJob({
|
|
232
|
-
cronTime: '* * * * * *',
|
|
233
|
-
onTick: function(done) {
|
|
234
|
-
c++;
|
|
235
|
-
},
|
|
236
|
-
onComplete: function () {
|
|
237
|
-
expect(c).to.eql(1);
|
|
238
|
-
done();
|
|
239
|
-
},
|
|
240
|
-
start: true
|
|
241
|
-
});
|
|
242
|
-
|
|
243
|
-
clock.tick(1000);
|
|
244
|
-
|
|
245
|
-
clock.restore();
|
|
246
|
-
job.stop();
|
|
247
|
-
});
|
|
248
|
-
|
|
249
|
-
it('should start and stop job', function(done) {
|
|
250
|
-
var clock = sinon.useFakeTimers();
|
|
251
|
-
var c = 0;
|
|
252
|
-
|
|
253
|
-
var job = new cron.CronJob('* * * * * *', function() {
|
|
254
|
-
c++;
|
|
255
|
-
this.stop();
|
|
256
|
-
}, function() {
|
|
257
|
-
expect(c).to.eql(1);
|
|
258
|
-
|
|
259
|
-
clock.restore();
|
|
260
|
-
done();
|
|
261
|
-
});
|
|
262
|
-
job.start();
|
|
263
|
-
|
|
264
|
-
clock.tick(1000);
|
|
265
|
-
});
|
|
266
|
-
|
|
267
|
-
it('should run on a specific date', function() {
|
|
268
|
-
var c = 0;
|
|
269
|
-
var d = new Date();
|
|
270
|
-
var clock = sinon.useFakeTimers(d.getTime());
|
|
271
|
-
var s = d.getSeconds()+1;
|
|
272
|
-
d.setSeconds(s);
|
|
273
|
-
|
|
274
|
-
var job = new cron.CronJob(d, function() {
|
|
275
|
-
var t = new Date();
|
|
276
|
-
expect(t.getSeconds()).to.eql(d.getSeconds());
|
|
277
|
-
c++;
|
|
278
|
-
}, null, true);
|
|
279
|
-
clock.tick(1000);
|
|
280
|
-
|
|
281
|
-
clock.restore();
|
|
282
|
-
job.stop();
|
|
283
|
-
expect(c).to.eql(1);
|
|
284
|
-
});
|
|
285
|
-
|
|
286
|
-
it('should run on a specific date with oncomplete', function(done) {
|
|
287
|
-
var c = 0;
|
|
288
|
-
var d = new Date();
|
|
289
|
-
var clock = sinon.useFakeTimers(d.getTime());
|
|
290
|
-
var s = d.getSeconds()+1;
|
|
291
|
-
d.setSeconds(s);
|
|
292
|
-
|
|
293
|
-
var job = new cron.CronJob(d, function() {
|
|
294
|
-
var t = new Date();
|
|
295
|
-
expect(t.getSeconds()).to.eql(d.getSeconds());
|
|
296
|
-
c++;
|
|
297
|
-
}, function() {
|
|
298
|
-
expect(c).to.eql(1);
|
|
299
|
-
done();
|
|
300
|
-
}, true);
|
|
301
|
-
clock.tick(1000);
|
|
302
|
-
|
|
303
|
-
clock.restore();
|
|
304
|
-
job.stop();
|
|
305
|
-
});
|
|
306
|
-
|
|
307
|
-
describe('with timezone', function() {
|
|
308
|
-
it('should run a job using cron syntax', function () {
|
|
309
|
-
var clock = sinon.useFakeTimers();
|
|
310
|
-
|
|
311
|
-
var c = 0;
|
|
312
|
-
|
|
313
|
-
var moment = require("moment-timezone");
|
|
314
|
-
var zone = "America/Chicago";
|
|
315
|
-
|
|
316
|
-
// New Orleans time
|
|
317
|
-
var t = moment();
|
|
318
|
-
t.tz(zone);
|
|
319
|
-
|
|
320
|
-
// Current time
|
|
321
|
-
d = moment();
|
|
322
|
-
|
|
323
|
-
// If current time is New Orleans time, switch to Los Angeles..
|
|
324
|
-
if (t.hours() === d.hours()) {
|
|
325
|
-
zone = "America/Los_Angeles";
|
|
326
|
-
t.tz(zone);
|
|
327
|
-
}
|
|
328
|
-
expect(d.hours()).to.not.eql(t.hours());
|
|
329
|
-
|
|
330
|
-
// If t = 59s12m then t.setSeconds(60)
|
|
331
|
-
// becones 00s13m so we're fine just doing
|
|
332
|
-
// this and no testRun callback.
|
|
333
|
-
t.add(1, 's');
|
|
334
|
-
// Run a job designed to be executed at a given
|
|
335
|
-
// time in `zone`, making sure that it is a different
|
|
336
|
-
// hour than local time.
|
|
337
|
-
var job = new cron.CronJob(t.seconds() + ' ' + t.minutes() + ' ' + t.hours() + ' * * *', function(){
|
|
338
|
-
c++;
|
|
339
|
-
}, null, true, zone);
|
|
340
|
-
|
|
341
|
-
clock.tick(1000);
|
|
342
|
-
clock.restore();
|
|
343
|
-
job.stop();
|
|
344
|
-
expect(c).to.eql(1);
|
|
345
|
-
});
|
|
346
|
-
|
|
347
|
-
it('should run a job using a date', function () {
|
|
348
|
-
var c = 0;
|
|
349
|
-
|
|
350
|
-
var moment = require("moment-timezone");
|
|
351
|
-
var zone = "America/Chicago";
|
|
352
|
-
|
|
353
|
-
// New Orleans time
|
|
354
|
-
var t = moment();
|
|
355
|
-
t.tz(zone);
|
|
356
|
-
|
|
357
|
-
// Current time
|
|
358
|
-
d = moment();
|
|
359
|
-
|
|
360
|
-
// If current time is New Orleans time, switch to Los Angeles..
|
|
361
|
-
if (t.hours() === d.hours()) {
|
|
362
|
-
zone = "America/Los_Angeles";
|
|
363
|
-
t.tz(zone);
|
|
364
|
-
}
|
|
365
|
-
expect(d.hours()).to.not.eql(t.hours());
|
|
366
|
-
|
|
367
|
-
d.add(1, 's');
|
|
368
|
-
var clock = sinon.useFakeTimers(d._d.getTime());
|
|
369
|
-
|
|
370
|
-
var job = new cron.CronJob(d._d, function() {
|
|
371
|
-
c++;
|
|
372
|
-
}, null, true, zone);
|
|
373
|
-
|
|
374
|
-
clock.tick(1000);
|
|
375
|
-
clock.restore();
|
|
376
|
-
job.stop();
|
|
377
|
-
expect(c).to.eql(1);
|
|
378
|
-
});
|
|
379
|
-
|
|
380
|
-
it('should test if timezone is valid.', function () {
|
|
381
|
-
expect(function () {
|
|
382
|
-
new cron.CronJob({
|
|
383
|
-
cronTime: '* * * * * *',
|
|
384
|
-
onTick: function (){},
|
|
385
|
-
timeZone: 'fake/timezone'
|
|
386
|
-
});
|
|
387
|
-
}).to.throw(Error);
|
|
388
|
-
});
|
|
389
|
-
});
|
|
390
|
-
|
|
391
|
-
it('should wait and not fire immediately', function() {
|
|
392
|
-
var clock = sinon.useFakeTimers();
|
|
393
|
-
var c = 0;
|
|
394
|
-
|
|
395
|
-
var d = new Date().getTime() + 31 * 86400 * 1000;
|
|
396
|
-
|
|
397
|
-
var job = cron.job(new Date(d), function() {
|
|
398
|
-
c++;
|
|
399
|
-
});
|
|
400
|
-
job.start();
|
|
401
|
-
|
|
402
|
-
clock.tick(1000);
|
|
403
|
-
|
|
404
|
-
clock.restore();
|
|
405
|
-
job.stop();
|
|
406
|
-
expect(c).to.eql(0);
|
|
407
|
-
});
|
|
408
|
-
|
|
409
|
-
it('should start, change time, start again', function() {
|
|
410
|
-
var c = 0;
|
|
411
|
-
var clock = sinon.useFakeTimers();
|
|
412
|
-
|
|
413
|
-
var job = new cron.CronJob('* * * * * *', function() {
|
|
414
|
-
c++;
|
|
415
|
-
});
|
|
416
|
-
|
|
417
|
-
job.start();
|
|
418
|
-
clock.tick(1000);
|
|
419
|
-
|
|
420
|
-
job.stop();
|
|
421
|
-
var time = cron.time('*/2 * * * * *');
|
|
422
|
-
job.setTime(time);
|
|
423
|
-
job.start();
|
|
424
|
-
|
|
425
|
-
clock.tick(4000);
|
|
426
|
-
|
|
427
|
-
clock.restore();
|
|
428
|
-
job.stop();
|
|
429
|
-
expect(c).to.eql(3);
|
|
430
|
-
});
|
|
431
|
-
|
|
432
|
-
it('should start, change time, exception', function() {
|
|
433
|
-
var c = 0;
|
|
434
|
-
var clock = sinon.useFakeTimers();
|
|
435
|
-
|
|
436
|
-
var job = new cron.CronJob('* * * * * *', function() {
|
|
437
|
-
c++;
|
|
438
|
-
});
|
|
439
|
-
|
|
440
|
-
var time = new Date();
|
|
441
|
-
job.start();
|
|
442
|
-
|
|
443
|
-
clock.tick(1000);
|
|
444
|
-
|
|
445
|
-
job.stop();
|
|
446
|
-
expect(function() {
|
|
447
|
-
job.setTime(time);
|
|
448
|
-
}).to.throw(Error);
|
|
449
|
-
|
|
450
|
-
clock.restore();
|
|
451
|
-
job.stop();
|
|
452
|
-
expect(c).to.eql(1);
|
|
453
|
-
});
|
|
454
|
-
|
|
455
|
-
it('should scope onTick to running job', function() {
|
|
456
|
-
var clock = sinon.useFakeTimers();
|
|
457
|
-
|
|
458
|
-
var job = new cron.CronJob('* * * * * *', function() {
|
|
459
|
-
expect(job).to.be.instanceOf(cron.CronJob);
|
|
460
|
-
expect(job).to.eql(this);
|
|
461
|
-
}, null, true);
|
|
462
|
-
|
|
463
|
-
clock.tick(1000);
|
|
464
|
-
|
|
465
|
-
clock.restore();
|
|
466
|
-
job.stop();
|
|
467
|
-
});
|
|
468
|
-
|
|
469
|
-
it('should scope onTick to object', function() {
|
|
470
|
-
var clock = sinon.useFakeTimers();
|
|
471
|
-
|
|
472
|
-
var job = new cron.CronJob('* * * * * *', function() {
|
|
473
|
-
expect(this.hello).to.eql('world');
|
|
474
|
-
expect(job).to.not.eql(this);
|
|
475
|
-
}, null, true, null, {'hello':'world'});
|
|
476
|
-
|
|
477
|
-
clock.tick(1000);
|
|
478
|
-
|
|
479
|
-
clock.restore();
|
|
480
|
-
job.stop();
|
|
481
|
-
});
|
|
482
|
-
|
|
483
|
-
it('should scope onTick to object within contstructor object', function() {
|
|
484
|
-
var clock = sinon.useFakeTimers();
|
|
485
|
-
|
|
486
|
-
var job = new cron.CronJob({
|
|
487
|
-
cronTime: '* * * * * *',
|
|
488
|
-
onTick: function() {
|
|
489
|
-
expect(this.hello).to.eql('world');
|
|
490
|
-
expect(job).to.not.eql(this);
|
|
491
|
-
},
|
|
492
|
-
start: true,
|
|
493
|
-
context: {hello: 'world'}
|
|
494
|
-
});
|
|
495
|
-
|
|
496
|
-
clock.tick(1000);
|
|
497
|
-
|
|
498
|
-
clock.restore();
|
|
499
|
-
job.stop();
|
|
500
|
-
});
|
|
501
|
-
|
|
502
|
-
it('should not get into an infinite loop on invalid times', function() {
|
|
503
|
-
var clock = sinon.useFakeTimers();
|
|
504
|
-
|
|
505
|
-
var invalid1 = new cron.CronJob('* 60 * * * *', function() {
|
|
506
|
-
assert.ok(true);
|
|
507
|
-
}, null, true);
|
|
508
|
-
var invalid2 = new cron.CronJob('* * 24 * * *', function() {
|
|
509
|
-
assert.ok(true);
|
|
510
|
-
}, null, true);
|
|
511
|
-
|
|
512
|
-
clock.tick(1000);
|
|
513
|
-
|
|
514
|
-
// assert that it gets here
|
|
515
|
-
invalid1.stop();
|
|
516
|
-
invalid2.stop();
|
|
517
|
-
|
|
518
|
-
clock.restore();
|
|
519
|
-
});
|
|
520
|
-
|
|
521
|
-
it('should test start of month', function() {
|
|
522
|
-
var c = 0;
|
|
523
|
-
var d = new Date('12/31/2014');
|
|
524
|
-
d.setSeconds(59);
|
|
525
|
-
d.setMinutes(59);
|
|
526
|
-
d.setHours(23);
|
|
527
|
-
var clock = sinon.useFakeTimers(d.getTime());
|
|
528
|
-
|
|
529
|
-
var job = new cron.CronJob('0 0 0 1 * *', function() {
|
|
530
|
-
c++;
|
|
531
|
-
}, null, true);
|
|
532
|
-
|
|
533
|
-
clock.tick(1001);
|
|
534
|
-
expect(c).to.eql(1);
|
|
535
|
-
|
|
536
|
-
clock.tick(2678399001);
|
|
537
|
-
expect(c).to.eql(1);
|
|
538
|
-
|
|
539
|
-
clock.tick(2678400001); //jump over 2 firsts
|
|
540
|
-
clock.restore();
|
|
541
|
-
job.stop();
|
|
542
|
-
|
|
543
|
-
expect(c).to.eql(3);
|
|
544
|
-
});
|
|
545
|
-
|
|
546
|
-
it('should not fire if time was adjusted back', function() {
|
|
547
|
-
var c = 0;
|
|
548
|
-
var clock = sinon.useFakeTimers('setTimeout');
|
|
549
|
-
|
|
550
|
-
var job = new cron.CronJob('0 * * * * *', function() {
|
|
551
|
-
c++;
|
|
552
|
-
}, null, true);
|
|
553
|
-
|
|
554
|
-
clock.tick(60000);
|
|
555
|
-
expect(c).to.eql(0);
|
|
556
|
-
|
|
557
|
-
clock.restore();
|
|
558
|
-
job.stop();
|
|
559
|
-
});
|
|
560
|
-
|
|
561
|
-
it('should run every second monday');
|
|
562
|
-
});
|
package/tests/test-crontime.js
DELETED
|
@@ -1,193 +0,0 @@
|
|
|
1
|
-
var chai = require('chai'),
|
|
2
|
-
expect = chai.expect,
|
|
3
|
-
cron = require('../lib/cron');
|
|
4
|
-
|
|
5
|
-
describe('crontime', function() {
|
|
6
|
-
it('should test stars (* * * * * *)', function() {
|
|
7
|
-
expect(function() {
|
|
8
|
-
new cron.CronTime('* * * * * *');
|
|
9
|
-
}).to.not.throw(Error);
|
|
10
|
-
});
|
|
11
|
-
|
|
12
|
-
it('should test digit (0 * * * * *)', function() {
|
|
13
|
-
expect(function() {
|
|
14
|
-
new cron.CronTime('0 * * * * *');
|
|
15
|
-
}).to.not.throw(Error);
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
it('should test multi digits (08 * * * * *)', function() {
|
|
19
|
-
expect(function() {
|
|
20
|
-
new cron.CronTime('08 * * * * *');
|
|
21
|
-
}).to.not.throw(Error);
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
it('should test all digits (08 8 8 8 8 5)', function() {
|
|
25
|
-
expect(function() {
|
|
26
|
-
new cron.CronTime('08 * * * * *');
|
|
27
|
-
}).to.not.throw(Error);
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
it('should test too many digits (08 8 8 8 8 5)', function() {
|
|
31
|
-
expect(function() {
|
|
32
|
-
new cron.CronTime('08 * * * * *');
|
|
33
|
-
}).to.not.throw(Error);
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
it('should test standard cron format (* * * * *)', function() {
|
|
37
|
-
expect(function() {
|
|
38
|
-
new cron.CronTime('* * * * *');
|
|
39
|
-
}).to.not.throw(Error);
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
it('should test standard cron format (8 8 8 8 5)', function() {
|
|
43
|
-
var standard = new cron.CronTime('8 8 8 8 5');
|
|
44
|
-
var extended = new cron.CronTime('0 8 8 8 8 5');
|
|
45
|
-
|
|
46
|
-
expect(standard.dayOfWeek).to.deep.eql(extended.dayOfWeek);
|
|
47
|
-
expect(standard.month).to.deep.eql(extended.month);
|
|
48
|
-
expect(standard.dayOfMonth).to.deep.eql(extended.dayOfMonth);
|
|
49
|
-
expect(standard.hour).to.deep.eql(extended.hour);
|
|
50
|
-
expect(standard.minute).to.deep.eql(extended.minute);
|
|
51
|
-
expect(standard.second).to.deep.eql(extended.second);
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
it('should test hyphen (0-10 * * * * *)', function() {
|
|
55
|
-
expect(function() {
|
|
56
|
-
new cron.CronTime('0-10 * * * * *');
|
|
57
|
-
}).to.not.throw(Error);
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
it('should test multi hyphens (0-10 0-10 * * * *)', function() {
|
|
61
|
-
expect(function() {
|
|
62
|
-
new cron.CronTime('0-10 0-10 * * * *');
|
|
63
|
-
}).to.not.throw(Error);
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
it('should test all hyphens (0-10 0-10 0-10 0-10 0-10 0-1)', function() {
|
|
67
|
-
expect(function() {
|
|
68
|
-
new cron.CronTime('0-10 0-10 0-10 0-10 0-10 0-1');
|
|
69
|
-
}).to.not.throw(Error);
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
it('should test comma (0,10 * * * * *)', function() {
|
|
73
|
-
expect(function() {
|
|
74
|
-
new cron.CronTime('0,10 * * * * *');
|
|
75
|
-
}).to.not.throw(Error);
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
it('should test multi commas (0,10 0,10 * * * *)', function() {
|
|
79
|
-
expect(function() {
|
|
80
|
-
new cron.CronTime('0,10 0,10 * * * *');
|
|
81
|
-
}).to.not.throw(Error);
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
it('should test all commas (0,10 0,10 0,10 0,10 0,10 0,1)', function() {
|
|
85
|
-
expect(function() {
|
|
86
|
-
new cron.CronTime('0,10 0,10 0,10 0,10 0,10 0,1');
|
|
87
|
-
}).to.not.throw(Error);
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
it('should test alias (* * * * jan *)', function() {
|
|
91
|
-
expect(function() {
|
|
92
|
-
new cron.CronTime('* * * * jan *');
|
|
93
|
-
}).to.not.throw(Error);
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
it('should test multi aliases (* * * * jan,feb *)', function() {
|
|
97
|
-
expect(function() {
|
|
98
|
-
new cron.CronTime('* * * * jan,feb *');
|
|
99
|
-
}).to.not.throw(Error);
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
it('should test all aliases (* * * * jan,feb mon,tue)', function() {
|
|
103
|
-
expect(function() {
|
|
104
|
-
new cron.CronTime('* * * * jan,feb mon,tue');
|
|
105
|
-
}).to.not.throw(Error);
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
it('should test every second monday (* * * * * mon/2)');
|
|
109
|
-
|
|
110
|
-
it('should test unknown alias (* * * * jar *)', function() {
|
|
111
|
-
expect(function() {
|
|
112
|
-
new cron.CronTime('* * * * jar *');
|
|
113
|
-
}).to.throw(Error);
|
|
114
|
-
});
|
|
115
|
-
|
|
116
|
-
it('should test unknown alias - short (* * * * j *)', function() {
|
|
117
|
-
expect(function() {
|
|
118
|
-
new cron.CronTime('* * * * j *');
|
|
119
|
-
}).to.throw(Error);
|
|
120
|
-
});
|
|
121
|
-
|
|
122
|
-
it('should test Date', function() {
|
|
123
|
-
var d = new Date();
|
|
124
|
-
var ct = new cron.CronTime(d);
|
|
125
|
-
expect(ct.source.isSame(d.getTime())).to.be.true
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
it('should test day roll-over', function() {
|
|
129
|
-
var numHours = 24;
|
|
130
|
-
var ct = new cron.CronTime('0 0 17 * * *');
|
|
131
|
-
|
|
132
|
-
for (var hr = 0; hr < numHours; hr++) {
|
|
133
|
-
var start = new Date(2012, 3, 16, hr, 30, 30);
|
|
134
|
-
var next = ct._getNextDateFrom(start);
|
|
135
|
-
|
|
136
|
-
expect(next - start).to.be.lt(24*60*60*1000);
|
|
137
|
-
expect(next).to.be.gt(start);
|
|
138
|
-
}
|
|
139
|
-
});
|
|
140
|
-
|
|
141
|
-
it('should test illegal repetition syntax', function() {
|
|
142
|
-
expect(function(){
|
|
143
|
-
new cron.CronTime('* * /4 * * *');
|
|
144
|
-
}).to.throw(Error);
|
|
145
|
-
});
|
|
146
|
-
|
|
147
|
-
it('should test next date', function() {
|
|
148
|
-
var ct = new cron.CronTime('0 0 */4 * * *');
|
|
149
|
-
|
|
150
|
-
var nextDate = new Date();
|
|
151
|
-
nextDate.setHours(23);
|
|
152
|
-
var nextdt = ct._getNextDateFrom(nextDate);
|
|
153
|
-
|
|
154
|
-
expect(nextdt).to.be.gt(nextDate);
|
|
155
|
-
expect(nextdt.hours() % 4).to.eql(0);
|
|
156
|
-
});
|
|
157
|
-
|
|
158
|
-
it('should test next date from invalid date', function() {
|
|
159
|
-
var ct = new cron.CronTime('0 0 * * * *');
|
|
160
|
-
var nextDate = new Date('My invalid date string');
|
|
161
|
-
var nextdt = ct._getNextDateFrom(nextDate);
|
|
162
|
-
|
|
163
|
-
expect(nextdt.toString()).to.eql('Invalid date');
|
|
164
|
-
});
|
|
165
|
-
|
|
166
|
-
it('should test next real date', function() {
|
|
167
|
-
var ct = new cron.CronTime(new Date());
|
|
168
|
-
|
|
169
|
-
var nextDate = new Date();
|
|
170
|
-
nextDate.setMonth(nextDate.getMonth()+1);
|
|
171
|
-
expect(nextDate).to.be.gt(ct.source);
|
|
172
|
-
var nextdt = ct._getNextDateFrom(nextDate);
|
|
173
|
-
expect(nextdt.isSame(nextDate)).to.be.true;
|
|
174
|
-
});
|
|
175
|
-
|
|
176
|
-
it('should test next month selection');
|
|
177
|
-
|
|
178
|
-
describe('should throw an exception because `L` not supported', function() {
|
|
179
|
-
|
|
180
|
-
it('(* * * L * *)', function() {
|
|
181
|
-
expect(function() {
|
|
182
|
-
new cron.CronTime('* * * L * *');
|
|
183
|
-
}).to.throw(Error);
|
|
184
|
-
|
|
185
|
-
});
|
|
186
|
-
|
|
187
|
-
it('(* * * * * L)', function() {
|
|
188
|
-
expect(function() {
|
|
189
|
-
new cron.CronTime('* * * * * L');
|
|
190
|
-
}).to.throw(Error);
|
|
191
|
-
});
|
|
192
|
-
});
|
|
193
|
-
});
|