how can i schedule task from node-cron in vuejs

475 Views Asked by At

this is how to use it according to the docs in node js each * has what it does

cron.schedule('* * * * *', () => {
  console.log('running a task every minute');
});

 # ┌────────────── second (optional)
 # │ ┌──────────── minute
 # │ │ ┌────────── hour
 # │ │ │ ┌──────── day of month
 # │ │ │ │ ┌────── month
 # │ │ │ │ │ ┌──── day of week
 # │ │ │ │ │ │
 # │ │ │ │ │ │
 # * * * * * *

for example i want a mail to be sent every 10 seconds i do something like this

cronJob.schedule('10 * * * * *', () => {
})

how can i get the 10 seconds from my vue component and pass it to my cron job

2

There are 2 best solutions below

2
Rohit Jogi On
cron.schedule('*/10 * * * * *', () => {

 // other code goes here 

})

//This will run after every 10 sec

0
4x0t On

To send email from a node-cron script, you'll need the nodemailer npm package. Following that, you must configure your email by importing the package into your node-cron file and specifying the sending details for your message, a transporter, and mailoption:

    var transporter = nodemailer.createTransport({
   service: 'gmail',
    auth: {
           user: 'yoursendingmail',   
          pass:'yoursendingpassword'              
         },
        const mailOptions = { 
                  from: '[email protected]', 
                  to: '[email protected],          
                  subject: 'sub here',  
                  html: '<p>text here</p>'}
 };)

after, you set up the node-cron like this:

cronJob.schedule('10 * * * * *', () => {
  transporter.sendMail(mailOptions, function (err, info)
    {
      if(err){}
      else{}//can log info here if email is sent
    }
})