Tuesday, May 1, 2018

What is PM2 ? - Some quick learning notes

From the definition from the official site, it appeared like below.

PM2 empowers your process management workflow. It allows you to fine-tune the behavior, options, environment variables, logs files of each application via a process file. It’s particularly useful for micro-service based applications.

Configuration format supported are Javascript, JSON and YAML.

what mattered to me most as part of learning is

- Ability to keep the code running even if it crashes out due to some reason
- Ability to get the log files

Basic usage is :
pm2 start app.js
To install, follow the below
npm install pm2@latest -g

It seems we can create a configuration file to manage multiple applications. Like below

process.yml

apps:
  - script   : app.js
    instances: 4
    exec_mode: cluster
  - script : worker.js
    watch  : true
    env    :
      NODE_ENV: development
    env_production:
      NODE_ENV: production


pm2 start process.yml
Next much useful thing about the PM2 was it as Process Management tool.

PM2 manages application states so that it can start, stop, restart and delete processes.
Some useful commands are :

pm2 start app.js --name "my-api"
pm2 start web.js --name "web-interface"
pm2 restart web-interface
pm2 stop web-interface

to list all running processes pm2 list
pm2 show 0
pm2 list --sort name:desc
pm2 list --sort [name|id|pid|memory|cpu|status|uptime][:asc|desc]

PM2 allows to restart an application based on a memory limit.
pm2 start big-array.js --max-memory-restart 20M

Other interesting thing about the PM2 was the folder structure especially to redirect the application logs to the file. Below are the directory structure

$HOME/.pm2 will contain all PM2 related files
$HOME/.pm2/logs will contain all applications logs
$HOME/.pm2/pids will contain all applications pids
$HOME/.pm2/pm2.log PM2 logs
$HOME/.pm2/pm2.pid PM2 pid
$HOME/.pm2/rpc.sock Socket file for remote commands
$HOME/.pm2/pub.sock Socket file for publishable events
$HOME/.pm2/conf.js PM2 Configuration


references:
http://pm2.keymetrics.io/docs/usage/quick-start/
http://pm2.keymetrics.io/docs/usage/process-management/#max-memory-restart

No comments:

Post a Comment