diff --git a/src/scheduled-task.js b/src/scheduled-task.js index 3bd49dc..d275135 100644 --- a/src/scheduled-task.js +++ b/src/scheduled-task.js @@ -5,28 +5,22 @@ const Task = require('./task'); const Scheduler = require('./scheduler'); class ScheduledTask extends EventEmitter { - constructor(cronExpression, func, options) { + constructor(cronExpression, func, { timezone, scheduled = true, recoverMissedExecutions = false, args = [] } = {}) { super(); - if(!options){ - options = { - scheduled: true, - recoverMissedExecutions: false - }; - } let task = new Task(func); - let scheduler = new Scheduler(cronExpression, options.timezone, options.recoverMissedExecutions); + let scheduler = new Scheduler(cronExpression, timezone, recoverMissedExecutions); - scheduler.on('scheduled-time-matched', (now) => { - let result = task.execute(now); + scheduler.on('scheduled-time-matched', (now, ...args) => { + let result = task.execute(now, ...args); this.emit('task-done', result); }); - if(options.scheduled !== false){ - scheduler.start(); + if(scheduled !== false){ + scheduler.start(...args); } - this.start = () => { - scheduler.start(); + this.start = (...args) => { + scheduler.start(...args); }; this.stop = () => { diff --git a/src/scheduler.js b/src/scheduler.js index b1ef3b6..7ae8814 100644 --- a/src/scheduler.js +++ b/src/scheduler.js @@ -10,7 +10,7 @@ class Scheduler extends EventEmitter{ this.autorecover = autorecover; } - start(){ + start(...args){ // clear timeout if exsits this.stop(); @@ -26,7 +26,7 @@ class Scheduler extends EventEmitter{ for(let i = missedExecutions; i >= 0; i--){ var date = new Date(new Date().getTime() - i * 1000); if(lastExecution.getTime() < date.getTime() && (i === 0 || this.autorecover) && this.timeMatcher.match(date)){ - this.emit('scheduled-time-matched', date); + this.emit('scheduled-time-matched', date, ...args); date.setMilliseconds(0); lastExecution = date; } diff --git a/src/task.js b/src/task.js index 66f4924..8634296 100644 --- a/src/task.js +++ b/src/task.js @@ -11,10 +11,10 @@ class Task extends EventEmitter{ this._execution = execution; } - execute(now) { + execute(now, ...args) { let exec; try { - exec = this._execution(now); + exec = this._execution(now, ...args); } catch (error) { return this.emit('task-failed', error); }