Giter VIP home page Giter VIP logo

app-ready's Introduction

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.

app-ready's People

Contributors

sholladay avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.