Implement graceful shutdown procedures to handle SIGTERM signals, drain connections, complete in-flight requests, and clean up resources properly. Use when deploying containerized applications, handling server restarts, or ensuring zero-downtime deployments.
3f5182cImplement proper shutdown procedures to ensure all requests are completed, connections are closed, and resources are released before process termination.
Minimal working example:
import express from "express";
import http from "http";
class GracefulShutdownServer {
private app: express.Application;
private server: http.Server;
private isShuttingDown = false;
private activeConnections = new Set<any>();
private shutdownTimeout = 30000; // 30 seconds
constructor() {
this.app = express();
this.server = http.createServer(this.app);
this.setupMiddleware();
this.setupRoutes();
this.setupShutdownHandlers();
}
private setupMiddleware(): void {
// Track active connections
this.app.use((req, res, next) => {
if (this.isShuttingDown) {
res.set("Connection", "close");
return res.status(503).json({
error: "Server is shutting down",
// ... (see reference guides for full implementation)
Detailed implementations in the references/ directory:
| Guide | Contents | |---|---| | Express.js Graceful Shutdown | Express.js Graceful Shutdown | | Kubernetes-Aware Shutdown | Kubernetes-Aware Shutdown | | Worker Process Shutdown | Worker Process Shutdown | | Database Connection Pool Shutdown | Database Connection Pool Shutdown | | PM2 Graceful Shutdown | PM2 Graceful Shutdown | | Python/Flask Graceful Shutdown | Python/Flask Graceful Shutdown |
Copy a source-pinned command for your client. You run it yourself.
Destination: .claude/skills/graceful-shutdown · pinned to the source commit
git clone https://github.com/aj-geddes/useful-ai-prompts.git
cd useful-ai-prompts
git checkout 3f5182cfd739fc113f4af5244a1cf342ad7f7911
mkdir -p ".claude/skills/graceful-shutdown"
cp -r "skills/graceful-shutdown" ".claude/skills/graceful-shutdown"Review the source before running. This copies files into your project; it is not a one-click install and does not verify runtime safety.
Scanner static-checks@0.1.0 · commit 3f5182cfd739. Static checks cannot prove runtime safety – review the source and the exact diff before installing. How checks work.
No static rules matched. This is not a safety guarantee.