cron 1.5.0 → 1.7.1

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 CHANGED
@@ -1,9 +1,11 @@
1
1
  language: node_js
2
2
  node_js:
3
- - "4"
4
- - "6"
5
- - "8"
6
- - "10"
3
+ - '4'
4
+ - '6'
5
+ - '8'
6
+ - '10'
7
7
  notifications:
8
8
  email:
9
9
  on_success: never
10
+ slack:
11
+ secure: hHI67BDSUiJQeEiJI/+XJzR/oSsjYPoylhqY10WIZWXfs4TIrdLe513Fzt9BnDNtxpHnCqkgqCb1lF8gNkkOhJKezfDwOZK6HKpNp+zfn7WqBxL0VB3rPw3JoOO9JWoELwV2LyUutbw7m4+OMO45eWgQmQM4mddUMHjyV0awa7A=
package/CHANGELOG.md ADDED
@@ -0,0 +1,9 @@
1
+ ## [v1.7.1] - 2019-04-26
2
+ - GH-416 - Fix issue where next execution time is incorrect in some cases in Naoya Inada <naoina@kuune.org> in c08522ff80b3987843e9930c307b76d5fe38b5dc
3
+
4
+ ## [v1.7.0] - 2019-03-19
5
+ - GH-408 - DST issue by Shua Talansky <shua@bitbean.com> in 1e971fd6dfa6ba4b0469d99dd64e6c31189d17d3 and 849a2467d16216a9dfa818c57cc26be6b6d0899b
6
+
7
+ ## [v1.6.0] - 2018-11-15
8
+ - GH-393, GH-394 - Remove hard limit on max iters in favor of a timeout by Nick Campbell <nicholas.j.campbell@gmail.com> in 57632b0c06c56e82f40b740b8d7986be43842735
9
+ - GH-390 - better handling of real dates which are in the past by Nick Campbell <nicholas.j.campbell@gmail.com> in 7cbcc984aea6ec063e38829f68eb9bc0dfb1c775
package/README.md CHANGED
@@ -90,6 +90,29 @@ week where both 0 and 7 represent Sunday. We do not.
90
90
  * Day of Week: 0-6 (Sun-Sat)
91
91
 
92
92
 
93
+ Gotchas
94
+ ==
95
+
96
+ * Millisecond level granularity in JS or moment date objects.
97
+ Because computers take time to do things, there may be some delay in execution.
98
+ This should be on the order of milliseconds. This module doesn't allow MS level
99
+ granularity for the regular cron syntax, but _does_ allow you to specify a real
100
+ date of execution in either a javascript date object or a moment object.
101
+ When this happens you may find that you aren't able to execute a job that
102
+ _should_ run in the future like with `new Date().setMilliseconds(new
103
+ Date().getMilliseconds() + 1)`. This is due to those cycles of execution
104
+ above. This wont be the same for everyone because of compute speed. When I
105
+ tried it locally I saw that somewhere around the 4-5 ms mark was where I got
106
+ consistent ticks using real dates, but anything less than that would result
107
+ in an exception. This could be really confusing. We could restrict the
108
+ granularity for all dates to seconds, but felt that it wasn't a huge problem
109
+ so long as you were made aware. If this becomes more of an issue, We can
110
+ revisit it.
111
+ * Arrow Functions for `onTick`
112
+ Arrow functions get their `this` context from their parent scope. Thus, if you use them, you will not get
113
+ the `this` context of the cronjob. You can read a little more in this ticket [GH-40](https://github.com/kelektiv/node-cron/issues/47#issuecomment-459762775)
114
+
115
+
93
116
  API
94
117
  ==
95
118
 
@@ -0,0 +1,9 @@
1
+ const CronJob = require('../lib/cron.js').CronJob;
2
+
3
+ console.log('Before job instantiation');
4
+ const job = new CronJob('00 00 00 * * *', function() {
5
+ const d = new Date();
6
+ console.log('Midnight:', d);
7
+ });
8
+ console.log('After job instantiation');
9
+ job.start();
@@ -0,0 +1,24 @@
1
+ const CronJob = require('../lib/cron.js').CronJob;
2
+
3
+ const job = new CronJob(
4
+ '0 * * * * *',
5
+ function() {
6
+ console.log('Date: ', new Date());
7
+ },
8
+ null,
9
+ true
10
+ );
11
+
12
+ console.log('System TZ next 5: ', job.nextDates(5));
13
+
14
+ const jobUTC = new CronJob(
15
+ '0 * * * * *',
16
+ function() {
17
+ console.log('Date: ', new Date());
18
+ },
19
+ null,
20
+ true,
21
+ 'UTC'
22
+ );
23
+
24
+ console.log('UTC next 5: ', jobUTC.nextDates(5));
@@ -0,0 +1,19 @@
1
+ const CronJob = require('../lib/cron.js').CronJob;
2
+
3
+ // XXX: SEE README GOTCHAS ABOUT WHY THIS COULD BE IN THE PAST!
4
+ let d = new Date();
5
+ d.setMilliseconds(d.getMilliseconds() + 1);
6
+
7
+ console.log('Before job instantiation');
8
+ const job = new CronJob(
9
+ d,
10
+ () => {
11
+ const d2 = new Date();
12
+ console.log('Tick @:', d2);
13
+ },
14
+ () => {
15
+ console.log('complete');
16
+ }
17
+ );
18
+ console.log('After job instantiation');
19
+ job.start();
package/lib/cron.js CHANGED
@@ -32,7 +32,7 @@
32
32
  this.zone = zone;
33
33
  }
