@namnguyen.repl.it/nn-scheduler
v2.0.0
Published
A scheduler module for NodeJS
Readme
Example usage with timeout handling in actions
class MyAction {
static longRunningAction() {
return async () => {
try {
//console.log('Long running action started');
await new Promise((resolve, reject) => {
const timeout = setTimeout(() => reject(new Error('Action timed out')), 90 * 1000);
setTimeout(() => {
clearTimeout(timeout);
resolve();
}, 120 * 1000);
});
//console.log('Long running action completed');
} catch (error) {
console.error(Long running action failed: ${error});
throw error;
}
};
}
static failingAction() {
return async () => {
try {
//console.log('Failing action started');
await new Promise((_, reject) => setTimeout(() => reject(new Error('Action failed')), 1000));
//console.log('Failing action completed');
} catch (error) {
console.error(`Failing action failed: ${error}`);
throw error;
}
};
}}
const scheduler = new Scheduler({ logHandler: (msg) => //console.log(msg) });
scheduler.schedule([ { time: '07:15', action: MyAction.longRunningAction(), weekDays: ['*'] }, { time: '09:15', action: MyAction.failingAction(), weekDays: [6, 1, 2, 3] }, ]);
scheduler.interval([ { interval: 60 * 1000, action: MyAction.longRunningAction(), weekDays: ['*'] }, { interval: 60 * 1000, action: MyAction.failingAction(), weekDays: [2, 3, 4] }, ]);
Using PM2 to manage tasks: pm2 start scheduler.js --name scheduler pm2 list pm2 logs scheduler pm2 stop scheduler pm2 restart scheduler pm2 delete scheduler
