Currently I'm using node-cron package to schedule the task. I can make the task running but I can't make it stop.
My POST body is:
{
command: start/stop/destroy,
}
The code is:
export const schedule = async (req, res) => {
const cmd = req.body.command
const url_taskMap = {}
// run every midnight GMT+7
const task = await cron.schedule(process.env.SCHEDULE_TIME, () => {
console.log('doing task');
}, {
scheduled: true,
timezone: "Asia/Bangkok"
})
url_taskMap['url'] = task;
let my_job = url_taskMap['url'];
if (cmd === 'start') {
my_job.start()
res.status(200).send('Task started!')
} else if (cmd === 'stop') {
my_job.stop()
res.status(200).send('Task stoped!')
} else {
my_job.destroy()
res.status(200).send('Task destroyed!')
}
}
I follow the answer here https://stackoverflow.com/a/53684854/15088319 but the task still running. I don't know how to make it stop. Can you help me?