简体   繁体   中英

How to create user specific Cron Job in Node.js?

Requirement

I want to create an email scheduling system in which user set the time to schedule a email sending. Now in this case every user can set their appropriate time to send an email everyday. Now how can I tackle this for individual user?

Where I stuck? If I run the cron job every minute to verify the scheduled time set by the users and if the previous call of Cron() function is not finished then it will not run for the second time until the previous execution completes. and hence same task for the other users will not start. So do I need to create a separate cron job for each user? And if so then How can I implement that?

Cron Function Inside Cron function I am fetching all the users whose time is matching with the current time and then sending them email.

const job = new CronJob({
    cronTime: `* * * * *`,
    onTick: function () {
        Cron().catch((err) => console.error(`Error --> ${err.stack}`))
    },
    start: false,
    timeZone: `Asia/Kolkata`
})

job.start()

You Events in Nodejs (multithreading). With the help of MultiThreading, you can run multiple users tasks without stopping in between.

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