Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

getStatus() method and destroy() methods updated #296

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
24 changes: 16 additions & 8 deletions src/scheduled-task.js
Expand Up @@ -8,13 +8,13 @@ const uuid = require('uuid');
class ScheduledTask extends EventEmitter {
constructor(cronExpression, func, options) {
super();
if(!options){
if (!options) {
options = {
scheduled: true,
recoverMissedExecutions: false
recoverMissedExecutions: false,
};
}

this.options = options;
this.options.name = this.options.name || uuid.v4();

Expand All @@ -28,24 +28,32 @@ class ScheduledTask extends EventEmitter {
if(options.scheduled !== false){
this._scheduler.start();
}

if(options.runOnInit === true){
this.now('init');
}
}

now(now = 'manual') {
let result = this._task.execute(now);
this.emit('task-done', result);
}

start() {
this._scheduler.start();
this._scheduler.start();
}

stop() {
this._scheduler.stop();
}

destroy () {
return this._scheduler.destroy();
}

getStatus () {
return this._scheduler.getStatus();
}
}

module.exports = ScheduledTask;
28 changes: 21 additions & 7 deletions src/scheduler.js
Expand Up @@ -3,8 +3,8 @@
const EventEmitter = require('events');
const TimeMatcher = require('./time-matcher');

class Scheduler extends EventEmitter{
constructor(pattern, timezone, autorecover){
class Scheduler extends EventEmitter {
constructor(pattern, timezone, autorecover) {
super();
this.timeMatcher = new TimeMatcher(pattern, timezone);
this.autorecover = autorecover;
Expand All @@ -22,11 +22,11 @@ class Scheduler extends EventEmitter{
const elapsedTime = process.hrtime(lastCheck);
const elapsedMs = (elapsedTime[0] * 1e9 + elapsedTime[1]) / 1e6;
const missedExecutions = Math.floor(elapsedMs / 1000);
for(let i = missedExecutions; i >= 0; i--){

for (let i = missedExecutions; i >= 0; i--) {
const date = new Date(new Date().getTime() - i * 1000);
let date_tmp = this.timeMatcher.apply(date);
if(lastExecution.getTime() < date_tmp.getTime() && (i === 0 || this.autorecover) && this.timeMatcher.match(date)){
if (lastExecution.getTime() < date_tmp.getTime() && (i === 0 || this.autorecover) && this.timeMatcher.match(date)) {
this.emit('scheduled-time-matched', date_tmp);
date_tmp.setMilliseconds(0);
lastExecution = date_tmp;
Expand All @@ -38,12 +38,26 @@ class Scheduler extends EventEmitter{
matchTime();
}

stop(){
if(this.timeout){
stop() {
if (this.timeout) {
clearTimeout(this.timeout);
}
this.timeout = null;
}

destroy() {
return new Promise((res, rej) => {
this.stop();
setTimeout(() => {
if (!this.getStatus()) return res(true);
return rej(false);
}, 15);
});
}

getStatus() {
return this.timeout && !this.timeout._destroyed;
}
}

module.exports = Scheduler;