Graceful Shutdown

Graceful Shutdown

Guides an AI coding agent to implement graceful shutdown that handles SIGTERM, drains connections and finishes in-flight requests for zero-downtime deployments.

Skill files9 files

references/

scripts/

templates/

SKILL.md104 lines
1
---
2
name: graceful-shutdown
3
description: >
4
Implement graceful shutdown procedures to handle SIGTERM signals, drain
5
connections, complete in-flight requests, and clean up resources properly. Use
6
when deploying containerized applications, handling server restarts, or
7
ensuring zero-downtime deployments.
8
---
9
10
# Graceful Shutdown
11
12
## Table of Contents
13
14
- [Overview](#overview)
15
- [When to Use](#when-to-use)
16
- [Quick Start](#quick-start)
17
- [Reference Guides](#reference-guides)
18
- [Best Practices](#best-practices)
19
20
## Overview
21
22
Implement proper shutdown procedures to ensure all requests are completed, connections are closed, and resources are released before process termination.
23
24
## When to Use
25
26
- Kubernetes/Docker deployments
27
- Rolling updates and deployments
28
- Server restarts
29
- Load balancer drain periods
30
- Zero-downtime deployments
31
- Process managers (PM2, systemd)
32
- Long-running background jobs
33
- Database connection cleanup
34
35
## Quick Start
36
37
Minimal working example:
38
39
```typescript
40
import express from "express";
41
import http from "http";
42
43
class GracefulShutdownServer {
44
private app: express.Application;
45
private server: http.Server;
46
private isShuttingDown = false;
47
private activeConnections = new Set<any>();
48
private shutdownTimeout = 30000; // 30 seconds
49
50
constructor() {
51
this.app = express();
52
this.server = http.createServer(this.app);
53
this.setupMiddleware();
54
this.setupRoutes();
55
this.setupShutdownHandlers();
56
}
57
58
private setupMiddleware(): void {
59
// Track active connections
60
this.app.use((req, res, next) => {
61
if (this.isShuttingDown) {
62
res.set("Connection", "close");
63
return res.status(503).json({
64
error: "Server is shutting down",
65
// ... (see reference guides for full implementation)
66
```
67
68
## Reference Guides
69
70
Detailed implementations in the `references/` directory:
71
72
| Guide | Contents |
73
|---|---|
74
| [Express.js Graceful Shutdown](references/expressjs-graceful-shutdown.md) | Express.js Graceful Shutdown |
75
| [Kubernetes-Aware Shutdown](references/kubernetes-aware-shutdown.md) | Kubernetes-Aware Shutdown |
76
| [Worker Process Shutdown](references/worker-process-shutdown.md) | Worker Process Shutdown |
77
| [Database Connection Pool Shutdown](references/database-connection-pool-shutdown.md) | Database Connection Pool Shutdown |
78
| [PM2 Graceful Shutdown](references/pm2-graceful-shutdown.md) | PM2 Graceful Shutdown |
79
| [Python/Flask Graceful Shutdown](references/pythonflask-graceful-shutdown.md) | Python/Flask Graceful Shutdown |
80
81
## Best Practices
82
83
### ✅ DO
84
85
- Handle SIGTERM and SIGINT signals
86
- Stop accepting new requests immediately
87
- Wait for in-flight requests to complete
88
- Set reasonable shutdown timeouts
89
- Close database connections properly
90
- Flush logs and metrics
91
- Fail health checks during shutdown
92
- Test shutdown procedures
93
- Log shutdown progress
94
- Use graceful shutdown in containers
95
96
### ❌ DON'T
97
98
- Ignore shutdown signals
99
- Force kill processes without cleanup
100
- Set unreasonably long timeouts
101
- Skip resource cleanup
102
- Forget to close connections
103
- Block shutdown indefinitely
104

© AJ Geddes · MIT · source · posted by @OpenSourceBuffGuy on promptparty.com

@OpenSourceBuffGuy

I find openly licensed prompts and post them here.

0 followers · 14,621 prompts

More by @OpenSourceBuffGuy

See all
Sea turtle carrying floating film studio island prompt image

Sea turtle carrying floating film studio island

Draws a giant sea turtle carrying a floating island film studio through a starry sky, where fairies polish rough film strips into golden film.

image-prompt, nano-banana, 3d-render, cartoon, fantasy-world, creatures, animals, whimsical, dreamy, cinematic, dramatic-lighting, vibrant-colors, bokeh, miniature, widescreen, night-scene, illustration, concept-art, surreal, storytelling, clay-3d

Desert buggy escapes drones past industrial outpost prompt image

Desert buggy escapes drones past industrial outpost

Builds a 15-second desert chase video where a racer in an armored buggy escapes drones past an industrial outpost, using five reference images with set jobs.

video-prompt, hailuo, action, multi-shot, timed-beats, camera-movement, reference-image, character-consistency, negative-prompt, cinematic, sci-fi, cars, landscape, aerial-view, tracking-shot, photorealistic, sound-effects, structured-output, long-form, widescreen

1 view
Cozy Pixel Art Farm Island Village prompt image

Cozy Pixel Art Farm Island Village

Draws a cozy 16-bit pixel-art farming village across six small islands joined by bridges, with exact counts for buildings, animals and crops.

image-prompt, gpt-image, pixel-art, isometric, game-assets, concept-art, maps, worldbuilding, cozy, vibrant-colors, ocean, landscape, wide-shot, widescreen, game-development, video-games, structured-output, whimsical, summer, high-angle, aerial-view, animals, negative-prompt, cityscape

1 view

Reviews

Loading reviews…

Discussion

Loading comments…