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 necessary, use   cron_restart  

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 by specifying options later  -- watch   Open application

$ pm2 start server.js --watch

 

Let's illustrate this situation through gif dynamic diagram

 

Note: if the application uses  -- watch   Option to start, 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   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   -- max-memory-restart   Option specifies the memory threshold.

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

 

Used in configuration files   max_memory_restart   Properties.

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_delay   Property to set 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   autorestart: false   Property disables 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 will   stop_exit_codes   The 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_codes   Property to set the exit code without automatic restart.

server.config.js

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

 

Exponential backoff restart delay

stay   PM2 Runtime   A new restart mode is implemented on, which makes our applications restart 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:

Use options on the terminal command line  -- exp-backoff-restart-delay   Set this feature

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

 

In the configuration file, use   exp_backoff_restart_delay   Properties.

server.config.js

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

 

When the application crashes unexpectedly and activates the option -- exp backoff restart delay   When, 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

Posted on Mon, 29 Nov 2021 09:55:50 -0500 by Aaron_Escobar