8000 GH-146: add option to immediately run the onTick. Also, disambiguate … · github4f/node-cron@344ce9b · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 344ce9b

Browse files
committed
kelektivGH-146: add option to immediately run the onTick. Also, disambiguate the readme WRT start/onTick.
Signed-off-by: Nick Campbell <nicholas.j.campbell@gmail.com>
1 parent ca5f6b5 commit 344ce9b

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
language: node_js
22
node_js:
3+
- "4.1"
4+
- "4.0"
35
- "0.12"
46
- "0.10"
57
- "iojs"

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,14 @@ Parameter Based
146146

147147
`CronJob`
148148

149-
* `constructor(cronTime, onTick, onComplete, start, timezone, context)` - Of note, the first parameter here can be a JSON object that has the below names and associated types (see examples above).
149+
* `constructor(cronTime, onTick, onComplete, start, timezone, context, onInit)` - Of note, the first parameter here can be a JSON object that has the below names and associated types (see examples above).
150150
* `cronTime` - [REQUIRED] - The time to fire off your job. This can be in the form of cron syntax or a JS [Date](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date) object.
151151
* `onTick` - [REQUIRED] - The function to fire at the specified time.
152152
* `onComplete` - [OPTIONAL] - A function that will fire when the job is complete, when it is stopped.
153-
* `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).
153+
* `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.
154154
* `timeZone` - [OPTIONAL] - Specify the timezone for the execution. This will modify the actual time relative to your timezone.
155155
* `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.
156+
* `onInit` - [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.
156157
* `start` - Runs your job.
157158
* `stop` - Stops your job.
158159

lib/cron.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -346,24 +346,27 @@ function command2function(cmd) {
346346
return cmd
347347
}
348348

349-
function CronJob(cronTime, onTick, onComplete, startNow, timeZone, context) {
349+
function CronJob(cronTime, onTick, onComplete, startNow, timeZone, context, runOnInit) {
350+
var _cronTime = cronTime;
350351
if (typeof cronTime != "string" && arguments.length == 1) {
351352
//crontime is an object...
352353
onTick = cronTime.onTick;
353354
onComplete = cronTime.onComplete;
354355
context = cronTime.context;
355-
startNow = cronTime.start || cronTime.startNow;
356+
startNow = cronTime.start || cronTime.startNow || cronTime.startJob;
356357
timeZone = cronTime.timeZone;
357-
cronTime = cronTime.cronTime;
358+
runOnInit = cronTime.onInit;
359+
_cronTime = cronTime.cronTime;
358360
}
359361

360362
this.context = (context || this);
361363
this._callbacks = [];
362364
this.onComplete = command2function(onComplete);
363-
this.cronTime = new CronTime(cronTime, timeZone);
365+
this.cronTime = new CronTime(_cronTime, timeZone);
364366

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

369+
if (runOnInit) fireOnTick.call(this);
367370
if (startNow) start.call(this);
368371

369372
return this;
@@ -384,6 +387,12 @@ CronJob.prototype.nextDate = function() {
384387
return this.cronTime.sendAt();
385388
}
386389

390+
var fireOnTick = function() {
391+
for (var i = (this._callbacks.length - 1); i >= 0; i--)
392+
this._callbacks[i].call(this.context, this.onComplete);
393+
}
394+
CronJob.prototype.fireOnTick = fireOnTick;
395+
387396
var start = function() {
388397
if (this.running) return;
389398

@@ -423,8 +432,9 @@ var start = function() {
423432
//start before calling back so the callbacks have the ability to stop the cron job
424433
if (!(self.runOnce)) self.start();
425434

426-
for (var i = (self._callbacks.length - 1); i >= 0; i--)
427-
self._callbacks[i].call(self.context, self.onComplete);
435+
self.fireOnTick();
436+
//for (var i = (self._callbacks.length - 1); i >= 0; i--)
437+
//self._callbacks[i].call(self.context, self.onComplete);
428438
}
429439
}
430440

0 commit comments

Comments
 (0)
0