简体   繁体   中英

Can node-cron run every minute offset by a few seconds

I can schedule node-cron to run every minute or every thirty seconds (see code), but I'd like it to run half-past every minute. The reason why is I also have another task scheduled once per hour and I'd like the minute task to be complete before the hourly task is run.

The minute task reads Modbus values and logs to a MongoDB database. The hourly task grabs all of the records and POSTs them to a REST endpoint and logs the sync time. Because both tasks run at the same time on the hour, one value is left out of the POST.

var cron = require('node-cron');

cron.schedule('* * * * *', () => { // Every minute
    console.log("--------------------------------------------------");
    console.log('Cron Task - Time: ' + (new Date()));
    console.log("--------------------------------------------------");
});

cron.schedule('*/30 * * * * *', () => { // Every 30 seconds
    console.log("--------------------------------------------------");
    console.log('Cron Task - Time: ' + (new Date()));
    console.log("--------------------------------------------------");
});

you should manege your schedule and say to CPU run that after which one, first running 30 second, after that one minute and finally hour schedule is running. At the next time for minute and hour all of thing is ok and automate run it.

var cron = require('node-cron');
var minutsBoolean = false;
var hourBoolean = false;

function oneHourIsRun() {
    cron.schedule('* 0-23 * * *', () => { // Every hour
        console.log("--------------------------------------------------");
        console.log('Cron Task - Time: ' + (new Date()));
        //hourly task grabs all of the records and POSTs them to a REST endpoint and logs the sync time
        console.log("--------------------------------------------------");
    });
}
function oneMinutsIsRun() {
    cron.schedule('* * * * *', () => { // Every one minuts, runnig after 30 second
        console.log("--------------------------------------------------");
        console.log('Cron Task - Time: ' + (new Date()));
        //minute task reads Modbus values and logs to a MongoDB database .then() {below code in here}
        if (!hourBoolean) {
            oneHourIsRun();
            hourBoolean = true;
        }
        console.log("--------------------------------------------------");
    });
}

cron.schedule('30 * * * * *', () => { // Every 30 second, */30 ruunig in 30 minuts of hour not 30 secend
    console.log("--------------------------------------------------");
    console.log('Cron Task - Time: ' + (new Date()));
    if (!minutsBoolean) {
        oneMinutsIsRun();
        minutsBoolean = true;
    }
    console.log("--------------------------------------------------");
});

Through some trial an error, I figured it out. The following runs half past every minute:

var cron = require('node-cron');

cron.schedule('30 0-59 * * * *', () => { // Every minute offset 30 seconds
    console.log("--------------------------------------------------");
    console.log('Cron Task - READ - Time: ' + (new Date()));
    console.log("--------------------------------------------------");
});

/*
--------------------------------------------------
Cron Task - READ - Time: Tue Oct 01 2019 10:09:30 GMT-0400 (EDT)
--------------------------------------------------
--------------------------------------------------
Cron Task - READ - Time: Tue Oct 01 2019 10:10:30 GMT-0400 (EDT)
--------------------------------------------------
--------------------------------------------------
Cron Task - READ - Time: Tue Oct 01 2019 10:11:30 GMT-0400 (EDT)
--------------------------------------------------
--------------------------------------------------
Cron Task - READ - Time: Tue Oct 01 2019 10:12:30 GMT-0400 (EDT)
--------------------------------------------------
*/

I ended up keeping the read task running every minute on the minute, but made the hourly sync task run 30 seconds after the hour:

var cron = require('node-cron');
const schedule_read = require('./schedule_read');
const schedule_sync = require('./schedule_sync');

cron.schedule('* * * * *', () => { // Every minute
    console.log("--------------------------------------------------");
    console.log('Cron Task - READ - Time: ' + (new Date()));
    console.log("--------------------------------------------------");
    schedule_read.read();
});

cron.schedule('30 0 0-23 * * *', () => { // Every hour offset by 30 seconds
    console.log("--------------------------------------------------");
    console.log(' Cron Task - SYNC - Time: ' + (new Date()));
    console.log("--------------------------------------------------");
    schedule_sync.sync();
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM