简体   繁体   English

如果响应时间太大,请重新启动node.js

[英]restart node.js forever process if response time too big

I got forever script for managing node.js site. 我得到了永远用于管理node.js站点的脚本。

Sometimes node.js site hangs and response time go above 30 seconds. 有时node.js站点挂起,响应时间超过30秒。 And in fact site is down. 实际上网站已经关闭。 Then fast cure for it is restarting forever : 然后快速治愈它forever重新启动:

$ forever restart 3

where 3 is script number in forever list . 其中3forever list脚本编号。

Is it possible to make it automatically? 可以自动制作吗? Is there option in forever which make it restart if response time will be more than 2 seconds for example? 如果响应时间超过2秒,是否会forever存在使其重新启动的选项?

Or maybe I got to run external script which will check response time and make descension to restart hanging forever script. 或者,也许我得走了外部脚本,它会检查响应时间,使降温者重新启动吊forever脚本。

Or maybe I need to write this logic inside my node.js site? 或者我可能需要在node.js网站中编写这个逻辑?

I am assuming you want to restart the server if most of the reply are taking longer than x seconds. 我假设您要重新启动服务器,如果大多数回复时间超过x秒。 There are many tools that helps you to restart your instances based on their health. 有许多工具可以帮助您根据健康状况重新启动实例。 Monit is one of them. Monit就是其中之一。 In this guide, monit restart the instance if reply doesn't come back in 10 seconds. 指南中,如果回复未在10秒内返回,则monit会重新启动实例。

If you want to kill the instance if one request if any of the requests are taking too long, then you note down the time when you take in the request, and note down the time when request leaves. 如果你想要杀死一个实例,如果有任何一个请求需要花费太长时间,那么你记下你接收请求的时间,并记下请求离开的时间。 If the time is too long, throw an exception that you know will not get caught and the server would restart by itself. 如果时间太长,抛出一个你知道不会被捕获的异常,服务器会自动重启。 If you use express, then check the code for their logger under development mode, as it tracks the response time. 如果使用express,则在开发模式下检查其记录器的代码,因为它跟踪响应时间。

Aside leorex solution , I have something like this before to send 500 on timed out requests: 除了leorex解决方案 ,我之前有类似的东西发送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);
};

You can use addTimeout module to take away the timeout clearing part. 您可以使用addTimeout模块来取消超时清除部分。

Once you implemented, you can handle as you like, you can just call process.exit(1); 一旦实现,你可以随意处理,你可以调用process.exit(1); so forever will immediately replaces you. 所以永远会立即取代你。

You can make this smarter. 你可以让这更聪明。 Also in my application, if an uncaught error happens, I signal the supervisor process so that it will spin up another worker process and gracefully go down(close http server and wait for all pending requests to finish). 同样在我的应用程序中,如果发生未被捕获的错误,我会通知主管进程,以便它将启动另一个工作进程并正常关闭(关闭http服务器并等待所有挂起的请求完成)。 You can do the same in your application, but make sure everything has a timeout callback as a failover/backup plan. 您可以在应用程序中执行相同操作,但请确保所有内容都具有超时回调作为故障转移/备份计划。

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

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