cron 1.5.1 → 1.6.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 CHANGED
@@ -90,6 +90,26 @@ 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
+
112
+
93
113
  API
94
114
  ==
95
115
 
@@ -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
@@ -143,6 +143,11 @@
143
143
  }
144
144
 
145
145
  if (this.realDate) {
146
+ const diff = moment().diff(date);
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 (
@@ -347,6 +344,7 @@ Time Zone: ${zone || '""'} - Cron String: ${this} - UTC offset: ${date.format('Z
347
344
  break;
348
345
  }
349
346
 
347
+ clearTimeout(findingTimeout);
350
348
  return date;
351
349
  },
352
350
 
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.1",
4
+ "version": "1.6.0",
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"
@@ -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'
@@ -4,7 +4,7 @@ var cron = require('../lib/cron');
4
4
  var moment = require('moment-timezone');
5
5
  var sinon = require('sinon');
6
6
 
7
- // most eslint errors here are due to side effects. i don't care much about them right now
7
+ /* eslint-disable no-new */
8
8
 
9
9
  describe('crontime', function() {
10
10
  it('should test stars (* * * * * *)', function() {
@@ -170,13 +170,18 @@ describe('crontime', function() {
170
170
  });
171
171
 
172
172
  it('should test next real date', function() {
173
- var ct = new cron.CronTime(new Date());
173
+ var initialDate = new Date()
174
+ var ct = new cron.CronTime(initialDate);
174
175
 
175
176
  var nextDate = new Date();
176
177
  nextDate.setMonth(nextDate.getMonth() + 1);
177
178
  expect(nextDate).to.be.gt(ct.source._d);
178
- var nextdt = ct._getNextDateFrom(nextDate);
179
- 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;
180
185
  });
181
186
 
182
187
  it('should test next month selection');