简体   繁体   中英

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?

Figured it out by doing this:

var getTexts = new cronJob( '45 * * * * *', function(){

what it does is run the cronjob every 45 seconds , which then will send out the text message at the designated time. works perfect, so far ...

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