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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add additional callback arguments pass through #190

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 8 additions & 14 deletions src/scheduled-task.js
Expand Up @@ -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 = () => {
Expand Down
4 changes: 2 additions & 2 deletions src/scheduler.js
Expand Up @@ -10,7 +10,7 @@ class Scheduler extends EventEmitter{
this.autorecover = autorecover;
}

start(){
start(...args){
// clear timeout if exsits
this.stop();

Expand All @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions src/task.js
Expand Up @@ -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);
}
Expand Down