Skip to content

Instantly share code, notes, and snippets.

@robcmills
Last active November 29, 2017 20:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robcmills/eea133f3bc2dae6106c2c109327869d2 to your computer and use it in GitHub Desktop.
Save robcmills/eea133f3bc2dae6106c2c109327869d2 to your computer and use it in GitHub Desktop.
PM2 @ BC

PM2

Overview

In production Heroku uses Procfile. pm2 roughly emulates this.

Trace npm start -> node pm2/start.js

Basically pm2/start.js is just a node script that uses pm2's node api to start processes according to the dynamically imported config.

start-logs.js It starts a child process that runs pm2 logs, parses, formats and colorizes its output. http://pm2.keymetrics.io/docs/usage/log-management/

watch-and-restart.js It sets up filesystem watching and triggers process restarts.

  • watch
    • can be true, false, or an array of file paths
    • if false, disables filesystem watching and process restarting
    • if true, watches filesystem for changes and restarts process
    • if an array is provided, recursively watches provided paths for file changes and restarts process
  • ignore_watch
    • array of relative paths, strings, or regexes to ignore from file changes observed by watch

Configs

const prefix = process.argv[2] || 'start'
const config = require(`./configs/${prefix}.config.js`)

start.config.js

pm2/configs/defaults.js

http://pm2.keymetrics.io/docs/usage/application-declaration/

npm run start:config my.custom

Scripts

What npm run start:* configs are available and how they differ from npm start

When you should make changes to default configs

You should change default config if:

You make a code change and the process running that code doesn't restart

and conversely

You make a code change and a process not running that code restarts

Code that is not running should not restart processes, i.e. scripts, migrations, etc.

Examples:

npx pm2

You can use npx to run pm2 cli commands:

  • npx pm2 ls show all processes
  • npx pm2 logs worker show output from only worker process
  • npx pm2 monit cool pm2 dashboard

http://pm2.keymetrics.io/docs/usage/quick-start/

How to write and run your own config (advanced)

Example:

  • web.profile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment