'How can I execute multiple cron jobs at different timings using node-cron in nodejs?
How can I execute this with different timings? Please help on this. Thanks in Advance...
const Cron = require('node-cron');
const Cron2 = require('node-cron');
var TaskOne = Cron.schedule('*/10 * * * *', async() => {
//first job implemented here and its working fine
function1();
});
var TaskTwo = Cron2.schedule('*/11 * * * *', async() => {
// Second job implemented here....
//Why this block is not getting executed???????????????????????
function2();
});
How can I execute this with different timings? TaskTwo is not getting executed. Debugger not goes into TaskTwo. Please help on this. Thanks in Advance...
Solution 1:[1]
if node-cron
is not the only prefrence for you then you can use https://github.com/agenda/agenda
Solution 2:[2]
There's no need to require
the same package twice, so you should remove Cron2
on the second line. Thus the line var TaskTwo = Cron2.schedule('*/11 * * * *', async() => {
should be changed to:
var TaskTwo = Cron.schedule('*/11 * * * *', async() => {
As for why the second schedule is not working, it might be because you didn't start()
the schedule, as shown in https://github.com/node-cron/node-cron#start. So adding the code below at the end of your script might trigger both cron to run:
TaskOne.start();
TaskTwo.start();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | code_thirsty |
Solution 2 | Tyler2P |