This is part 5 of my Node.js series, make sure you checkout the previous posts!
So far in the series we have determined that there are a lot of advantages migrating to Node.js, from programmer’s team ease of adoption to performance, but it does need a few tools though.
When running your code directly from the node or npm executable, as it happens in any other languages, errors will occur, so you do not want your services braking down every time someone forgets to treat some exception. You will also like to continuously deliver code updates without clients experiencing downtime or performance degrading.
To achieve this you should probably be checking PM2 out, from Keymetrics.
This tool will not only keep your code running, but it also gives you strong professional tools for monitoring, managing and even debugging your running code.
If you already have Node.js installed, all you need to do is run this on your console:
npm install pm2 -g
pm2 start myservice.js
If your console returns something like this, you are good to go:
[PM2] Starting /myservice.js in fork_mode (1 instance) [PM2] Done. ⇆ PM2+ activated | Web: https://app.pm2.io/#/r/MY_ID | Server: vrsbrazil | Conn: Axon PM2+ on-premise link: root.keymetrics.io ┌───────────┬────┬──────┬──────┬────────┬─────────┬────────┬─────┬───────────┬──────────┬──────────┐ │ App name │ id │ mode │ pid │ status │ restart │ uptime │ cpu │ mem │ user │ watching │ ├───────────┼────┼──────┼──────┼────────┼─────────┼────────┼─────┼───────────┼──────────┼──────────┤ │ myservice │ 0 │ fork │ 7398 │ online │ 0 │ 0s │ 0% │ 12.8 MB │ vrsbrazil│ disabled │ └───────────┴────┴──────┴──────┴────────┴─────────┴────────┴─────┴───────────┴──────────┴──────────┘ Use `pm2 show ` to get more details about an app
So this is the part where it gets interesting. If you checkout line 3, you will see an URL, the ID here is obviously changed but once you run it on your machine and reach out to this link, you will get to the cloud monitoring page, and it looks like this:
As you can see this is obviously a paid feature, but it can really level up your game. If you have up to 4 processes you may use the free version and there is actually a lot to it, the real time monitoring it offers is quite useful, but there are a lot of features you may get with the paid one.
Back on the console, like I mentioned in the beginning, you will want to have your service fault proof and this is one of the pre-built features, in case your code fails, PM2 will automatically start restart it for you. Of course there needs to be a way you make sure your processes will start automatically in case you need to restart your server and to do that all you need to do is run this at your console:
The console you return the next command you need to run in order to enable it to start on boot, in my case, this is what I got:
[PM2] Init System found: systemd [PM2] To setup the Startup Script, copy/paste the following command: sudo env PATH=$PATH:/usr/bin /home/vrsbrazil/.npm-global/lib/node_modules/pm2/bin/pm2 startup systemd -u vrsbrazil --hp /home/vrsbrazil
Make sure you run the suggested code and you should be all set.
So there you go then, this is a good head start for you to get your Node.js service running on fail-safe mode.
This is it for this post. I hope you enjoy it, and if you can, do let me know what you are building with it on the comments.
There is still a lot to talk about on this Node.js series so please follow me on twitter @vrsbrazil so you can get notified on the next post.
Thank you very much!