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.2.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.3.0
Choose a head ref
  • 13 commits
  • 4 files changed
  • 7 contributors

Commits on Dec 19, 2016

  1. Add example for job.running in README.md

    The variable job.running can be used to get status of job, but have to
    use a slightly modified syntax
    sravan committed Dec 19, 2016
    Copy the full SHA
    20fe53e View commit details

Commits on Dec 31, 2016

  1. Merge pull request #267 from sravan-s/job-running-documentation

    Add example for job.running in README.md
    ncb000gt authored Dec 31, 2016
    Copy the full SHA
    0315441 View commit details

Commits on Jan 26, 2017

  1. README.md typo fix

    kaworu authored Jan 26, 2017
    Copy the full SHA
    1b1c7bb View commit details

Commits on Jan 29, 2017

  1. Merge pull request #274 from kAworu/patch-1

    README.md typo fix
    ncb000gt authored Jan 29, 2017
    Copy the full SHA
    8272fa2 View commit details

Commits on Mar 20, 2017

  1. Add utcOffset support

    matsukaz committed Mar 20, 2017
    Copy the full SHA
    4cb50d3 View commit details

Commits on Apr 22, 2017

  1. Fix the typo

    vladikoff committed Apr 22, 2017
    Copy the full SHA
    53cd4f2 View commit details

Commits on Aug 4, 2017

  1. Copy the full SHA
    49fc43c View commit details

Commits on Sep 12, 2017

  1. Added timezone reference

    Hello guys.
    
    I'd like to propose to add where we can find a time zone reference. To find this I had to check in closed issues, so to make easier, just put this line at Timezone param description!
    
    Cheers
    raulfdm authored Sep 12, 2017
    Copy the full SHA
    ceff34b View commit details
  2. Merge pull request #304 from raulfdm/patch-1

    Added timezone reference
    ncb000gt authored Sep 12, 2017
    Copy the full SHA
    a6f839f View commit details
  3. Merge pull request #300 from saltire/job-args

    Match exports.job() signature to CronJob constructor
    ncb000gt authored Sep 12, 2017
    Copy the full SHA
    357c189 View commit details
  4. Merge pull request #285 from vladikoff/fix-typo

    Fix the typo
    ncb000gt authored Sep 12, 2017
    Copy the full SHA
    61b25c5 View commit details
  5. Merge pull request #184 from matsukaz/feature/support_utc_offset

    Add utcOffset support
    ncb000gt authored Sep 12, 2017
    Copy the full SHA
    6806fb1 View commit details
  6. housekeeping and version.

    Signed-off-by: Nick Campbell <nicholas.j.campbell@gmail.com>
    ncb000gt committed Sep 12, 2017
    Copy the full SHA
    c4f7415 View commit details
Showing with 114 additions and 13 deletions.
  1. +37 −3 README.md
  2. +11 −5 lib/cron.js
  3. +6 −5 package.json
  4. +60 −0 tests/test-cron.js
