I see from docs you can delete one by one by name for example ...
var schedule = require('node-schedule');
// sample announcement
var rule = new schedule.RecurrenceRule();
rule.dayOfWeek = [1, 2, 3, 4, 5];
rule.minute = 50;
rule.hour = 12;
var message = schedule.scheduleJob("AnnouncementOne", rule, function() {
// make my announcement
})
AnnouncementOne.cancel();
but I would like code to get all scheduled jobs and then loop through them to delete each one. I think the following gets all the jobs ...
var jobList = schedule.scheduledJobs;
which outputs following to console ...
{ 'AnnouncementOne':
Job {
job: [Function],
callback: false,
name: 'AnnouncementOne',
trackInvocation: [Function],
stopTrackingInvocation: [Function],
triggeredJobs: [Function],
setTriggeredJobs: [Function],
cancel: [Function],
cancelNext: [Function],
reschedule: [Function],
nextInvocation: [Function],
pendingInvocations: [Function] },
'AnnouncementTwo':
Job {
job: [Function],
callback: false,
name: 'AnnouncementTwo',
trackInvocation: [Function],
stopTrackingInvocation: [Function],
triggeredJobs: [Function],
setTriggeredJobs: [Function],
cancel: [Function],
cancelNext: [Function],
reschedule: [Function],
nextInvocation: [Function],
pendingInvocations: [Function] } }
how do I loop through jobList to delete each job?
Alternative Code:
var jobList = schedule.scheduledJobs;
for(jobName in jobList){
var job = 'jobList.' + jobName;
eval(job+'.cancel()');
}
It is better to keep away from eval
const schedule = require('node-schedule');
const _ = require('lodash');
const j = schedule.scheduleJob('SomeDirtyWork', '42 * * * *', () => {
// Something is going on
});
const jobNames = _.keys(schedule.scheduledJobs);
for(let name of jobNames) schedule.cancelJob(name);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With