Skip to content

sholladay/app-ready

Repository files navigation

app-ready Build status for App Ready

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.

Why?

  • Allows any parent process to track async startup.
  • Avoids filesystem polling and "ready files".
  • Helps you do easy zero downtime deployments.

Install

npm install app-ready --save

Usage

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();

Use with PM2

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.

API

appReady()

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.

Related

Contributing

See our contributing guidelines for more details.

  1. Fork it.
  2. Make a feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request.

License

MPL-2.0 © Seth Holladay

Go make something, dang it.