40 changes: 37 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@ Originally this project was a NodeJS fork of [James Padolsey's][jamespadolsey] [

After [Craig Condon][crcn] made some updates and changes to the code base this has evolved to something that has a bit of both. The cron syntax parsing is mostly James' while using timeout instead of interval is Craig's.

Additionally, this library goes beyond the basic cron syntax and allows you to supply a Date object. This will be used as the trigger for your callback. Cron syntax is still an acceptable CronTime format. Although the Cron patterns suported here extend on the standard Unix format to support seconds digits, leaving it off will default to 0 and match the Unix behavior.
Additionally, this library goes beyond the basic cron syntax and allows you to supply a Date object. This will be used as the trigger for your callback. Cron syntax is still an acceptable CronTime format. Although the Cron patterns supported here extend on the standard Unix format to support seconds digits, leaving it off will default to 0 and match the Unix behavior.

If You Are Submitting Bugs/Issues
=============
@@ -136,6 +136,40 @@ try {
```


How to check if a job is running
==========

```javascript
var cron = require('cron');

var job1 = new cron.CronJob({
cronTime: '* * * * *',
onTick: function() {
console.log('job 1 ticked');
},
start: false,
timeZone: 'America/Los_Angeles'
});

var job2 = new cron.CronJob({
cronTime: '* * * * *',
onTick: function() {
console.log('job 2 ticked');
},
start: false,
timeZone: 'America/Los_Angeles'
});

console.log('job1 status', job1.running); // job1 status undefined
console.log('job2 status', job2.running); // job2 status undefined

job1.start(); // job 1 started

console.log('job1 status', job1.running); // job1 status true
console.log('job2 status', job2.running); // job2 status undefined
```


Install
==========

@@ -155,9 +189,9 @@ Parameter Based
* `onTick` - [REQUIRED] - The function to fire at the specified time.
* `onComplete` - [OPTIONAL] - A function that will fire when the job is complete, when it is stopped.
* `start` - [OPTIONAL] - Specifies whether to start the job just before exiting the constructor. By default this is set to false. If left at default you will need to call `job.start()` in order to start the job (assuming `job` is the variable you set the cronjob to). This does not immediately fire your `onTick` function, it just gives you more control over the behavior of your jobs.
* `timeZone` - [OPTIONAL] - Specify the timezone for the execution. This will modify the actual time relative to your timezone. If the timezone is invalid, an error is thrown.
* `timeZone` - [OPTIONAL] - Specify the timezone for the execution. This will modify the actual time relative to your timezone. If the timezone is invalid, an error is thrown. You can check all timezones available at [Moment Timezone Website](http://momentjs.com/timezone/).
* `context` - [OPTIONAL] - The context within which to execute the onTick method. This defaults to the cronjob itself allowing you to call `this.stop()`. However, if you change this you'll have access to the functions and values within your context object.
* `runOnInit` - [OPTIONAL] - This will immediately fire your `onTick` function as soon as the requisit initialization has happened. This option is set to `false` by default for backwards compatability.
* `runOnInit` - [OPTIONAL] - This will immediately fire your `onTick` function as soon as the requisit initialization has happened. This option is set to `false` by default for backwards compatibility.
* `start` - Runs your job.
* `stop` - Stops your job.

16 changes: 11 additions & 5 deletions lib/cron.js
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@ var exports = {},
timeUnits = ['second', 'minute', 'hour', 'dayOfMonth', 'month', 'dayOfWeek'],
spawn = child_process && child_process.spawn;

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

if (zone) {
@@ -23,6 +23,8 @@ function CronTime(source, zone) {
this.zone = zone;
}

if (utcOffset) this.utcOffset = utcOffset;

var that = this;
timeUnits.map(function(timeUnit){
that[timeUnit] = {};
@@ -112,6 +114,9 @@ CronTime.prototype = {
if (this.zone)
date = date.tz(this.zone);

if (this.utcOffset)
date = date.utcOffset(this.utcOffset);

if (this.realDate)
return date;

@@ -370,7 +375,7 @@ function command2function(cmd) {
return cmd
}

function CronJob(cronTime, onTick, onComplete, startNow, timeZone, context, runOnInit) {
function CronJob(cronTime, onTick, onComplete, startNow, timeZone, context, runOnInit, utcOffset) {
var _cronTime = cronTime;
if (typeof cronTime != 'string' && arguments.length == 1) {
//crontime is an object...
@@ -381,12 +386,13 @@ function CronJob(cronTime, onTick, onComplete, startNow, timeZone, context, runO
timeZone = cronTime.timeZone;
runOnInit = cronTime.runOnInit;
_cronTime = cronTime.cronTime;
utcOffset = cronTime.utcOffset;
}

this.context = (context || this);
this._callbacks = [];
this.onComplete = command2function(onComplete);
this.cronTime = new CronTime(_cronTime, timeZone);
this.cronTime = new CronTime(_cronTime, timeZone, utcOffset);

addCallback.call(this, command2function(onTick));

@@ -516,8 +522,8 @@ CronJob.prototype.stop = function() {
if (typeof this.onComplete == 'function') this.onComplete();
}

exports.job = function(cronTime, onTick, onComplete) {
return new CronJob(cronTime, onTick, onComplete);
exports.job = function(cronTime, onTick, onComplete, startNow, timeZone, context, runOnInit) {
return new CronJob(cronTime, onTick, onComplete, startNow, timeZone, context, runOnInit);
}

exports.time = function(cronTime, timeZone) {
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "cron",
"description": "Cron jobs for your node",
"version": "1.2.1",
"version": "1.3.0",
"author": "Nick Campbell <nicholas.j.campbell@gmail.com> (http://github.com/ncb000gt)",
"bugs" : {
"url" : "http://github.com/ncb000gt/node-cron/issues"
"bugs": {
"url": "http://github.com/kelektiv/node-cron/issues"
},
"repository": {
"type": "git",
"url": "http://github.com/ncb000gt/node-cron.git"
"url": "http://github.com/kelektiv/node-cron.git"
},
"main": "lib/cron",
"scripts": {
@@ -35,6 +35,7 @@
"Vadim Baryshev <vadimbaryshev@gmail.com> (https://github.com/baryshev)",
"Leandro Ferrari <lfthomaz@gmail.com> (https://github.com/lfthomaz)",
"Gregg Zigler <greggzigler@gmail.com> (https://github.com/greggzigler)",
"Jordan Abderrachid <jabderrachid@gmail.com> (https://github.com/jordanabderrachid)"
"Jordan Abderrachid <jabderrachid@gmail.com> (https://github.com/jordanabderrachid)",
"Masakazu Matsushita <matsukaz@gmail.com> (matsukaz)"
]
}
60 changes: 60 additions & 0 deletions tests/test-cron.js
Original file line number Diff line number Diff line change
@@ -559,4 +559,64 @@ describe('cron', function() {
});

it('should run every second monday');

describe('with utcOffset', function() {
it('should run a job using cron syntax with number format utcOffset', function () {
var clock = sinon.useFakeTimers();
var c = 0;

var moment = require("moment-timezone");

// Current time
var t = moment();

// UTC Offset decreased by an hour
var utcOffset = t.utcOffset() - 60;

var job = new cron.CronJob(t.seconds() + ' ' + t.minutes() + ' ' + t.hours() + ' * * *', function(){
c++;
}, null, true, null, null, null, utcOffset);

// tick 1 sec before an hour
clock.tick(1000 * 60 * 60 - 1);
expect(c).to.eql(0);

clock.tick(1);
clock.restore();
job.stop();
expect(c).to.eql(1);
});

it('should run a job using cron syntax with string format utcOffset', function () {
var clock = sinon.useFakeTimers();
var c = 0;

var moment = require("moment-timezone");

// Current time
var t = moment();

// UTC Offset decreased by an hour (string format '(+/-)HH:mm')
var utcOffset = t.utcOffset() - 60;
var utcOffsetString = (0 < utcOffset ? '+' : '-');
utcOffsetString += ('0' + Math.floor(Math.abs(utcOffset) / 60)).slice(-2);
utcOffsetString += ':';
utcOffsetString += ('0' + (utcOffset % 60)).slice(-2);

var job = new cron.CronJob(t.seconds() + ' ' + t.minutes() + ' ' + t.hours() + ' * * *', function(){
c++;
}, null, true, null, null, null, utcOffsetString);

// tick 1 sec before an hour
clock.tick(1000 * 60 * 60 - 1);
expect(c).to.eql(0);

// tick 1 sec
clock.tick(1);
clock.restore();
job.stop();
expect(c).to.eql(1);
});

});
});