Comparing version 0.3.3 to 0.3.4
@@ -279,15 +279,55 @@ var CronDate = Date; | ||
if(this.running) return; | ||
var MAXDELAY = 2147483647; // The maximum number of milliseconds setTimeout will wait. | ||
var self = this; | ||
var timeout = this.cronTime.getTimeout(); | ||
var remaining = 0; | ||
if (this.cronTime.realDate) this.runOnce = true; | ||
// The callback wrapper checks if it needs to sleep another period or not | ||
// and does the real callback logic when it's time. | ||
function callbackWrapper() { | ||
// If there is sleep time remaining, calculate how long and go to sleep | ||
// again. This processing might make us miss the deadline by a few ms | ||
// times the number of sleep sessions. Given a MAXDELAY of almost a | ||
// month, this should be no issue. | ||
if (remaining) { | ||
if (remaining > MAXDELAY) { | ||
remaining -= MAXDELAY; | ||
timeout = MAXDELAY; | ||
} else { | ||
timeout = remaining; | ||
remaining = 0; | ||
} | ||
self._timeout = setTimeout(callbackWrapper, timeout); | ||
} else { | ||
// We have arrived at the correct point in time. | ||
self.running = false; | ||
//start before calling back so the callbacks have the ability to stop the cron job | ||
if (!(self.runOnce)) self.start(); | ||
self._callback(); | ||
} | ||
} | ||
if (timeout >= 0) { | ||
this.running = true; | ||
this._timeout = setTimeout(function(self) { | ||
self.running = false; | ||
//start before calling back so the callbacks have the ability to stop the cron job | ||
if (!(self.runOnce)) self.start(); | ||
// Don't try to sleep more than MAXDELAY ms at a time. | ||
self._callback(); | ||
}, timeout, this); | ||
if (timeout > MAXDELAY) { | ||
remaining = timeout - MAXDELAY; | ||
console.log('WARNING: timeout specified (' + timeout + ') was greater than the max of ' + MAXDELAY + '.'); | ||
timeout = MAXDELAY; | ||
} | ||
this._timeout = setTimeout(callbackWrapper, timeout); | ||
} else { | ||
@@ -294,0 +334,0 @@ this.stop(); |
{ | ||
"name": "cron", | ||
"description": "CronJob's for your node", | ||
"version": "0.3.3", | ||
"version": "0.3.4", | ||
"author": "Nick Campbell <nicholas.j.campbell@gmail.com> (http://github.com/ncb000gt)", | ||
@@ -6,0 +6,0 @@ "bugs" : { |
@@ -296,3 +296,17 @@ var testCase = require('nodeunit').testCase, | ||
}, 5250); | ||
}, | ||
'test long wait should not fire immediately': function(assert) { | ||
assert.expect(1); | ||
var count = 0; | ||
var d = new Date().getTime() + 31 * 86400 * 1000; | ||
var job = cron.job(new Date(d), function() { | ||
assert.ok(false); | ||
}); | ||
job.start(); | ||
setTimeout(function() { | ||
job.stop(); | ||
assert.ok(true); | ||
assert.done(); | ||
}, 250); | ||
} | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Non-existent author
Supply chain riskThe package was published by an npm account that no longer exists.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
28237
719
1