Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: kelektiv/node-cron
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v1.1.1
Choose a base ref
...
head repository: kelektiv/node-cron
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v1.2.0
Choose a head ref
  • 12 commits
  • 6 files changed
  • 4 contributors

Commits on Sep 16, 2015

  1. Copy the full SHA
    74d8f63 View commit details
  2. fixed 1 second bug

    nilpotence committed Sep 16, 2015
    Copy the full SHA
    d6eff7d View commit details

Commits on Oct 28, 2015

  1. umd wrapper

    iofjuupasli committed Oct 28, 2015
    Copy the full SHA
    f146bd5 View commit details

Commits on Oct 7, 2016

  1. styles

    Signed-off-by: Nick Campbell <nicholas.j.campbell@gmail.com>
    ncb000gt committed Oct 7, 2016
    Copy the full SHA
    5de4d24 View commit details
  2. Don't throw when runnables are out of bounds, warn on parse verificat…

    …ion with specifics.
    
    Signed-off-by: Nick Campbell <nicholas.j.campbell@gmail.com>
    ncb000gt committed Oct 7, 2016
    Copy the full SHA
    1005bac View commit details
  3. Add another test to verify GH-209.

    Signed-off-by: Nick Campbell <nicholas.j.campbell@gmail.com>
    ncb000gt committed Oct 7, 2016
    Copy the full SHA
    8ddfe14 View commit details
  4. Merge branch 'master' of https://github.com/iofjuupasli/node-cron int…

    …o iofjuupasli-master
    ncb000gt committed Oct 7, 2016
    Copy the full SHA
    8b4676b View commit details
  5. Merge branch 'master' of https://github.com/partnering/node-cron into…

    … partnering-master
    
    Signed-off-by: Nick Campbell <nicholas.j.campbell@gmail.com>
    
    Conflicts:
    	lib/cron.js
    ncb000gt committed Oct 7, 2016
    Copy the full SHA
    6eb9c3d View commit details
  6. Copy the full SHA
    ab7047f View commit details

Commits on Nov 5, 2016

  1. Reschedule onTick if time was ajusted back GH-249

    Alexey Komarov committed Nov 5, 2016
    Copy the full SHA
    3a4e13e View commit details

Commits on Dec 18, 2016

  1. Merge pull request #259 from alexey-komarov/time-ajusted-back

    Reschedule onTick if time was ajusted back GH-249
    ncb000gt authored Dec 18, 2016
    Copy the full SHA
    ef09e22 View commit details
  2. Version bump.

    Signed-off-by: Nick Campbell <nicholas.j.campbell@gmail.com>
    ncb000gt committed Dec 18, 2016
    Copy the full SHA
    483d12e View commit details
Showing with 197 additions and 91 deletions.
  1. +43 −0 .eslintrc.json
  2. +1 −0 README.md
  3. +117 −77 lib/cron.js
  4. +1 −1 package.json
  5. +35 −1 tests/test-cron.js
  6. +0 −12 tests/test-crontime.js
