无需停机重载

As opposed to restart, which kills and restarts the process, reload achieves a 0-second-downtime reload.

Warning This feature only works for apps in cluster_mode, that uses HTTP/HTTPS/Socket connections.

To reload an app:

$ pm2 reload api

If the reload system hasn't managed to reload your app, a timeout will simply kill the process and will restart it.

Graceful reload

Sometimes you can experience a very long reload, or a reload that doesn't work (fallback to restart).

It means that your app still has open connections on exit.

To work around this problem you have to use the graceful reload. Graceful reload is a mechanism that will send a shutdown message to your process before reloading it. You can control the time that the app has to shutdown via the PM2_GRACEFUL_TIMEOUT environment variable.

Example:

process.on('message', function(msg) {
  if (msg == 'shutdown') {
    // Your process is going to be reloaded
    // You have to close all database/socket.io/* connections

    console.log('Closing all connections...');

    // You will have 4000ms to close all connections before
    // the reload mechanism will try to do its job

    setTimeout(function() {
      console.log('Finished closing connections');
      // This timeout means that all connections have been closed
      // Now we can exit to let the reload mechanism do its job
      process.exit(0);
    }, 1500);
  }
});

Then use the command:

$ pm2 gracefulReload [all|name]

When PM2 starts a new process to replace an old one, it will wait for the new process to begin listening to a connection or a timeout before sending the shutdown message to the old one. You can define the timeout value with the PM2_GRACEFUL_LISTEN_TIMEOUT environamente variable. I f a script does not need to listen to a connection, it can manually tell PM2 that the process has started up by calling process.send('online').