Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses and challenges
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Calculator

0

84
Views
cronjob to go to database node.js

I have this:

cronJob = require('cron').CronJob;

var getTexts = new cronJob( '13 12 * * *', function(){
    var viewConformationEmails = "select * from clients";
    ibmdb.open(ibmdbconn, function(err, conn) {
        if (err) return console.log(err);
        conn.query(viewConformationEmails, function(err, rows) {
            if (err) {
                console.log(err);
            } else if (!err) {
                console.log("Success")
            }
    
            var arrayOfNumbers = []
            for (var i = 0; i < rows.length; i++) {
                arrayOfNumbers.push(rows[i].PHONE_NUMBER)
            }
            console.log("arrayOfNumbers: ", arrayOfNumbers)
    
            conn.close(function() {
            });
        });
    });
})

What I am doing here is setting a cronjob for 12:13 pm today, which already passed. And within that cronjob function, I go to my database and get the phone numbers, put them in an array, and then try to log the array.

However, at 12:13 pm it didn't go through. I tried this with twillo as well, and it went through. but I cannot figure out what I am doing wrong, or if I can even do what I am looking for. Is this possible

7 months ago · Juan Pablo Isaza
1 answers
Answer question

0

According to the cron documentation, the range parameters are:

Seconds: 0-59
Minutes: 0-59
Hours: 0-23
Day of Month: 1-31
Months: 0-11 (Jan-Dec)
Day of Week: 0-6 (Sun-Sat)

So, if you want to run schedule your task on 12:13pm, you need to set your parameters in this way:

const CronJob = require('cron').CronJob;

console.log('Before job instantiation');
const job = new CronJob('* 13 12 * * *', function() {
    const now = new Date();
    console.log('Task runs at', now);
});
console.log('After job instantiation');
job.start();

Note: setting this time without specifying the Seconds parameter, will run the task at every second between 12:13pm ~ 12:14pm internally. So, if you want to run your task once at a specific time, define the Seconds parameter:

const job = new CronJob('0 13 12 * * *', function() {
  // rest of the codes ...
}
7 months ago · Juan Pablo Isaza Report
Answer question
Find remote jobs