[英]node.js function stuck in infinite loop
I have this:我有这个:
var getTexts = new cronJob('* 5 * * * *', function() {
let weekday = ['SUNDAY', 'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY'][new Date().getDay()]
console.log(weekday)
var viewConformationEmails = "select " + weekday + " 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);
var today = new Date();
var time = today.getMinutes() + " " + today.getHours() + " * " + "* " + "*"
var errorJob = new cronJob(time, function() {
client.messages.create({
to: "1234567890",
from: '12055578708',
body: 'Error with your code: ' + err
}, function(err, data) {});
}, null, true);
}
//note: when you're testing, Monday = day 1
for (let i = 0; i < rows.length; i++) {
let todaysColumn = rows[i][weekday];
//userInfoString is the variable for splitting the string into 4 parts
let userInfoString = todaysColumn.split('|');
// console.log(userInfoString)
//userTextString is the string that holds the value of (ex) 01 11 * * 1
const userTextString = userInfoString[0]
//userPhoneNumber gets phone number from finalSessionString
const userPhoneNumber = userInfoString[1]
//userNameString is getting the name of the person training
const userNameString = userInfoString[2]
//trainerNameString is getting the name of the trainer
const trainerNameString = userInfoString[3]
let timeSplit = todaysColumn.split(' ')
let minute = timeSplit[0]
console.log(minute)
let today = new Date();
if (Math.abs(today.getMinutes() - Number(minute)) <= 5) {
client.messages.create({
to: userPhoneNumber,
from: '12055578708',
body: 'hello ' + userNameString + ', \nYour training session just finished with ' + trainerNameString
}, function(err, data) {
if (err) {
console.log("err: " + err)
}
console.log(data)
});
} else {
console.log("not working")
}
}
conn.close(function() {
// console.log("closed the function /login");
});
});
});
}, null, true)
now, let me explain.现在,让我解释一下。 my cronjob runs every 5 minutes, querying the database, which the returns all the rows for the date today.
我的 cronjob 每 5 分钟运行一次,查询数据库,该数据库返回今天日期的所有行。 it returns something like this:
它返回如下内容:
56 15 * * 2|1234567890|Gianluca A|contact@inderatech.com
then i go and split it up using the .split
you see at the beginning of the loop.然后我使用循环开始时看到的
.split
将其拆分。 I then do logic to see if the time returned from the db was in the last 5 minutes of the current time.然后我执行逻辑以查看从数据库返回的时间是否在当前时间的最后 5 分钟内。
It does this successfully.它成功地做到了这一点。 Now, the problem is, it sends the texts message, but it sends it an infinite amount of times.
现在,问题是,它发送了文本消息,但它发送了无限次。 I just need it to send 1 time.
我只需要它发送 1 次。 How can i fix this?
我怎样才能解决这个问题?
The cron expression has 6 total elements so the first is seconds. cron 表达式总共有 6 个元素,所以第一个是秒。 You have a
*
so you've set it to run every second during the fifth minute of the hour so it will run 60 times in a row.您有一个
*
因此您已将其设置为在每小时的第五分钟内每秒运行一次,因此它将连续运行 60 次。
The cron expression for "every 5 minutes" is */5 * * * *
(note the slash, and 5 not 6 elements in the expression). “每 5 分钟”的 cron 表达式是
*/5 * * * *
(注意斜杠,表达式中是 5 而不是 6 个元素)。
'rows.length' 的值是多少?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.