Agent skill

graceful-shutdown

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.

Stars 151
Forks 20

Install this agent skill to your Project

npx add-skill https://github.com/aj-geddes/useful-ai-prompts/tree/main/skills/graceful-shutdown

SKILL.md

Graceful Shutdown

Table of Contents

  • Overview
  • When to Use
  • Quick Start
  • Reference Guides
  • Best Practices

Overview

Implement proper shutdown procedures to ensure all requests are completed, connections are closed, and resources are released before process termination.

When to Use

  • Kubernetes/Docker deployments
  • Rolling updates and deployments
  • Server restarts
  • Load balancer drain periods
  • Zero-downtime deployments
  • Process managers (PM2, systemd)
  • Long-running background jobs
  • Database connection cleanup

Quick Start

Minimal working example:

typescript
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)

Reference Guides

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

Best Practices

✅ DO

  • Handle SIGTERM and SIGINT signals
  • Stop accepting new requests immediately
  • Wait for in-flight requests to complete
  • Set reasonable shutdown timeouts
  • Close database connections properly
  • Flush logs and metrics
  • Fail health checks during shutdown
  • Test shutdown procedures
  • Log shutdown progress
  • Use graceful shutdown in containers

❌ DON'T

  • Ignore shutdown signals
  • Force kill processes without cleanup
  • Set unreasonably long timeouts
  • Skip resource cleanup
  • Forget to close connections
  • Block shutdown indefinitely

Expand your agent's capabilities with these related and highly-rated skills.

aj-geddes/useful-ai-prompts

websocket-implementation

Implement real-time bidirectional communication with WebSockets including connection management, message routing, and scaling. Use when building real-time features, chat systems, live notifications, or collaborative applications.

151 20
Explore
aj-geddes/useful-ai-prompts

refactor-legacy-code

Modernize and improve legacy codebases while maintaining functionality. Use when you need to refactor old code, reduce technical debt, modernize deprecated patterns, or improve code maintainability without breaking existing behavior.

151 20
Explore
aj-geddes/useful-ai-prompts

Sentiment Analysis

Classify text sentiment using NLP techniques, lexicon-based analysis, and machine learning for opinion mining, brand monitoring, and customer feedback analysis

151 20
Explore
aj-geddes/useful-ai-prompts

flask-api-development

Develop lightweight Flask APIs with routing, blueprints, database integration, authentication, and request/response handling. Use when building RESTful APIs, microservices, or lightweight web services with Flask.

151 20
Explore
aj-geddes/useful-ai-prompts

ML Model Explanation

Interpret machine learning models using SHAP, LIME, feature importance, partial dependence, and attention visualization for explainability

151 20
Explore
aj-geddes/useful-ai-prompts

Statistical Hypothesis Testing

Conduct statistical tests including t-tests, chi-square, ANOVA, and p-value analysis for statistical significance, hypothesis validation, and A/B testing

151 20
Explore

Didn't find tool you were looking for?

Be as detailed as possible for better results