How to run node server forever
The DEVO.PS team's obsession with JavaScript is no secret; node. js as the server side, Angularjs as the client, in a way, our stack is built with it. We build static clients and restful JSON The API approach means that we run a lot of node. js, and I have to admit that although all of node. JS is awesome, it still makes us headache when we run it in a production environment. Its tools and best practices are still scarce compared to some more mature languages (imagine: monitoring, logging, Error handling).
Run node js forever
So far, we still rely on pretty playful node-forever modules. It's a great module, but it's still missing some features:
- Limited monitoring and logging capabilities
- Poor support for process management configuration
- Cluster not supported
- Code base Aging (meaning frequent failure while upgrading node. js)
That's why we're going to write the PM2 module in the last few months. We'd like to give you a look first before we release the official version of the production environment.
Node forever
# # #PM2到底是什么个东西呢?
First thing you need to do isnpmto install it first: forever monitor node js
npm install -g pm2
Let's compare the table to the following:
Feature |
Forever |
PM2 |
Keep Alive |
? |
? |
Coffeescript |
? |
|
Log Aggregation |
|
? |
Api |
|
? |
Terminal Monitoring |
|
? |
Clustering |
|
? |
JSON Configuration |
|
? |
Now let me introduce a little bit of the main features ...
# # #原生的集群化支持
Node v0.6 introduces cluster features that allow you to share sockets across multiple node apps. The problem is that it doesn't run outside the container and requires some extra configuration to handle the main and child processes.
PM2 Native support handles this problem and does not require additional code: PM2 itself as the main process, then it encapsulates your code into a special cluster process, like node. js, adding some global variables to your code files.
To start a cluster that uses all CPU cores, you only need to type the following instructions:
$ pm2 start app.js -i max
And then:
$ pm2 list
Then it will show something like the following (ASCII UI FTW);
As you can see, the number of processes in your app now depends on your CPU cores.
# # #按照termcaps Management of-htop (System monitoring and process management software under Linux)
It'spm2 listgood enough to see all the running processes and their states through commands. But how do you track their resource consumption? Don't worry, use this command:
$ pm2 monit
You can get the CPU usage and memory footprint of the process (as well as the cluster).
Disclaimer: Node-usage does not support MacOS (any performance requirement) so far, but it works well under Linux.
Now, let's check out our cluster and the garbage collection on the memory stack, assuming you already have an HTTP benchmark tool (if not, you must use WRK):
$ express bufallo //Create an express app$ cd bufallo$ npm install$ pm2 start app.js -i max$ wrk -c 100-d 100 http://localhost:3000/
In another terminal, run the monitoring option:
$ pm2 monit
Yes ~
# # #实时集中log处理
Now you have to manage multiple cluster processes: One to crawl data, one to process data, and so on ... This means a lot of log, which you can deal with in an old-fashioned way:
$ tail -f /path/to/log1 /path/to/log2 ...
But we thought very thoughtful, we added thelogsfunction:
$ pm2 logs
# # #快速恢复
Now that things are going well, your process is buzzing and you need to do a hard restart (restart). Now? Yes, first of all, dump:
$ pm2 dump
You can then restore it from the file:
$ pm2 kill //让我们假设一个PM2停掉了$ pm2 resurect //我所有的进程又满血满状态复活了
# # #强健的API
For example, you want to monitor all the processes that are managed by PM2, and you want to monitor the state of the machines that are running them (and even want to create a angular app to invoke these APIs ...):
$ pm2 web
Open a browser input http://localhost:9615, I go!
# # # #对了, there are many features ...
- All tests Pass,
- The Next Generationupdate-rc.d(pm2 startup), of course it's still alpha,
- Change file Auto Restart () in development mode,pm2 devsame as draft,
- Automatically refreshes log,
- Quickly manage your apps with JSON files,
- An uncaught exception is recorded in the error log.
- Record the number and time of reboots,
- Automatically kills processes when exiting.
# #下一步计划?
First of all, you can go to GitHub to powder us (we like stars).
The PM2 we develop provides an advanced and complete node process management solution. We hope to have more people to help us: more pull requests. Some of the features that are still on the development roadmap will be completed as soon as possible, and these are:
- Remote Management/Status check,
- Embedded cross-process communication channel (message bus),
- V8 A memory leak check for garbage collection,
- Web interface,
- Monitoring of data persistence,
- Mail notifications.
Farewell Node-forever, hug pm2[Reprint]