34
34
 
35
- if (utcOffset) this.utcOffset = utcOffset;
35
+ if (typeof utcOffset !== 'undefined') this.utcOffset = utcOffset;
36
36
 
37
37
  var that = this;
38
38
  timeUnits.map(function(timeUnit) {
@@ -138,11 +138,16 @@
138
138
  date = date.tz(this.zone);
139
139
  }
140
140
 
141
- if (this.utcOffset) {
141
+ if (typeof this.utcOffset !== 'undefined') {
142
142
  date = date.utcOffset(this.utcOffset);
143
143
  }
144
144
 
145
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
+
146
151
  return date;
147
152
  }
148
153
 
@@ -213,25 +218,15 @@
213
218
  throw new Error('ERROR: You specified an invalid date.');
214
219
  }
215
220
 
216
- if (this.realDate && start < new Date()) {
217
- throw new Error('WARNING: Date in past. Will never be fired.');
218
- }
219
-
220
- if (this.realDate) {
221
- return date;
222
- }
223
-
224
- /*
225
- max itterations shouldn't be more than
226
- 11 months + 30 days + 23 hours + 59 minutes + 59 seconds
227
- anything more than that value should be considered a bug.
228
- note: *2 is added incase we missed something.
229
- */
230
- var MAX_ITERATIONS = (11 + 30 + 23 + 59 + 59) * 2;
231
- var i = 0;
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 finding = true;
225
+ var findingTimeout = setTimeout(function() {
226
+ finding = false;
227
+ }, 5000);
232
228
  // determine next date
