Comparing version 1.0.1 to 1.0.2
@@ -6,3 +6,3 @@ | ||
Isomorphic JavaScript cron parser. | ||
Pure JavaScript Isomorphic cron parser and scheduler without dependencies. | ||
@@ -229,2 +229,8 @@ ------------------------------------------------------------------------------------ | ||
self.schedulerDefaults = { | ||
stopAt: Infinity, | ||
maxRuns: Infinity, | ||
kill: false | ||
}; | ||
parsePattern(pattern, self); | ||
@@ -319,6 +325,46 @@ | ||
Cron.prototype.msToNext = function (date) { | ||
return (this.next(date) - new Date().getTime()); | ||
Cron.prototype.msToNext = function (prev) { | ||
return (this.next(prev) - new Date().getTime()); | ||
} | ||
Cron.prototype.schedule = function (opts, f, recurse) { | ||
var self = this, | ||
waitMs; | ||
// Make opts optional | ||
if ( f === undefined ) { | ||
f = opts; | ||
opts = {}; | ||
} | ||
opts.previous = opts.startAt || new Date(); | ||
opts.stopAt = opts.stopAt || this.schedulerDefaults.stopAt; | ||
opts.maxRuns = opts.maxRuns || this.schedulerDefaults.maxRuns; | ||
opts.kill = opts.kill || this.schedulerDefaults.kill; | ||
// One-timer | ||
opts.startAt = undefined; | ||
waitMs = this.msToNext(opts.previous); | ||
// Check for stop conditions | ||
if ( opts.maxRuns <= 0 ) return; | ||
if ( opts.stopAt !== Infinity && opts.previous.getTime() + waitMs/1000 > opts.stopAt.getTime() ) return; | ||
if ( opts.kill ) return; | ||
// All ok, go go! | ||
setTimeout( function() { opts.maxRuns--; f(); self.schedule(opts, f, true); }, waitMs ); | ||
// First run? Return killer | ||
if ( !recurse ) { | ||
return { | ||
kill: function() { | ||
opts.kill = true; | ||
} | ||
} | ||
} | ||
} | ||
// Expose to | ||
@@ -338,2 +384,33 @@ // ... Node | ||
this.cron = function (pattern) { return new Cron(pattern); }; | ||
} | ||
// Debug shit - To be removed | ||
if(false) { | ||
var scheduler = new Cron('* * * * * *'); | ||
// Start an infinite job that runs every second | ||
var job0 = scheduler.schedule(function() { | ||
console.log('I\'m invincible!!'); | ||
}); | ||
// Start a job that runs from 5 seconds from now, to 10 seconds from now | ||
var job1 = scheduler.schedule({ | ||
startAt: new Date(new Date().getTime()+5000), | ||
stopAt: new Date(new Date().getTime()+10000), | ||
},function() { | ||
console.log('5-10s'); | ||
}); | ||
// Start a job that runs from 7 seconds from now, to 15 seconds from now | ||
var job2 = scheduler.schedule({ | ||
startAt: new Date(new Date().getTime()+7000), | ||
stopAt: new Date(new Date().getTime()+15000), | ||
},function() { | ||
console.log('7-15s...?'); | ||
}); | ||
// Kill the infinite job after 20 seconds, HEH! | ||
setTimeout( function () { job0.kill(); }, 20000); | ||
} |
{ | ||
"name": "croner", | ||
"version": "1.0.1", | ||
"description": "Isomorphic JavaScript cron parser.", | ||
"version": "1.0.2", | ||
"description": "Isomorphic JavaScript cron parser and scheduler.", | ||
"author": "Hexagon <github.com/hexagon>", | ||
@@ -6,0 +6,0 @@ "contributors": [{ |
@@ -6,3 +6,3 @@ | ||
Pure JavaScript Isomorphic cron parser without dependencies. | ||
Pure JavaScript Isomorphic cron parser and scheduler without dependencies. | ||
@@ -9,0 +9,0 @@ |
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
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
19590
449