简体   繁体   中英

How to Run Cron Task Once with Node-Schedule

I used node-schedule to schedule a task executed only once for all at particular time. The doc shows that for me:

Date-based Scheduling: Say you very specifically want a function to execute at 5:30am on December 21, 2012. Remember - in JavaScript - 0 - January, 11 - December.

 const schedule = require('node-schedule'); const date = new Date(2012, 11, 21, 5, 30, 0); const job = schedule.scheduleJob(date, function(){ console.log('The world is going to end today.'); });

But this code runs the task every minute, I just want it to run the task only once at the particular time specified as here:

const date = new Date(2012, 11, 21, 5, 30, 0);

The date here is a default. I changed it for future date in my code as below:

const now = new Date().getTime();
const seconds = 180;
const parsedDate = new Date(Date.parse(now));
const date = new Date(parsedDate.getTime() + (1000 * seconds));
schedule.scheduleJob(date, function(data) {
    console.log("Job ran @", new Date().toString());
});

Here I just want to cron a job after 180 seconds (3 minute) from current date. But console result shows that console log is written every minutes after deploy:

>  Job ran @ Tue Mar 16 2021 08:56:00 GMT+0000 (GMT)
>  Job ran @ Tue Mar 16 2021 08:57:00 GMT+0000 (GMT)
>  Job ran @ Tue Mar 16 2021 08:58:00 GMT+0000 (GMT)
>  Job ran @ Tue Mar 16 2021 08:59:00 GMT+0000 (GMT)

Thanks for your help.

I get over this issue by adding time zone into my code. I use an answer from another post. The function here comes with adding time zone feature to Date function in easy way.

This should solve your problem, please feel free to offer fixes. This method will account also for daylight saving time for the given date.

 Date dateWithTimeZone = (timeZone, year, month, day, hour, minute, second) => { let date = new Date(Date.UTC(year, month, day, hour, minute, second)); let utcDate = new Date(date.toLocaleString('en-US', { timeZone: "UTC" })); let tzDate = new Date(date.toLocaleString('en-US', { timeZone: timeZone })); let offset = utcDate.getTime() - tzDate.getTime(); date.setTime( date.getTime() + offset ); return date; };

And then my code looks like that:

schedule.scheduleJob(dateWithTimeZone("Europe/Istanbul",
      req.body.year, req.body.month, req.body.day, req.body.hour,
      req.body.minute, req.body.second), function(data) {
    console.log("Job ran @", new Date().toString());
});

And finally it works fine for me. I can schedule job executed only once at the time I prefer in my time zone.

Thanks for your attention. Regarding...

I found a simple way to schedule a cron job.

const schedule = require('node-schedule');
const moment = require('moment');

const rule = scheduled.RecurrenceRule();

let time = '2021-07-30 14:20:00';
rule.tz = 'Asia/Kolkata';
rule.year = moment(time).year();
rule.month = moment(time).month();
rule.date = moment(time).date();
rule.hour = moment(time).hours();
rule.minute = moment(time).minutes();
rule.second = moment(time).seconds();
schedule.scheduleJob(rule, async function () {
     console.log("Scheduled")
});

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