Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Node-Schedule trigger function each 2 hours

I am using the package node-schedule to avoid using setInterval in my application, I need to trigger 2 functions, one each 30 minuts and one each 2 hours, on the node-schedule docs I can see :

*    *    *    *    *    *
┬    ┬    ┬    ┬    ┬    ┬
│    │    │    │    │    │
│    │    │    │    │    └ day of week (0 - 7) (0 or 7 is Sun)
│    │    │    │    └───── month (1 - 12)
│    │    │    └────────── day of month (1 - 31)
│    │    └─────────────── hour (0 - 23)
│    └──────────────────── minute (0 - 59)
└───────────────────────── second (0 - 59, OPTIONAL)

So for my function who needed to be triggered each 30min I done this :
*/30 * * * * and this is working, the function is triggered each 30min.

For my second function (who need to be triggered each 2 hours) I used this: * */2 * * * and thats not working at all unfortunately (it seems like it trigger each 10 mins)..

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

If someone could help me thanks in advance !

Here is my code if necessary :

import { scheduleJob } from 'node-schedule';

(async function() {
    const firstScheduler = scheduleJob('* */2 * * *', async function() {
        // Not Working
    });
    const secScheduler = scheduleJob('*/30 * * * *', async function() {
        // Working
    });
})();

>Solution :

* means ‘every’. Your current expression means, every minute of every 2 hours (12 am, 2 am, 4 am, …).

Adding 0 in the minute section will resolve it. It executes exactly at 0th minute of every 2 hours.

const firstScheduler = scheduleJob('0 */2 * * *', async function() {
     // Run every 2 hours
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading