Header Ads

Cron Implementation

Cron Implementation


Schedule the jobs to handle the functionality at specific times and intervals based on user implementation. Node cron package used to handle this functionality. Basically, this will be used for repeated time and repeated notification and email. 


How to create a cron

  •  create a new application in node.
  • Add some user functionality based on the requirement
  • Add the node-cron package
  • Then schedule a task like ( repeated notification or email).

How to implement a cron
  • First, create a new service name as cron. service
  • Then add the functionality in cron service
  • Call this service in app.js
  • Use the schedule cron job time in cronTab
  • Then run the application and it will automatically run the functionality based on cron time.


How to use it

Install the node cron package. cronJobs

Add the .env file. Schedule the time in every day at 12am.

Cron_time_functionality time=0 0 * * *


Add the cron service file, then add the functionality to update DB for all users.


const CronJobs = require('cron).CronJob;

const UpdateUserData = async() => {

    return new CronJobs({ Cron_time_functionality ,  Onclick: () => {

        updateUserData().then((response) => {

                   console.log("successfully updated the user data @ 12 am every day")

        }).catch(error) => {

                    throw (error).

            }

     })

}

Call the service in the app.js and it will automatically run this cron job.


Post a Comment

0 Comments