node-schedule 1.2.5 → 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 CHANGED
@@ -58,7 +58,7 @@ The cron format consists of:
58
58
  ```
59
59
  * * * * * *
60
60
  ┬ ┬ ┬ ┬ ┬ ┬
61
- │ │ │ │ │ |
61
+ │ │ │ │ │ │
62
62
  │ │ │ │ │ └ day of week (0 - 7) (0 or 7 is Sun)
63
63
  │ │ │ │ └───── month (1 - 12)
64
64
  │ │ │ └────────── day of month (1 - 31)
@@ -89,6 +89,13 @@ var j = schedule.scheduleJob('0 17 ? * 0,4-6', function(){
89
89
 
90
90
  Execute a cron job every 5 Minutes = */5 * * * *
91
91
 
92
+ You can also get when it is scheduled to run for every invocation of the job:
93
+ ```js
94
+ var j = schedule.scheduleJob('0 1 * * *', function(fireDate){
95
+ console.log('This job was supposed to run at ' + fireDate + ', but actually ran at ' + new Date());
96
+ });
97
+ ```
98
+ This is useful when you need to check if there is a delay of the job invocation when the system is busy, or save a record of all invocations of a job for audit purpose.
92
99
  #### Unsupported Cron Features
93
100
 
94
101
  Currently, `W` (nearest weekday), `L` (last day of month/week), and `#` (nth weekday
package/lib/schedule.js CHANGED
@@ -167,12 +167,12 @@ function Job(name, job, callback) {
167
167
 
168
168
  util.inherits(Job, events.EventEmitter);
169
169
 
170
- Job.prototype.invoke = function() {
170
+ Job.prototype.invoke = function(fireDate) {
171
171
  if (typeof this.job == 'function') {
172
172
  this.setTriggeredJobs(this.triggeredJobs() + 1);
173
- this.job();
173
+ this.job(fireDate);
174
174
  } else {
175
- this.job.execute();
175
+ this.job.execute(fireDate);
176
176
  }
177
177
  };
178
178
 
@@ -491,10 +491,15 @@ function recurMatch(val, matcher) {
491
491
 
492
492
  /* Date-based scheduler */
493
493
  function runOnDate(date, job) {
494
- var now = (new Date()).getTime();
494
+ var now = Date.now();
495
495
  var then = date.getTime();
496
496
 
497
- return lt.setTimeout(job, (then < now ? 0 : then - now));
497
+ return lt.setTimeout(function() {
498
+ if (then > Date.now())
499
+ runOnDate(date, job);
500
+ else
501
+ job();
502
+ }, (then < now ? 0 : then - now));
498
503
  }
499
504
 
500
505
  var invocations = [];
@@ -535,7 +540,7 @@ function prepareNextInvocation() {
535
540
 
536
541
  job.stopTrackingInvocation(cinv);
537
542
 
538
- job.invoke();
543
+ job.invoke(cinv.fireDate instanceof CronDate ? cinv.fireDate.toDate() : cinv.fireDate);
539
544
  job.emit('run');
540
545
  });
541
546
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-schedule",
3
- "version": "1.2.5",
3
+ "version": "1.3.0",
4
4
  "description": "A cron-like and not-cron-like job scheduler for Node.",
5
5
  "keywords": [
6
6
  "schedule",
package/test/.eslintrc ADDED
@@ -0,0 +1,5 @@
1
+ {
2
+ "rules": {
3
+ "quotes": ["off"]
4
+ }
5
+ }
@@ -0,0 +1,24 @@
1
+ 'use strict';
2
+
3
+ var schedule = require('../lib/schedule');
4
+
5
+ module.exports = {
6
+ 'Cancel Long Running Job': {
7
+ 'should work even when recurring jobs are to be run on the past': function (test) {
8
+ var ok = true;
9
+ var job = schedule.scheduleJob('*/1 * * * * *', function () {
10
+ test.ok(ok);
11
+ var time = Date.now();
12
+ while (ok && (Date.now() - time < 2000)) {
13
+ }
14
+ });
15
+
16
+ test.ok(job);
17
+ setTimeout(function () {
18
+ job.cancel();
19
+ test.done();
20
+ ok = false;
21
+ }, 2100);
22
+ }
23
+ }
24
+ };