Running function every minute and sending message


I have this function:

    var getTexts = new cronJob( '*/1 * * * *', 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")
            }

            
            for (var i = 0; i < rows.length; i++) {
                // arrayOfNumbers.push(rows[i].NAME)
                // arrayOfNumbers.push(rows[i].PHONE_NUMBER)
                // arrayOfNumbers.push(rows[i].HOUR)
                // arrayOfNumbers.push(rows[i].MINUTE)
                var minute = rows[i].MINUTE;
                var hour = rows[i].HOUR;
                console.log(rows[i])
                var stringg = rows[i]["MINUTE"] + " " + rows[i]["HOUR"] + " * " + "* " + "*"


                var textJob = new cronJob( stringg, function(){
                    client.messages.create( { to:'xxx', from:'yyy', body:'Hello! Hope you’re having a good day!' }, function( err, data ) {});
                  },  null, true);
            }

            conn.close(function() {
            });
        });
    });
}, null, true)

what it is supposed to do is run a cronjob every 1 minute, which gets all the results from my clients table. I then loop through each result, and if it is the current time and it mathces the time in the database, then it sends the message. however, it runs the first cronjob at each minute of the day, and then let’s say my etxt is supposed to go our at 9:15, then it can’t get there fast enough, because it is still going through the first cronjob itteration. That’s my guess, but i’m not 100% sure and i cannot figure out why. Can someone help?

Source: JavaSript – Stack Overflow

October 29, 2021
Category : News
Tags: cron | javascript | node.js | twilio

Leave a Reply

Your email address will not be published. Required fields are marked *

Sitemap | Terms | Privacy | Cookies | Advertising

Senior Software Developer

Creator of @LzoMedia I am a backend software developer based in London who likes beautiful code and has an adherence to standards & love's open-source.