puma-plugin-delayed_stop 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +9 -0
- data/LICENSE +21 -0
- data/README.md +76 -0
- data/lib/puma/plugin/delayed_stop/version.rb +5 -0
- data/lib/puma/plugin/delayed_stop.rb +45 -0
- data/lib/puma-plugin-delayed_stop.rb +5 -0
- metadata +120 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 960c22d0477d4a3fc18be9e9cf3e0a1ba32ee622aabee83cf810abaec8746e6a
|
|
4
|
+
data.tar.gz: 2f2e4b5884ecc1854e8ddd473f569cf9bd4a0e0b3d6e1ea421dfe5f8e8f6bcb5
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: da26be51c235e68f1595f85bb6fca5ef42f71a01fcabe9e9fff44c620d4d9e3e380f027367d593a0a781fc50a7700c4b20f5dbc2a2de2b8b228163cd8f13fe80
|
|
7
|
+
data.tar.gz: 59b545e4b85684c5c69df706870c74d7c688dfd161750007db407bbf2eb6c96438804889d1ea05d0ff7331cb5706f47814c047248476b7a1be377133c8ae6f94
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.1.0 (2026/03/19)
|
|
4
|
+
|
|
5
|
+
- Initial release
|
|
6
|
+
- Supports Puma 5, 6, and 7
|
|
7
|
+
- Configurable signal via `PUMA_DELAYED_STOP_SIGNAL` (default: `QUIT`)
|
|
8
|
+
- Configurable drain period via `PUMA_DELAYED_STOP_DRAIN_SECONDS` (default: `5`)
|
|
9
|
+
- Validates that the configured signal does not conflict with Puma's built-in signal handlers
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright © 2026 The Regents of the University of California
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# puma-plugin-delayed_stop
|
|
2
|
+
|
|
3
|
+
A [Puma](https://puma.io) plugin that delays shutdown after receiving a signal, giving container orchestrators (Kubernetes, Docker Swarm, ECS, etc.) time to remove the instance from load balancing before connections are closed.
|
|
4
|
+
|
|
5
|
+
## The problem
|
|
6
|
+
|
|
7
|
+
When Puma receives `SIGTERM`, it begins shutting down immediately. In orchestrated environments, the termination signal often arrives *before* the orchestrator has finished removing the container from its service mesh or load balancer. Requests routed to the container during this window are dropped.
|
|
8
|
+
|
|
9
|
+
## The solution
|
|
10
|
+
|
|
11
|
+
This plugin intercepts a configurable signal (default: `SIGQUIT`) and waits a configurable number of seconds before telling Puma to stop. This gives the orchestrator time to update its routing tables.
|
|
12
|
+
|
|
13
|
+
A typical Kubernetes setup would configure the pod's `preStop` hook to send `SIGQUIT` before the kubelet sends `SIGTERM`:
|
|
14
|
+
|
|
15
|
+
```yaml
|
|
16
|
+
lifecycle:
|
|
17
|
+
preStop:
|
|
18
|
+
exec:
|
|
19
|
+
command: ["kill", "-QUIT", "1"]
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
In Docker Swarm, configure `stop_signal` to send `SIGQUIT` first and set `stop_grace_period` long enough to cover both the drain and Puma's graceful shutdown:
|
|
23
|
+
|
|
24
|
+
```yaml
|
|
25
|
+
services:
|
|
26
|
+
web:
|
|
27
|
+
image: myapp:latest
|
|
28
|
+
stop_signal: SIGQUIT
|
|
29
|
+
stop_grace_period: 30s
|
|
30
|
+
environment:
|
|
31
|
+
PUMA_DELAYED_STOP_DRAIN_SECONDS: "5"
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Swarm sends `stop_signal` when removing a task, then waits up to `stop_grace_period` before sending `SIGKILL`. The plugin sleeps through the drain period while Swarm updates its routing mesh, then tells Puma to shut down gracefully with the remaining time.
|
|
35
|
+
|
|
36
|
+
## Installation
|
|
37
|
+
|
|
38
|
+
Add to your Gemfile:
|
|
39
|
+
|
|
40
|
+
```ruby
|
|
41
|
+
gem "puma-plugin-delayed_stop"
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Then in your `config/puma.rb`:
|
|
45
|
+
|
|
46
|
+
```ruby
|
|
47
|
+
plugin :delayed_stop
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Configuration
|
|
51
|
+
|
|
52
|
+
Configuration is via environment variables:
|
|
53
|
+
|
|
54
|
+
| Variable | Default | Description |
|
|
55
|
+
|---|---|---|
|
|
56
|
+
| `PUMA_DELAYED_STOP_SIGNAL` | `QUIT` | Signal name (without `SIG` prefix) that triggers the delayed stop |
|
|
57
|
+
| `PUMA_DELAYED_STOP_DRAIN_SECONDS` | `5` | Seconds to wait before telling Puma to stop |
|
|
58
|
+
|
|
59
|
+
**Warning:** Do not set `PUMA_DELAYED_STOP_SIGNAL` to a signal that Puma already handles (`TERM`, `INT`, `HUP`, `USR1`, `USR2`). Puma registers its own handlers for these signals *after* plugins start, so the plugin's handler will be silently overwritten. The default (`QUIT`) is safe because Puma does not trap it. See [Puma's signal handling documentation](https://github.com/puma/puma/blob/master/docs/signals.md) for the full list of reserved signals.
|
|
60
|
+
|
|
61
|
+
## How it works
|
|
62
|
+
|
|
63
|
+
1. On startup, the plugin registers a signal handler for the configured signal.
|
|
64
|
+
2. When the signal is received, the handler sleeps for the configured drain period.
|
|
65
|
+
3. After sleeping, it calls `launcher.stop`, which initiates Puma's normal graceful shutdown.
|
|
66
|
+
|
|
67
|
+
## Development
|
|
68
|
+
|
|
69
|
+
```bash
|
|
70
|
+
bundle install
|
|
71
|
+
bundle exec rspec
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## License
|
|
75
|
+
|
|
76
|
+
[MIT](LICENSE)
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Graceful termination in Swarm/Kubernetes
|
|
4
|
+
#
|
|
5
|
+
# Puma immediately closes its incoming socket upon receipt of SIGTERM.
|
|
6
|
+
# When running in an orchestrator like Kubernetes or Docker Swarm, this
|
|
7
|
+
# can happen before the orchestrator has stopped routing requests to
|
|
8
|
+
# the terminated container, causing dropped requests. This plugin adds
|
|
9
|
+
# a configurable delay so the orchestrator has time to remove the
|
|
10
|
+
# terminated container before Puma exits as usual and stops serving requests.
|
|
11
|
+
|
|
12
|
+
Puma::Plugin.create do
|
|
13
|
+
# Signals Puma registers handlers for. It overwrites plugin handlers for these
|
|
14
|
+
# after plugins start, so using one here would silently break the delayed stop.
|
|
15
|
+
# INFO (BSD/macOS) and PWR (Linux) were added in Puma 7.
|
|
16
|
+
PUMA_SIGNALS = %w[TERM INT USR1 USR2 HUP INFO PWR].freeze
|
|
17
|
+
|
|
18
|
+
# POSIX signal that triggers a delayed stop.
|
|
19
|
+
# Defaults to QUIT so as not to interfere with Puma's default signal handling.
|
|
20
|
+
# Accepts both "QUIT" and "SIGQUIT" forms; the SIG prefix is stripped.
|
|
21
|
+
STOP_SIGNAL = ENV.fetch("PUMA_DELAYED_STOP_SIGNAL", "QUIT").sub(/\ASIG/i, "")
|
|
22
|
+
|
|
23
|
+
# Time to wait in seconds before stopping.
|
|
24
|
+
DRAIN_SECONDS = Integer(ENV.fetch("PUMA_DELAYED_STOP_DRAIN_SECONDS", "5"))
|
|
25
|
+
|
|
26
|
+
def start(launcher)
|
|
27
|
+
if PUMA_SIGNALS.include?(STOP_SIGNAL)
|
|
28
|
+
raise ArgumentError,
|
|
29
|
+
"[delayed_stop] PUMA_DELAYED_STOP_SIGNAL=#{STOP_SIGNAL} conflicts with " \
|
|
30
|
+
"Puma's built-in SIG#{STOP_SIGNAL} handler. Use a signal Puma does not " \
|
|
31
|
+
"reserve (e.g. QUIT). Puma reserves: #{PUMA_SIGNALS.map { |s| "SIG#{s}" }.join(', ')}"
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Puma 6 renamed `events` to `log_writer` for logging.
|
|
35
|
+
logger = launcher.respond_to?(:log_writer) ? launcher.log_writer : launcher.events
|
|
36
|
+
|
|
37
|
+
Signal.trap(STOP_SIGNAL) do
|
|
38
|
+
Thread.new do
|
|
39
|
+
logger.log("[delayed_stop] Received SIG#{STOP_SIGNAL}, sleeping #{DRAIN_SECONDS}s before stopping")
|
|
40
|
+
sleep(DRAIN_SECONDS)
|
|
41
|
+
launcher.stop
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: puma-plugin-delayed_stop
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Dan Schmidt
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2026-03-20 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: puma
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - ">="
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '5.0'
|
|
20
|
+
- - "<"
|
|
21
|
+
- !ruby/object:Gem::Version
|
|
22
|
+
version: '8'
|
|
23
|
+
type: :runtime
|
|
24
|
+
prerelease: false
|
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
26
|
+
requirements:
|
|
27
|
+
- - ">="
|
|
28
|
+
- !ruby/object:Gem::Version
|
|
29
|
+
version: '5.0'
|
|
30
|
+
- - "<"
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '8'
|
|
33
|
+
- !ruby/object:Gem::Dependency
|
|
34
|
+
name: bundler
|
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - ">="
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '2.0'
|
|
40
|
+
type: :development
|
|
41
|
+
prerelease: false
|
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - ">="
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: '2.0'
|
|
47
|
+
- !ruby/object:Gem::Dependency
|
|
48
|
+
name: rake
|
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
|
50
|
+
requirements:
|
|
51
|
+
- - "~>"
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: '13.0'
|
|
54
|
+
type: :development
|
|
55
|
+
prerelease: false
|
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
57
|
+
requirements:
|
|
58
|
+
- - "~>"
|
|
59
|
+
- !ruby/object:Gem::Version
|
|
60
|
+
version: '13.0'
|
|
61
|
+
- !ruby/object:Gem::Dependency
|
|
62
|
+
name: rspec
|
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
|
64
|
+
requirements:
|
|
65
|
+
- - "~>"
|
|
66
|
+
- !ruby/object:Gem::Version
|
|
67
|
+
version: '3.0'
|
|
68
|
+
type: :development
|
|
69
|
+
prerelease: false
|
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
71
|
+
requirements:
|
|
72
|
+
- - "~>"
|
|
73
|
+
- !ruby/object:Gem::Version
|
|
74
|
+
version: '3.0'
|
|
75
|
+
description: |
|
|
76
|
+
A Puma plugin that intercepts a configurable signal (default: SIGQUIT) and
|
|
77
|
+
waits a configurable number of seconds before telling Puma to stop. This
|
|
78
|
+
gives orchestrators like Kubernetes and Docker Swarm time to remove the
|
|
79
|
+
container from load balancing before connections are closed.
|
|
80
|
+
email:
|
|
81
|
+
- danschmidt5189@berkeley.edu
|
|
82
|
+
executables: []
|
|
83
|
+
extensions: []
|
|
84
|
+
extra_rdoc_files: []
|
|
85
|
+
files:
|
|
86
|
+
- CHANGELOG.md
|
|
87
|
+
- LICENSE
|
|
88
|
+
- README.md
|
|
89
|
+
- lib/puma-plugin-delayed_stop.rb
|
|
90
|
+
- lib/puma/plugin/delayed_stop.rb
|
|
91
|
+
- lib/puma/plugin/delayed_stop/version.rb
|
|
92
|
+
homepage: https://github.com/BerkeleyLibrary/puma-plugin-delayed_stop
|
|
93
|
+
licenses:
|
|
94
|
+
- MIT
|
|
95
|
+
metadata:
|
|
96
|
+
allowed_push_host: https://rubygems.org
|
|
97
|
+
homepage_uri: https://github.com/BerkeleyLibrary/puma-plugin-delayed_stop
|
|
98
|
+
source_code_uri: https://github.com/BerkeleyLibrary/puma-plugin-delayed_stop
|
|
99
|
+
changelog_uri: https://github.com/BerkeleyLibrary/puma-plugin-delayed_stop/blob/main/CHANGELOG.md
|
|
100
|
+
rubygems_mfa_required: 'true'
|
|
101
|
+
post_install_message:
|
|
102
|
+
rdoc_options: []
|
|
103
|
+
require_paths:
|
|
104
|
+
- lib
|
|
105
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
106
|
+
requirements:
|
|
107
|
+
- - ">="
|
|
108
|
+
- !ruby/object:Gem::Version
|
|
109
|
+
version: '2.7'
|
|
110
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
111
|
+
requirements:
|
|
112
|
+
- - ">="
|
|
113
|
+
- !ruby/object:Gem::Version
|
|
114
|
+
version: '0'
|
|
115
|
+
requirements: []
|
|
116
|
+
rubygems_version: 3.5.22
|
|
117
|
+
signing_key:
|
|
118
|
+
specification_version: 4
|
|
119
|
+
summary: Puma plugin that delays shutdown for graceful container draining
|
|
120
|
+
test_files: []
|