PM2 v2 - Bradbury
by Alexandre Strzelewicz -



After months of discussion with our growing & outstanding user community and hundreds of issues solved on Github we are happy to announce the release of PM2 V2. PM2 V2 is about increased stability and performance, better support for Windows, and better integration for Docker users!

Also PM2 has reached some amazing milestones:

Tiny memory and CPU footprint

Thanks to a code reflow, CPU usage has been dropped by over 60% in idle and by 40% in stressed environments.

Overall memory usage has been reduced by over 30%.



Keymetrics dashboard with pm2-probe module

This improvement in resource consumption now makes PM2/Keymetrics a first-class citizen on tiny computers like Raspberry Pi and Node.js IoT. Even more when PM2 will run on Node v7.

Specialized CLI for Docker

Thanks to numerous feedback from the community on using pm2 inside docker, we decided to create a custom binary that integrates seamlessly with Docker. This CLI tool is available as soon as you update pm2.

pm2-docker is automatically configured to perfectly fit with Docker (System signal forwarding, Graceful Shutdown). It enables the no-daemon mode and outputs applications logs to stdout/stderr by default. It also allows you to configure keymetrics bucket keys, to choose the log output format, and allows auto-exit on failure.

>>> pm2-docker -h

  Usage: pm2-docker start <app>

  Commands:

    start [options] <file|json_file>  start json_file or application

  Options:

    -h, --help             output usage information
    -V, --version          output the version number
    --raw                  raw log output
    --json                 output logs in json format
    --format               output logs formated like key=val
    --secret [key]         keymetrics secret key
    --public [key]         keymetrics public key
    --machine-name [name]  keymetrics machine name
    --auto-exit            exit if all processes are errored/stopped or 0 apps launched

To benefit from every PM2 options regarding process management, we recommend that you declare options in an ecosystem.json file and then start it with pm2-docker:

pm2-docker start ecosystem.json  

Last but not least, we created an official lightweight Docker image based on Linux Alpine using pm2-docker, check this out here: pm2-docker-alpine

pm2 install <transpiler>

Now PM2 support these 3 transpilers: Typescript, Livescript and Coffeescript. Before v2, only Coffeescript was integrated to PM2. Now you can enable the transpiler you prefer by running pm2 install <transpiler>.

For example to give pm2 the ability to run Typescript projects just do:

$ pm2 install typescript

You are now able to run any typescript project without configuration.

$ pm2 start script.ts

It supports both fork mode and cluster mode:

For Coffeescript:

$ pm2 install coffeescript
$ pm2 start app.coffee

For Livescript:

$ pm2 install livescript
$ pm2 start app.ls

Alternatively you can use the pm2 development command to run these apps with watch and restart feature:

$ rundev app.ts

Windows support

We just welcomed a new developer at Keymetrics, taking care of our PM2 Windows Users. Numerous bugs have been resolved and some more will be fixed in the following weeks (popup in cluster mode). Stay tuned! More progress are underway to make PM2 a first-class Windows citizen.

Blazing fast

Installation time has been boosted by freezing the dependency list via a npm-shrinkwrap making installation 2 times faster. Once we battle test shrinkpack we may plan to integrate it in a next release to reduce install time even more.

Updating PM2 is now also faster and Keymetrics linking has been delayed at the end of process revival.

Growth of Module System

The Module System has been shipped into PM2 one year ago and we got a lot of feedbacks from our users to make it better. Most of the Module System issues have been fixed.

Improvements:

  • New documentation
  • Raw NPM interactive output at installation
  • Optional and configurable module related informations display on installation
  • Better error messages

Better PM2 API

This is where most of the enhancements are coming from. Building a better API required to refactor some parts of code on PM2 client side.

While being 100% backward compatible with previous PM2 versions, the new API has been improved in:

  • Auto connection (no need to call .connect() anymore)
  • Multiple PM2 capabilities on a single server
  • Every Errors are Javascript Error objects
  • cwd, daemon mode, pm2_home configurable by instanciating a custom PM2

Now you can start an application as easy as this:

const pm2 = require('pm2');

pm2.start('app.js');  

Or:

const PM2 = require('pm2');

var pm2 = new PM2.custom({  
  pm2_home : '/tmp/somewhere'
});

pm2.start({  
  script   : 'app.js',
  name     : 'my-api',
  instances: 4 
}, (err, procs) => {
  console.log('%d processes started', procs.length);
  pm2.disconnect();
})

Refer to the API Documentation for further informations.

Specialized CLI for development

For development mode, the automatically installed rundev CLI allows to start a process file in a separated pm2 process and restart them on file change. Seamlessly this command will output logs of each app into the current terminal.

By installing typescript or any other transpilers, it will automatically make this command able to transpile any file.

Example:

Command line usage:

>>> rundev start -h

  Usage: start [options] <file|json_file>

  start target config file/script in development mode

  Options:

    -h, --help         output usage information
    --raw              raw log output
    --timestamp        print timestamp
    --ignore [files]   files to ignore while watching
    --post-exec [cmd]  execute extra command after change detected
    --silent-exec      do not output result of post command
    --test-mode        debug mode for test suit
    --env [name]       select env_[name] env variables in process config file
    --auto-exit        exit if all processes are errored/stopped or 0 apps launched

Keymetrics agent

The Keymetrics agent has been greatly improved! It takes less memory, less CPU, uses the new PM2 API, has a better Watchdog system, a better stability, smarter auto-reconnection and better multi-retries connection initialization.

It's working like a charm, from tiny computers like the Raspberry PI 3 to large servers.

Enhancements

  • No more external git repository, replaced with a tgz
  • pm2 start npm -- start now auto exits
  • Better code structure (CLI.js splitted into lib/API/)
  • --skip-env option to avoid env refresh on restart/reload
  • --listen-timeout option for reload and gracefulReload
  • pm2 ls --watch to refresh process list
  • pm2 inception (calling pm2 inside pm2) fully supported
  • Modules upgraded to latest majors available at this time

For detailed CHANGELOG.md check out the official PM2 repository here

Upgrading PM2

To benefits from those enhancements, just upgrade your PM2:

# Install the latest PM2
$ npm install pm2 -g 
# Update PM2 and keep the process list
$ pm2 update

Updating PM2 is straightforward!