pm2 restart strategies

use PM2 launch application When, the application will restart automatically when it exits automatically, the event loop is empty (node.js), or the application crashes. However, you can also configure additional restart policies, such as:

  • Restart the application using a scheduled task
  • Restart the application after the file changes
  • Restart when application reaches memory threshold
  • Delayed start and automatic restart
  • By default, automatic restart is disabled on crash or exit (applications always restart using PM2)
  • Automatically restart applications at specific exponential growth times

Restart the application using a scheduled task

Use the following command to set the task of scheduled restart

$ pm2 start server.js --cron-restart="0 0 * * *"
# Or set a scheduled task when restarting
$ pm2 restart app --cron-restart="0 0 * * *"

If yes configuration file If so, use cron_restart attribute:

server.config.js

module.exports = {
  apps : [{
    name: 'server',
    script: 'server.js',
    instances: 1,
    cron_restart: '0 0 * * *',
    env: {
      NODE_ENV: 'development'
    },
    env_production: {
      NODE_ENV: 'production'
    }
  }]
}

Automatically restart the application after file changes

When the files in the current directory or its subdirectory are modified, PM2 can automatically restart your application:

Use the following command to start the application by specifying the option -- watch later

$ pm2 start server.js --watch

Let's illustrate this situation through gif dynamic diagram

Note: if the application starts using the – watch option, stopping the application does not prevent it from restarting when the file changes. To completely disable the watch function, execute the following command:

$ pm2 stop app --watch 

Or use the following command to switch the watch option when the application restarts.

$ pm2 restart app --watch 

In the configuration file, use the watch: true attribute

module.exports = {
  script: "server.js",
  watch: true
}

We can also specify which folder to listen to in the configuration file, and automatically restart the application when its contents are modified. You can also specify that some folders are ignored, and no restart will be triggered regardless of how their contents change

module.exports = {
  script: "server.js",
  // Specify the folder to listen to
  watch: ["server", "client"],
  // Specify delay time
  watch_delay: 1000,
  // Specify the folder to ignore
  ignore_watch : ["node_modules", "client/img"],
}

Restart when application reaches memory threshold

PM2 allows applications to be reloaded according to the memory limit (if they are not in the cluster, they will automatically fall back and restart) / please note that PM2 internal working program (check memory) starts every 30 seconds, so it may take a moment after reaching the memory threshold, and the process will restart automatically.

Use the -- Max memory restart option to specify the memory threshold.

$ pm2 start server.js --max-memory-restart 300M

Use Max in configuration file_ memory_ Restart attribute.

server.config.js

module.exports = {
  script: 'server.js',
  max_memory_restart: '300M'
}

Note: the units can be K(ilobyte) (e.g. 512K), m (egabyte) (e.g. 128M), G (igabyte) (e.g. 1G).

Delayed restart

Use the Restart Delay policy to set the delay between automatic restarts:

$ pm2 start server.js --restart-delay=3000

In the configuration file, use restart_ The delay property sets delayed restart.

server.config.js

module.exports = {
  script: 'server.js',
  restart_delay: 3000
}

Prohibit automatic restart

This is useful if we want to run the script once and do not want the process manager to restart our script when the script finishes running.

$ pm2 start server.js --no-autorestart

In the configuration file, use the autorestart: false attribute to disable automatic restart.

server.config.js

module.exports = {
  script: 'server.js',
  autorestart: false
}

Specifies an exit code that does not restart automatically

Sometimes we may want the application to restart automatically in the event of a failure (that is, a non-zero exit code) rather than the process manager to restart it when it closes properly (that is, the exit code is equal to 0).

In this case, PM2 can still be used well and stop_ exit_ The codes option is set to the exit code that should skip automatic restart:

$ pm2 start server.js --stop-exit-codes 0

In the configuration file, use stop_ exit_ The codes property sets the exit code without automatic restart.

server.config.js

module.exports = [{
  script: 'server.js',
  stop_exit_codes: [0]
}]

Exponential backoff restart delay

A new restart mode has been implemented on PM2 Runtime to restart our applications in a more intelligent way. When an exception occurs (such as database shutdown), instead of restarting the application crazily, exponential backoff restart will increase the time between restarts and reduce the pressure on our database or external providers... Very easy to use:

Set this function on the terminal command line with the option -- exp backoff restart delay

$ pm2 start app.js --exp-backoff-restart-delay=100

In the configuration file, use exp_backoff_restart_delay property.

server.config.js

module.exports = {
  script: 'server.js',
  exp_backoff_restart_delay: 100
}

When the application crashes unexpectedly and the option -- exp backoff restart delay is activated, we will be able to see the new application state waiting restart.

By running pm2 logs , we will also see an increase in restart latency:

PM2      | App [throw:0] will restart in 100ms
PM2      | App [throw:0] exited with code [1] via signal [SIGINT]
PM2      | App [throw:0] will restart in 150ms
PM2      | App [throw:0] exited with code [1] via signal [SIGINT]
PM2      | App [throw:0] will restart in 225ms

As you can see, the restart delay between restarts will increase exponentially until the maximum value between restarts is 15000 milliseconds.

When the application returns to stable mode (uptime does not exceed 30 seconds), the restart delay will automatically reset to 0 milliseconds.

For more information about pm2, refer to pm2 tutorial

Tags: Front-end Programming

Posted on Mon, 29 Nov 2021 17:52:29 -0500 by The_Stranger