Skip to content

Commit

Permalink
Merge 5c85c84 into 1dd2e57
Browse files Browse the repository at this point in the history
  • Loading branch information
massimo-ua committed Jul 21, 2020
2 parents 1dd2e57 + 5c85c84 commit 8a5bfd7
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 18 deletions.
22 changes: 8 additions & 14 deletions src/scheduled-task.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit 8a5bfd7

Please sign in to comment.