简体   繁体   English

每分钟运行一次函数并发送消息

[英]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.它应该做的是每 1 分钟运行一次 cronjob,它从我的clients表中获取所有结果。 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.但是,它在一天中的每一分钟运行第一个 cronjob,然后假设我的 etxt 应该在 9:15 到达我们的位置,然后它无法足够快地到达那里,因为它仍在经历第一个 cronjob 迭代. That's my guess, but i'm not 100% sure and i cannot figure out why.这是我的猜测,但我不是 100% 确定,我不知道为什么。 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.它的作用是每45 seconds运行一次 cronjob,然后在指定的时间发送短信。 works perfect, so far ...工作完美,到目前为止......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM