Skip to content

Commit

Permalink
Changed onTick and onComplete to be asynchronous.
Browse files Browse the repository at this point in the history
Change-log:
[NEW] Async keyword for onTick function
[NEW] Async keyword for onComplete function
[UPDATE] README.md
[UPDATE] dependency versions
  • Loading branch information
nemanjapetrovic committed Nov 3, 2018
1 parent 20e3628 commit f696191
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 23 deletions.
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ module.exports = class Hello extends BaseCron {
}
//override
onTick() {
async onTick() {
console.log('Hello from cron job.');
}
//override
onComplete() {
async onComplete() {
console.log('Finished.');
}
Expand All @@ -51,11 +51,20 @@ hello.start();

- start - starts the cron job
- stop - stops the cron job
- onTick - function which will be trigger on each tick
- onComplete - function which will be triggerd when the job is stopped
- *async* onTick - function which will be trigger on each tick
- *async* onComplete - function which will be triggerd when the job is stopped
- isCronRunning - checks if the cron job is running
- getCronInstance - will return you an instance of *CronJob* from wrapped *[cron](https://www.npmjs.com/package/cron)* npm package

# Cron Ranges

- Seconds: 0-59
- Minutes: 0-59
- Hours: 0-23
- Day of Month: 1-31
- Months: 0-11 (Jan-Dec)
- Day of Week: 0-6 (Sun-Sat)

# [Contribution](https://github.com/nemanjapetrovic/cron-es6/blob/master/CONTRIBUTING.md)

Feel free to contribute by forking this repository, making some changes, and submitting pull requests. For any questions or advice place an issue on this repository.
Expand Down
20 changes: 15 additions & 5 deletions examples/hello.example.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,34 @@
*/
const BaseCron = require('../index');

async function fooAsync() {
console.log('hello from async func');
}

let counter = 0;
class Hello extends BaseCron {

constructor() {
super('Hello', '* * * * * *');
}

onTick() {
console.log('***Hello from cron job.');
async onTick() {
console.log('Next tick:');

try {
await fooAsync();
} catch (err) {
console.log(err);
}

counter++;
if (counter === 3) {
this.stop();
}
}

onComplete() {
console.log(`Complete successful: ${counter}`);
async onComplete() {
console.log(`------------\nComplete successful: ${counter}`);
console.log(`is cron running: ${hello.isCronRunning()}`);
}

Expand All @@ -29,4 +39,4 @@ class Hello extends BaseCron {
let hello = new Hello();
console.log('starting cron');
hello.start();
console.log(`is cron running: ${hello.isCronRunning()}`);
console.log(`is cron running: ${hello.isCronRunning()}\n------------`);
10 changes: 6 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const cron = require('cron').CronJob;
*
* @class BaseCron
*/
module.exports = class BaseCron {
class BaseCron {

/**
* Creates an instance of BaseCron.
Expand Down Expand Up @@ -51,7 +51,7 @@ module.exports = class BaseCron {
* @public
* @memberof BaseCron
*/
onTick() {
async onTick() {
throw new Error('ERR_METHOD_NOT_IMPLEMENTED');
}

Expand All @@ -61,7 +61,7 @@ module.exports = class BaseCron {
* @public
* @memberof BaseCron
*/
onComplete() {
async onComplete() {
throw new Error('ERR_METHOD_NOT_IMPLEMENTED');
}

Expand All @@ -85,4 +85,6 @@ module.exports = class BaseCron {
return this._cronJob.running;
}

};
};

module.exports = BaseCron;
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cron-es6",
"version": "1.0.1",
"version": "1.0.2",
"description": "ES6 (ECMAScript 2015) node cron wrapper around https://www.npmjs.com/package/cron npm package.",
"main": "index.js",
"author": "Nemanja Petrovic <nempet@protonmail.com>",
Expand All @@ -19,7 +19,7 @@
"node-cron-es6"
],
"dependencies": {
"cron": "^1.4.1"
"cron": "^1.5.0"
},
"bugs": {
"url": "https://github.com/nemanjapetrovic/cron-es6/issues",
Expand Down

0 comments on commit f696191

Please sign in to comment.