233
229
  while (true) {
234
- i++;
235
230
  var diff = date - start;
236
231
  var prevMonth = date.month();
237
232
  var prevDay = date.days();
@@ -239,11 +234,13 @@
239
234
  var prevSeconds = date.seconds();
240
235
  var origDate = new Date(date);
241
236
 
242
- if (i > MAX_ITERATIONS) {
237
+ if (!finding) {
243
238
  throw new Error(
244
- `Something went wrong. cron reached maximum iterations.
245
- Please open an issue (https://github.com/kelektiv/node-cron/issues/new) and provide the following string
246
- Time Zone: ${zone || '""'} - Cron String: ${this} - UTC offset: ${date.format('Z')} - current Date: ${moment().toString()}`
239
+ `Something went wrong. cron reached maximum iterations.
240
+ Please open an issue (https://github.com/kelektiv/node-cron/issues/new) and provide the following string
241
+ Time Zone: ${zone || '""'} - Cron String: ${this} - UTC offset: ${date.format(
242
+ 'Z'
243
+ )} - current Date: ${moment().toString()}`
247
244
  );
248
245
  }
249
246
  if (
@@ -263,7 +260,11 @@ Time Zone: ${zone || '""'} - Cron String: ${this} - UTC offset: ${date.format('Z
263
260
 
264
261
  if (
265
262
  !(date.date() in this.dayOfMonth) &&
266
- Object.keys(this.dayOfMonth).length !== 31
263
+ Object.keys(this.dayOfMonth).length !== 31 &&
264
+ !(
265
+ date.day() in this.dayOfWeek &&
266
+ Object.keys(this.dayOfWeek).length !== 7
267
+ )
267
268
  ) {
268
269
  date.add(1, 'd');
269
270
  if (date.days() === prevDay) {
@@ -277,7 +278,11 @@ Time Zone: ${zone || '""'} - Cron String: ${this} - UTC offset: ${date.format('Z
277
278
 
278
279
  if (
279
280
  !(date.day() in this.dayOfWeek) &&
280
- Object.keys(this.dayOfWeek).length !== 7
281
+ Object.keys(this.dayOfWeek).length !== 7 &&
282
+ !(
283
+ date.date() in this.dayOfMonth &&
284
+ Object.keys(this.dayOfMonth).length !== 31
285
+ )
281
286
  ) {
282
287
  date.add(1, 'd');
283
288
  if (date.days() === prevDay) {
@@ -297,9 +302,17 @@ Time Zone: ${zone || '""'} - Cron String: ${this} - UTC offset: ${date.format('Z
297
302
  Object.keys(this.hour).length !== 24
298
303
  ) {
299
304
  origDate = moment(date);
305
+ var curHour = date.hours();
300
306
  date.hours(
301
- date.hours() === 23 && diff > 86400000 ? 0 : date.hours() + 1
307
+ date.hours() === 23 && diff > 86400000 ? 0 : date.hours() + 1
302
308
  );
309
+ /*
310
+ * 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)
311
+ * We will therefore jump to 3AM if time stayed at 1AM
312
+ */
313
+ if(curHour === date.hours()){
314
+ date.hours(date.hours() + 2);
315
+ }
303
316
  date.minutes(0);
304
317
  date.seconds(0);
305
318
  if (date <= origDate) {
@@ -347,6 +360,7 @@ Time Zone: ${zone || '""'} - Cron String: ${this} - UTC offset: ${date.format('Z
347
360
  break;
348
361
  }
349
362
 
363
+ clearTimeout(findingTimeout);
350
364
  return date;
351
365
  },
352
366
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "cron",
3
3
  "description": "Cron jobs for your node",
4
- "version": "1.5.0",
4
+ "version": "1.7.1",
5
5
  "author": "Nick Campbell <nicholas.j.campbell@gmail.com> (http://github.com/ncb000gt)",
6
6
  "bugs": {
7
7
  "url": "http://github.com/kelektiv/node-cron/issues"
@@ -18,18 +18,18 @@
18
18
  "moment-timezone": "^0.5.x"
19
19
  },
20
20
  "devDependencies": {
21
- "mocha": "~5.1.x",
22
- "chai": "~4.1.x",
23
- "sinon": "~1.12.x",
24
- "eslint": "~5.3.x",
25
- "eslint-config-prettier": "~3.0.x",
26
- "eslint-config-standard": "~11.0.x",
27
- "eslint-plugin-import": "~2.14.x",
28
- "eslint-plugin-node": "~7.0.x",
29
- "eslint-plugin-prettier": "~2.6.x",
30
- "eslint-plugin-promise": "~3.8.x",
31
- "eslint-plugin-standard": "~3.1.x",
32
- "prettier": "~1.14.x"
21
+ "mocha": "~5.2.x",
22
+ "chai": "~4.2.x",
23
+ "sinon": "~7.2.x",
24
+ "eslint": "~5.13.x",
25
+ "eslint-config-prettier": "~4.0.x",
26
+ "eslint-config-standard": "~12.0.x",
27
+ "eslint-plugin-import": "~2.16.x",
28
+ "eslint-plugin-node": "~8.0.x",
29
+ "eslint-plugin-prettier": "~3.0.x",
30
+ "eslint-plugin-promise": "~4.0.x",
31
+ "eslint-plugin-standard": "~4.0.x",
32
+ "prettier": "~1.16.x"
33
33
  },
34
34
  "keywords": [
35
35
  "cron",
@@ -3,6 +3,8 @@ var expect = chai.expect;
3
3
  var sinon = require('sinon');
4
4
  var cron = require('../lib/cron');
5
5
 
6
+ /* eslint-disable no-new */
7
+
6
8
  describe('cron', function() {
7
9
  it('should run every second (* * * * * *)', function() {
8
10
  var clock = sinon.useFakeTimers();
@@ -544,7 +546,7 @@ describe('cron', function() {
544
546
 
545
547
  it('should test if timezone is valid.', function() {
546
548
  expect(function() {
547
- new cron.CronJob({ // ignore eslint error here
549
+ new cron.CronJob({
548
550
  cronTime: '* * * * * *',
549
551
  onTick: function() {},
550
552
  timeZone: 'fake/timezone'
@@ -737,7 +739,9 @@ describe('cron', function() {
737
739
 
738
740
  it('should not fire if time was adjusted back', function() {
739
741
  var c = 0;
740
- var clock = sinon.useFakeTimers('setTimeout');
742
+ var clock = sinon.useFakeTimers({
743
+ toFake: ['setTimeout']
744
+ });
741
745
 
742
746
  var job = new cron.CronJob(
743
747
  '0 * * * * *',
@@ -865,6 +869,31 @@ describe('cron', function() {
865
869
  expect(c).to.eql(8);
866
870
  });
867
871
 
872
+ it('should trigger onTick at midnight', function() {
873
+ var c = 0;
874
+ var d = new Date('12/31/2014');
875
+ d.setSeconds(59);
876
+ d.setMinutes(59);
877
+ d.setHours(23);
878
+ var clock = sinon.useFakeTimers(d.getTime());
879
+
880
+ var job = new cron.CronJob({
881
+ cronTime: '00 * * * * *',
882
+ onTick: function() {
883
+ c++;
884
+ },
885
+ start: true,
886
+ timeZone: 'UTC'
887
+ });
888
+
889
+ clock.tick(1000); // move clock 1 second
890
+ expect(c).to.eql(1);
891
+
892
+ clock.restore();
893
+ job.stop();
894
+ expect(c).to.eql(1);
895
+ });
896
+
868
897
  it('should run every day UTC', function() {
869
898
  var c = 0;
870
899
  var d = new Date('12/31/2014');
@@ -2,8 +2,9 @@ var chai = require('chai');
2
2
  var expect = chai.expect;
3
3
  var cron = require('../lib/cron');
4
4
  var moment = require('moment-timezone');
5
+ var sinon = require('sinon');
5
6
 
6
- // most eslint errors here are due to side effects. i don't care much about them right now
7
+ /* eslint-disable no-new */
7
8
 
8
9
  describe('crontime', function() {
9
10
  it('should test stars (* * * * * *)', function() {
@@ -169,13 +170,18 @@ describe('crontime', function() {
169
170
  });
170
171
 
171
172
  it('should test next real date', function() {
172
- var ct = new cron.CronTime(new Date());
173
+ var initialDate = new Date();
174
+ var ct = new cron.CronTime(initialDate);
173
175
 
174
176
  var nextDate = new Date();
175
177
  nextDate.setMonth(nextDate.getMonth() + 1);
176
178
  expect(nextDate).to.be.gt(ct.source._d);
177
- var nextdt = ct._getNextDateFrom(nextDate);
178
- expect(nextdt.isSame(nextDate)).to.be.true;
179
+ var nextdt = ct.sendAt(0);
180
+ // there shouldn't be a "next date" when using a real date.
181
+ // execution happens once
182
+ // so the return should be the date passed in unless explicitly reset
183
+ expect(nextdt.isBefore(nextDate)).to.be.true;
184
+ expect(nextdt.isSame(initialDate)).to.be.true;
179
185
  });
180
186
 
181
187
  it('should test next month selection');
@@ -297,4 +303,52 @@ describe('crontime', function() {
297
303
  new Date(Date.UTC(2019, 1, 1, 0, 0)).valueOf()
298
304
  );
299
305
  });
306
+ it('should generate the right next day when cron is set to both day of the month and day of the week (1)', function() {
307
+ var cronTime = new cron.CronTime('0 8 1 * 4');
308
+ var previousDate = new Date(Date.UTC(2019, 3, 22, 0, 0));
309
+ var nextDate = cronTime._getNextDateFrom(previousDate, 'UTC');
310
+ expect(nextDate.valueOf()).to.equal(
311
+ new Date(Date.UTC(2019, 3, 25, 8, 0)).valueOf()
312
+ );
313
+ });
314
+ it('should generate the right next day when cron is set to both day of the month and day of the week (2)', function() {
315
+ var cronTime = new cron.CronTime('0 8 1 * 4');
316
+ var previousDate = new Date(Date.UTC(2019, 3, 26, 0, 0));
317
+ var nextDate = cronTime._getNextDateFrom(previousDate, 'UTC');
318
+ expect(nextDate.valueOf()).to.equal(
319
+ new Date(Date.UTC(2019, 4, 1, 8, 0)).valueOf()
320
+ );
321
+ });
322
+ it('should generate the right next day when cron is set to both day of the month and day of the week (3)', function() {
323
+ var cronTime = new cron.CronTime('0 8 1 * 4');
324
+ var previousDate = new Date(Date.UTC(2019, 7, 1, 7, 59));
325
+ var nextDate = cronTime._getNextDateFrom(previousDate, 'UTC');
326
+ expect(nextDate.valueOf()).to.equal(
327
+ new Date(Date.UTC(2019, 7, 1, 8, 0)).valueOf()
328
+ );
329
+ });
330
+
331
+ it('should accept 0 as a valid UTC offset', function() {
332
+ var clock = sinon.useFakeTimers();
333
+
334
+ var cronTime = new cron.CronTime('0 11 * * *', null, 0);
335
+ var expected = moment().add(11, 'hours').unix();
336
+ var actual = cronTime.sendAt().unix();
337
+
338
+ expect(actual).to.equal(expected);
339
+
340
+ clock.restore();
341
+ });
342
+
343
+ it('should accept -120 as a valid UTC offset', function() {
344
+ var clock = sinon.useFakeTimers();
345
+
346
+ var cronTime = new cron.CronTime('0 11 * * *', null, -120);
347
+ var expected = moment().add(13, 'hours').unix();
348
+ var actual = cronTime.sendAt().unix();
349
+
350
+ expect(actual).to.equal(expected);
351
+
352
+ clock.restore();
353
+ });
300
354
  });