简体   繁体   中英

Forever Node.js Script Hangs Up on Loop

I have made a Node.js script which checks for new entries in a MySQL database and uses socket.io to send data to the client's web browser. The script is meant to check for new entries approximately every 2 seconds. I am using Forever to keep the script running as this is hosted on a VPS.

I believe what's happening is that the for loop is looping infinitely (more on why I think that's the issue below). There are no error messages in the Forever generated log file and the script is "running" even when it's started to hang up. Specifically, the part of the script that hangs up is the script stops accepting browser requests at port 8888 and doesn't serve the client-side socket.io js files. I've done some troubleshooting and identified a few key components that may be causing this issue, but at the end of the day, I'm not sure why it's happening and can't seem to find a work around.

Here is the relevant part of the code:

http.listen(8888,function(){
    console.log("Listening on 8888");
});

function checkEntry() {
    pool.getConnection(function(err,connection) {
        connection.query("SELECT * FROM `data_alert` WHERE processtime > " + (Math.floor(new Date() / 1000) - 172800) + " AND pushed IS NULL", function (err, rows) {
            connection.release();
            if (!err) {
                if(Object.keys(rows).length > 0) {
                    var x;
                    for(x = 0; x < Object.keys(rows).length; x++) {
                        connection.query("UPDATE `data_alert` SET pushed = 1 WHERE id = " + rows[x]['id'],function() {
                            connection.release();
                            io.emit('refresh feed', 'refresh');
                        });
                    }
                }
            }
        });
    });
    setTimeout(function() { checkEntry();var d = new Date();console.log(d.getTime()); },1000);
}

checkEntry();

Just a few interesting things I've discovered while trouble shooting...

  • This only happens when I run the script on Forever. Work's completely fine if I use shell and just leave my terminal open.
  • It starts to happen after 5-30 minutes of running the script, it does not immediately hang up on the first execution of the checkEntry function.
  • I originally tried this with setInterval instead of setTimeout, the issue has remained exactly the same.
  • If I remove the setInterval/setTimeout function and run the checkEntry function only once, it does not hang up.
  • If I take out the javascript for loop in the checkEntry function, the hang ups stop (but obviously, that for loop controls necessary functionality so I have to at least find another way of using it).
  • I've also tried using a for-in loop for the rows object and the performance is exactly the same.

Any ideas would be immensely helpful at this point. I started working with Node.js just recently so there may be a glaringly obvious reason that I'm missing here.

Thank you.

So I just wanted to come back to this and address what the issue was. It took me quite some time to figure out and it can only be explained by my own inexperience. There is a section to my script where my code contained the following:

app.get("/", (request, response) => {
    // Some code to log things to the console here.
});

The issue was that I was not sending a response. The new code looks as follows and has resolved my hang up issues:

app.get("/", (request, response) => {
    // Some code to log things to the console here.
    response.send("OK");
});

The issue had nothing to do with the part of the code I presented in the initial question.

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