Production deployment
Eighteenth and final chapter of the "Fastify with TypeScript" series. Let's close the loop: graceful shutdown, health checks for your orchestrator, a production-ready Dockerfile, and a look back at the principles the whole series has been following.
Graceful shutdown
When Kubernetes (or docker stop , or a Ctrl-C) sends SIGTERM , you want to stop accepting new connections, let the requests in flight finish, close the database pool, and only then exit. close-with-grace wires the signals to app.close() , and forcibly exits if that takes too long:
npm install close-with-grace
Enter fullscreen mode
Exit fullscreen mode
Replace src/server.ts with:
// src/server.ts import closeWithGrace from 'close-with-grace' ; import { buildApp } from './app.ts' ;
const app = buildApp ( ) ;
closeWithGrace ( { delay : 10_000 } , async ( { signal , err } ) => { if ( err ) { app . log . error ( { err } , 'Shutting down due to an error' ) ; } else { app . log .…