Your application is serving requests constantly for your users. You and your team want to ship features and fixes as soon as they are ready, so you do continuous delivery. But what happens to your users who used your product at the time of the deployment? Chances are, the requests they have in progress are going to fail. This post helps you fix that.
When you deploy a new version of your application, the old must be replaced. The process manager you are using (no matter if it is Heroku, Kubernetes, supervisor or anything else) will first send a
SIGTERM
For your Node.js process you may add something like this:
process.on("SIGTERM", () => {logger.info("shutdown started");server.stop().then(closeMysqlConnection()).then(() => {logger.info("process is stopping");});});
Health checks of your applications are called by the load balancer of your application to let it know if the application instance is healthy, and can server traffic. If you are using Kubernetes, Kubernetes has two distinct health checks:
On how to set up health checks for Kubernetes, check out the official Configure Liveness and Readiness Probes docs.
terminus
terminus is an open-source project, which adds health checks and graceful shutdown to your applications - to save you from the boilerplate code you would add otherwise. You only have to provide the cleanup logic for graceful shutdowns, and the health check logic for health checks, all the rest is handled by terminus.
Let's take a look at an example:
const http = require("http");const terminus = require("@godaddy/terminus");const server = http.createServer((request, response) => {response.end("<html><body><h1>Hello, World!</h1></body></html>");});const PORT = process.env.PORT || 3000;function onSigterm() {console.log("server is starting cleanup");return Promise.all([// your clean logic, like closing database connections]);}terminus(server, {// healtcheck optionshealthChecks: {"/_health/liveness": livenessCheck,"/_health/readiness": readinessCheck,},// cleanup optionstimeout: 1000,onSigterm,logger,});server.listen(PORT);
The example above decorates your HTTP server with two endpoints,
/_health/liveness
/_health/readiness
200
500
The
onSigterm
timeout
I hope with the help of
terminus