Skip to content

Commit

Permalink
Dependencies update. Adding EditorConfig file and code formatting.
Browse files Browse the repository at this point in the history
  • Loading branch information
nemanjapetrovic committed Jan 23, 2021
1 parent 42a4f5a commit b4d4455
Show file tree
Hide file tree
Showing 6 changed files with 2,123 additions and 115 deletions.
16 changes: 16 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
root = true

[*]
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
indent_style = space
indent_size = 2

[*.{js,txt,md,css,html,php,py,json,yml,sass,pug}]
indent_style = space
indent_size = 2

[*.{diff,md}]
trim_trailing_whitespace = false
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ hello.start();
- Months: 0-11 (Jan-Dec)
- Day of Week: 0-6 (Sun-Sat)

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

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.
Feel free to contribute by forking this repository, making changes, and submitting pull requests. For any questions or advice place an issue on this repository.

# License

Expand Down
46 changes: 22 additions & 24 deletions examples/hello.example.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,38 @@
*/
const BaseCron = require('../index');

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

let counter = 0;
class Hello extends BaseCron {
constructor () {
super('Hello', '* * * * * *');
}

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

async onTick() {
console.log('Next tick:');
async onTick () {
console.log('Next tick:');

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

counter++;
if (counter === 3) {
this.stop();
}
try {
await fooAsync();
} catch (err) {
console.log(err);
}

async onComplete() {
console.log(`------------\nComplete successful: ${counter}`);
console.log(`is cron running: ${hello.isCronRunning()}`);
counter++;
if (counter === 3) {
this.stop();
}
}

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

let hello = new Hello();
const hello = new Hello();
console.log('starting cron');
hello.start();
console.log(`is cron running: ${hello.isCronRunning()}\n------------`);
console.log(`is cron running: ${hello.isCronRunning()}\n------------`);
146 changes: 72 additions & 74 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,90 +1,88 @@
const cron = require('cron').CronJob;
const Cron = require('cron').CronJob;

/**
* BaseCron class for cron jobs.
*
* @class BaseCron
*/
class BaseCron {

/**
* Creates an instance of BaseCron.
*
* @param {string} cronName
* @param {string} cronTime
* @public
* @memberof BaseCron
*/
constructor(cronName, cronTime) {
if (!cronName) {
throw new Error('ERR_INVALID_ARG_VALUE');
}

this._cronName = cronName;
this._cronTime = cronTime;
this._cronJob = new cron(this._cronTime, this.onTick.bind(this), this.onComplete.bind(this));
/**
* Creates an instance of BaseCron.
*
* @param {string} cronName
* @param {string} cronTime
* @public
* @memberof BaseCron
*/
constructor (cronName, cronTime) {
if (!cronName) {
throw new Error('ERR_INVALID_ARG_VALUE');
}

/**
* Starts the cron job.
*
* @public
* @memberof BaseCron
*/
start() {
this._cronJob.start();
}
this._cronName = cronName;
this._cronTime = cronTime;
this._cronJob = new Cron(this._cronTime, this.onTick.bind(this), this.onComplete.bind(this));
}

/**
* Stops the cron job.
*
* @public
* @memberof BaseCron
*/
stop() {
this._cronJob.stop();
}
/**
* Starts the cron job.
*
* @public
* @memberof BaseCron
*/
start () {
this._cronJob.start();
}

/**
* Function onTick for the current cron job.
*
* @public
* @memberof BaseCron
*/
async onTick() {
throw new Error('ERR_METHOD_NOT_IMPLEMENTED');
}
/**
* Stops the cron job.
*
* @public
* @memberof BaseCron
*/
stop () {
this._cronJob.stop();
}

/**
* Function onComplete for the current cron job.
*
* @public
* @memberof BaseCron
*/
async onComplete() {
throw new Error('ERR_METHOD_NOT_IMPLEMENTED');
}
/**
* Function onTick for the current cron job.
*
* @public
* @memberof BaseCron
*/
async onTick () {
throw new Error('ERR_METHOD_NOT_IMPLEMENTED');
}

/**
* Returns an instance of cron npm module.
*
* @public
* @memberof BaseCron
*/
getCronInstance() {
return this._cronJob;
}
/**
* Function onComplete for the current cron job.
*
* @public
* @memberof BaseCron
*/
async onComplete () {
throw new Error('ERR_METHOD_NOT_IMPLEMENTED');
}

/**
* Check if current cron job is running.
*
* @public
* @memberof BaseCron
*/
isCronRunning() {
return this._cronJob.running;
}
/**
* Returns an instance of cron npm module.
*
* @public
* @memberof BaseCron
*/
getCronInstance () {
return this._cronJob;
}

};
/**
* Check if current cron job is running.
*
* @public
* @memberof BaseCron
*/
isCronRunning () {
return this._cronJob.running;
}
}

module.exports = BaseCron;
module.exports = BaseCron;
Loading

0 comments on commit b4d4455

Please sign in to comment.