Support graceful start in your app
Signal that your app is ready for use, so a process manager can bring it online at the right time.
- Allows any parent process to track async startup.
- Avoids filesystem polling and "ready files".
- Helps you do easy zero downtime deployments.
npm install app-ready --save
Get it into your program.
const appReady = require('app-ready');
Tell the parent process that we are ready.
await database.connect();
await server.listen();
appReady();
To achieve zero downtime deployments, PM2 needs to know when your app is ready to use. It tries to be smart and automatically waits for you to call server.listen()
. However, there are some limitations with this (for more details, see pm2#2573).
- If you never actually call
server.listen()
, PM2 won't know what to do and will eventually time out and forcefully restart your app in an ettempt to get it to boot correctly. - If you do call
server.listen()
, but your app is not 100% ready at that point, then your app will receive traffic at the wrong time and it might blow up.
These problems can be fixed by being more explicit. Let's use graceful start and tell PM2 to wait for our signal!
$ pm2 start app.js --wait-ready --listen-timeout 3000
Above, --wait-ready
tells PM2 to ignore server.listen()
and wait for the more explicit message that appReady()
sends for you. This allows you to more precisely control when PM2 begins sending traffic to your app.
Just in case something goes wrong, --listen-timeout
tells PM2 how long it should wait for the ready message before considering this a failed start, in which case it will restart the app and try again.
To support graceful stop, see handle-quit.
If the process is a child of another Node.js process, sends a message
event to the parent with a value of ready
. Otherwise, does nothing.
- handle-quit - Support graceful stop in your app
See our contributing guidelines for more details.
- Fork it.
- Make a feature branch:
git checkout -b my-new-feature
- Commit your changes:
git commit -am 'Add some feature'
- Push to the branch:
git push origin my-new-feature
- Submit a pull request.
Go make something, dang it.