Restart node.js forever if the response time is too long

I got forever a script to manage the site node.js.

Sometimes the node.jswebsite freezes and the response time exceeds 30 seconds. And actually the site is down. Then the quick cure for it restarts forever:

$ forever restart 3

where 3is the script number in forever list.

Is it possible to do this automatically? Is there an option in foreverthat causes it to restart if the response time is more than 2 seconds, for example?

Or maybe I need to run an external script that will check the response time and make a descension to restart the hanging foreverscript.

Or maybe I need to write this logic on my node.jssite?

+4
source share
2 answers

I assume you want to restart the server if most of the response takes more than x seconds. There are many tools available to help you restart your instances based on their health. Monit is one of them. In this guide, monit restarts the instance if the response does not return after 10 seconds.

, , - , , , , . , , , , , . , , .

+3

leorex solution, - , 500 :

var writeHead = res.writeHead;
var timeout = setTimeout(function () {
    res.statusCode = 500;
    res.end('Response timed out');
    // To avoid errors being thrown for writes after this, they should be ignored.
    res.writeHead = res.write = res.end = function () {};
}, 40000);
res.writeHead = function () {
    // This was called in time.
    clearTimeout(timeout);
    writeHead.apply(this, arguments);
};

addTimeout , -.

, , , process.exit(1);, .

. , , , ( http- ). , , - / .

+1

Source: https://habr.com/ru/post/1540977/


All Articles