43 changes: 43 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"extends": "eslint:recommended",
"parserOptions": {
"ecmaVersion": 6,
"ecmaFeatures": {
"blockBindings": true,
"jsx": true,
"experimentalObjectRestSpread": true
},
"sourceType": "module"
},
"globals": {
"after": true,
"afterEach": true,
"before": true,
"beforeEach": true,
"describe": true,
"expect": true,
"it": true,
"ga": true
},
"env": {
"browser": true,
"node": true
},
"plugins": [
"react"
],
"rules": {
"no-console": 0,
"new-cap": 0,
"strict": 0,
"no-underscore-dangle": 0,
"no-use-before-define": 0,
"no-unused-vars": [2, {"args": "after-used", "argsIgnorePattern": "^_"}],
"eol-last": 0,
"quotes": [2, "single"],
"jsx-quotes": 1,
"react/jsx-no-undef": 1,
"react/jsx-uses-react": 1,
"react/jsx-uses-vars": 1
}
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -42,6 +42,7 @@ new CronJob('* * * * * *', function() {
}, null, true, 'America/Los_Angeles');
```

Note - You need to explictly start a job in order to make it run. This gives a little more control over running your jobs.

Available Cron patterns:
==========
194 changes: 117 additions & 77 deletions lib/cron.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
var exports,
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define(['moment-timezone'], factory);
} else if (typeof exports === 'object') {
module.exports = factory(require('moment-timezone'), require('child_process'));
} else {
root.Cron = factory(root.moment);
}
}(this, function (moment, child_process) {

var exports = {},
timeUnits = ['second', 'minute', 'hour', 'dayOfMonth', 'month', 'dayOfWeek'],
spawn = require('child_process').spawn,
moment = require('moment-timezone');
spawn = child_process && child_process.spawn;

function CronTime(source, zone) {
this.source = source;

if (zone) {
if (moment.tz.names().indexOf(zone) === -1) {
throw new Error('Invalid timezone.');
@@ -24,8 +33,7 @@ function CronTime(source, zone) {
this.realDate = true;
} else {
this._parse();
if (!this._verifyParse())
throw Error("Could not verify a valid date. Please verify your parameters.");
this._verifyParse();
}
}

@@ -90,18 +98,15 @@ CronTime.prototype = {
}

if (!ok) {
console.error("Month '" + m + "' is limited to '" + con + "' days.");
return false;
console.warn('Month \'' + m + '\' is limited to \'' + con + '\' days.');
}
}

return true;
},

/**
* calculates the next send time
*/
sendAt: function() {
sendAt: function(i) {
var date = this.realDate ? this.source : moment();
// Set the timezone if given (http://momentjs.com/timezone/docs/#/using-timezones/parsing-in-zone/)
if (this.zone)
@@ -121,9 +126,21 @@ CronTime.prototype = {
date = date.add(1, 's');
}

date = this._getNextDateFrom(date);

return date;
//If the i argument is not given, return the next send time
if(isNaN(i) || i < 0){
date = this._getNextDateFrom(date);
return date;
}
//Else return the next i send times
else{
var dates = [];
for(;i>0;i--){
date = this._getNextDateFrom(date);
dates.push(moment(date));
if(i>1) date.add(1,'s');
}
return dates;
}
},

/**
@@ -156,15 +173,15 @@ CronTime.prototype = {
_getNextDateFrom: function(start) {
var date = moment(start);
if (date.toString() == 'Invalid date') {
console.log("ERROR: You specified an invalid date.");
console.log('ERROR: You specified an invalid date.');
return date;
}
if (this.realDate && start < new Date())
console.log("WARNING: Date in past. Will never be fired.");
console.log('WARNING: Date in past. Will never be fired.');
if (this.realDate) return date;

//sanity check
while (1) {
while (true) {
var diff = date - start,
origDate = new Date(date);

@@ -236,8 +253,7 @@ CronTime.prototype = {
* get next date that is a valid DST date
*/
_findDST: function(date) {
var newDate = moment(date),
addSeconds = 1;
var newDate = moment(date);
while (newDate <= date)
newDate.add(1, 's');

@@ -294,55 +310,56 @@ CronTime.prototype = {
},

_parseField: function(field, type, constraints) {
var rangePattern = /^(\d+)(?:-(\d+))?(?:\/(\d+))?$/g,
typeObj = this[type],
diff, pointer,
low = constraints[0],
high = constraints[1];

// * is a shortcut to [lower-upper] range
field = field.replace(/\*/g, low + '-' + high);

//commas separate information, so split based on those
var allRanges = field.split(',');

for (var i = 0; i < allRanges.length; i++) {
if (allRanges[i].match(rangePattern)) {
allRanges[i].replace(rangePattern, function($0, lower, upper, step) {
step = parseInt(step) || 1;
// Positive integer higher than constraints[0]
lower = Math.min(Math.max(low, ~~Math.abs(lower)), high);

// Positive integer lower than constraints[1]
upper = upper ? Math.min(high, ~~Math.abs(upper)) : lower;

// Count from the lower barrier to the upper
pointer = lower;

do {
typeObj[pointer] = true
pointer += step;
} while (pointer <= upper);
});
} else {
throw new Error('Field (' + field + ') cannot be parsed');
}
}
}
var rangePattern = /^(\d+)(?:-(\d+))?(?:\/(\d+))?$/g,
typeObj = this[type],
pointer,
low = constraints[0],
high = constraints[1];

// * is a shortcut to [lower-upper] range
field = field.replace(/\*/g, low + '-' + high);

//commas separate information, so split based on those
var allRanges = field.split(',');

for (var i = 0; i < allRanges.length; i++) {
if (allRanges[i].match(rangePattern)) {
allRanges[i].replace(rangePattern, function($0, lower, upper, step) {
step = parseInt(step) || 1;
// Positive integer higher than constraints[0]
lower = Math.min(Math.max(low, ~~Math.abs(lower)), high);

// Positive integer lower than constraints[1]
upper = upper ? Math.min(high, ~~Math.abs(upper)) : lower;

// Count from the lower barrier to the upper
pointer = lower;

do {
typeObj[pointer] = true
pointer += step;
} while (pointer <= upper);
});
} else {
throw new Error('Field (' + field + ') cannot be parsed');
}
}
}
};

function command2function(cmd) {
var command, args;
switch (typeof cmd) {
case 'string':
var args = cmd.split(' ');
var command = args.shift();
args = cmd.split(' ');
command = args.shift();

cmd = spawn.bind(undefined, command, args);
break;
case 'object':
var command = cmd && cmd.command;
command = cmd && cmd.command;
if (command) {
var args = cmd.args;
args = cmd.args;
var options = cmd.options;

cmd = spawn.bind(undefined, command, args, options);
@@ -355,7 +372,7 @@ function command2function(cmd) {

function CronJob(cronTime, onTick, onComplete, startNow, timeZone, context, runOnInit) {
var _cronTime = cronTime;
if (typeof cronTime != "string" && arguments.length == 1) {
if (typeof cronTime != 'string' && arguments.length == 1) {
//crontime is an object...
onTick = cronTime.onTick;
onComplete = cronTime.onComplete;
@@ -400,20 +417,41 @@ var fireOnTick = function() {
}
CronJob.prototype.fireOnTick = fireOnTick;

CronJob.prototype.nextDates = function(i) {
return this.cronTime.sendAt(i);
}

var start = function() {
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;
var startTime;

if (this.cronTime.realDate) this.runOnce = true;

function _setTimeout(timeout) {
startTime = Date.now();
self._timeout = setTimeout(callbackWrapper, timeout);
}

// 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() {
var diff = startTime + timeout - Date.now();

if (diff > 0) {
var newTimeout = self.cronTime.getTimeout();

if (newTimeout > diff) {
newTimeout = diff;
}

remaining += newTimeout;
}

// 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
@@ -430,7 +468,7 @@ var start = function() {
remaining = 0;
}

self._timeout = setTimeout(callbackWrapper, timeout);
_setTimeout(timeout);
} else {

// We have arrived at the correct point in time.
@@ -456,7 +494,7 @@ var start = function() {
timeout = MAXDELAY;
}

this._timeout = setTimeout(callbackWrapper, timeout);
_setTimeout(timeout);
} else {
this.stop();
}
@@ -478,23 +516,25 @@ CronJob.prototype.stop = function() {
if (typeof this.onComplete == 'function') this.onComplete();
}

if (exports) {
exports.job = function(cronTime, onTick, onComplete) {
return new CronJob(cronTime, onTick, onComplete);
}

exports.time = function(cronTime, timeZone) {
return new CronTime(cronTime, timeZone);
}
exports.job = function(cronTime, onTick, onComplete) {
return new CronJob(cronTime, onTick, onComplete);
}

exports.sendAt = function(cronTime) {
return exports.time(cronTime).sendAt();
}
exports.time = function(cronTime, timeZone) {
return new CronTime(cronTime, timeZone);
}

exports.timeout = function(cronTime) {
return exports.time(cronTime).getTimeout();
}
exports.sendAt = function(cronTime) {
return exports.time(cronTime).sendAt();
}

exports.CronJob = CronJob;
exports.CronTime = CronTime;
exports.timeout = function(cronTime) {
return exports.time(cronTime).getTimeout();
}

exports.CronJob = CronJob;
exports.CronTime = CronTime;

return exports;

}));
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "cron",
"description": "Cron jobs for your node",
"version": "1.1.1",
"version": "1.2.0",
"author": "Nick Campbell <nicholas.j.campbell@gmail.com> (http://github.com/ncb000gt)",
"bugs" : {
"url" : "http://github.com/ncb000gt/node-cron/issues"
Loading