[Whole Web] [Node.js, PM2] Controlling runaway apps using pm2
shows how to enable features in your pm2 config file that allow you to prevent runaway apps from bringing your server down. Setting max memory used, number of running processes allowed and maximum restart attempts are all covered.
{ "apps": [{ "name": "App1", "script": "app1/server.js", "log_file": "/var/log/pm2/app1.log", "error_file": "/var/log/pm2/app1-err.log", "watch": true, "ignore_watch": ["node_modules"], "env": { "NODE_ENV": "production" }, "instances": 0, "max_memory_restart": "16G", "max_restarts": 10 },{ "name": "App2", "script": "app2/server.js", "log_file": "/var/log/pm2/app2.log", "error_file": "/var/log/pm2/app2-err.log" } ] }
instances
There are three other options I want to cover real quickly before we wrap this up, and they're done inside the config file again.
The first one I want to cover is the instances object. What instances will do is spin up the number of processes defined here. If we set this to zero, PM2 is going to spin up one process for every CPU you have in your system.
If you want a little finer control of that, you can put in two, four, six, seven, whatever number you'd like to see as far as the number of running processes. The reason the instances option is so cool is because it allows you to do rolling restarts, and graceful reloads of your app.
In the event that you deploy code and issue a reload command to PM2, what's going to happen is it's not going to kill any active connections.
As those connections close or go away, it will stop them and bring up new processes that contain your new code. What essentially happens there is that anyone who's currently using your website during that reload command won't be disconnected.
Their session will stay, and they'll be allowed to finish whatever operation they were on without having their process killed. This creates a much more professional experience for your users.
max_memory_restart
Another cool option that may be handy for you in times of desperate need is the max memory restart. You can use this...let's say that you've been profiling your note app, and you've noticed that if you get above eight gig of memory utilization, your app starts behaving very sporadic and is prone to crash. What you can actually tell PM2 to do is just when you get to eight gig of memory utilization, to go ahead and restart your app.
Clearly, this isn't something that you want to depend on, or it doesn't...you don't want this to be your everyday running configuration. If you've got a bug that you just haven't been able to find, and you've got to keep your site up for your customers, this is going to help you out.
max_restart
The last one we'll cover is max restarts, which is probably a good one to use in combination with max memory restart. What this will do is tell PM2 to only restart your app the number of times listed here, for our example, 10 times.
After 10 times, if your app crashes, PM2 is going to bring that down or it's going to leave it in erred state and it won't attempt to restart it until you stop and restart the entire PM2 process.
This is just like a safety valve, and it's going to keep a rogue app on your system from consuming all your system resources by continually restarting. When it hits that max number, PM2 will just let it die and save your system until you can come in and manually intervene.
相关文章
- node.js 安装与环境变量配置
- web前端node.js常用命令
- node.js(2)
- 如何使用JSubFinder从网页JS代码中寻找到敏感信息
- 【JS 逆向百例】猿人学系列 web 比赛第二题:js 混淆 - 动态 cookie,详细剖析
- JS的StringBuffer类的实现及使用详解编程语言
- 建立一个node.js服务器(使用express搭建第一个Web环境)详解编程语言
- 使用express搭建第一个Web应用Node.js初学详解编程语言
- 使用html+css+js实现魔性的舞蹈详解编程语言
- JS arguments对象详解
- 快速在Linux平台上安装Node.js(node安装linux)
- 如何使用Node.js连接Oracle数据库(node连接oracle)
- 网易的基于Node.js的游戏服务器开源框架:Pomelo
- node.js与neo4j构建扩展性Web应用(nodeneo4j)
- Exploring the Power of Linux and Node.js for Web Development: A Comprehensive Guide(linuxnodejs)
- Node.js连接MSSQL数据库的操作指南(node 链接mssql)
- 使用Node.js实现对MSSQL数据库的连接(node连接 mssql)
- Node.js和MSSQL数据库的完美结合:使用node mssql模块(node mssql模块)
- 妙用JS获取Oracle信息的快速方法(js获取Oracle)
- 推荐js实现商品分类到搜索栏友好提示(人机交互)
- 基于jQuery的js分页代码
- js+css使DIV始终居于屏幕中间左下左上右上右下的代码集合
- JS解析json数据并将json字符串转化为数组的实现方法
- 利用js的Node遍历找到repeater的一个字段实例介绍
- js中的switch表达式使用示例
- js实现日期级联效果
- js/jquery获取文本框输入焦点的方法
- node.js入门教程
- 原生js和jQuery随意改变div属性style的名称和值
- node.js+Ajax实现获取HTTP服务器返回数据
- node.js中的events.emitter.removeListener方法使用说明
- node.js中的fs.futimes方法使用说明
- node.js解决获取图片真实文件类型的问题