puma-plugin-systemd 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/lib/puma/plugin/systemd.rb +109 -107
- metadata +15 -9
- metadata.gz.sig +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 331bad09b8a7f0e62675cd82edb25ac4154f062aa68c0a0d8281018ba276dd8b
|
4
|
+
data.tar.gz: 81e80990542efce424e76f4155a7bb4e416d63036163ac6b33aebbe88f20adc1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c38c6cc61c8695eaa7b28406899d95c75e1a65a4754f98a947948e87ea3329544c303defd99eba30263e896e3f2712524e2bec0730ce159cddf16216715540fc
|
7
|
+
data.tar.gz: d3991a2398c378bfe02eb49a6e813180998d609a14d929d48a69cda5f2ba5732f120909b08ff287a8a373dc3a33503e45f93a6e2f4a6515ccba505027dc8898f
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
data.tar.gz.sig
CHANGED
Binary file
|
data/lib/puma/plugin/systemd.rb
CHANGED
@@ -10,7 +10,115 @@ require "puma/plugin"
|
|
10
10
|
# you know when your system has *actually* started and is ready to take
|
11
11
|
# requests.
|
12
12
|
#
|
13
|
-
Puma::Plugin
|
13
|
+
class Puma::Plugin::Systemd
|
14
|
+
Puma::Plugins.register("systemd", self)
|
15
|
+
|
16
|
+
# Puma creates the plugin when encountering `plugin` in the config.
|
17
|
+
def initialize(loader)
|
18
|
+
# This is a Puma::PluginLoader
|
19
|
+
@loader = loader
|
20
|
+
end
|
21
|
+
|
22
|
+
# We can start doing something when we have a launcher:
|
23
|
+
def start(launcher)
|
24
|
+
@launcher = launcher
|
25
|
+
|
26
|
+
# Log relevant ENV in debug
|
27
|
+
@launcher.events.debug "systemd: NOTIFY_SOCKET=#{ENV["NOTIFY_SOCKET"].inspect}"
|
28
|
+
@launcher.events.debug "systemd: WATCHDOG_PID=#{ENV["WATCHDOG_PID"].inspect}"
|
29
|
+
@launcher.events.debug "systemd: WATCHDOG_USEC=#{ENV["WATCHDOG_USEC"].inspect}"
|
30
|
+
|
31
|
+
# Only install hooks if systemd is present, the systemd is booted by
|
32
|
+
# systemd, and systemd has asked us to notify it of events.
|
33
|
+
@systemd = Systemd.new
|
34
|
+
if @systemd.present? && @systemd.booted? && @systemd.notify?
|
35
|
+
@launcher.events.debug "systemd: detected running inside systemd, registering hooks"
|
36
|
+
register_hooks
|
37
|
+
else
|
38
|
+
@launcher.events.debug "systemd: not running within systemd, doing nothing"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
# Are we a single process worker, or do we have worker processes?
|
45
|
+
#
|
46
|
+
# Copied from puma, it's private:
|
47
|
+
# https://github.com/puma/puma/blob/v3.6.0/lib/puma/launcher.rb#L267-L269
|
48
|
+
#
|
49
|
+
def clustered?
|
50
|
+
(@launcher.options[:workers] || 0) > 0
|
51
|
+
end
|
52
|
+
|
53
|
+
def register_hooks
|
54
|
+
(@launcher.config.options[:on_restart] ||= []) << method(:restart)
|
55
|
+
@launcher.events.on_booted(&method(:booted))
|
56
|
+
in_background(&method(:status_loop))
|
57
|
+
in_background(&method(:watchdog_loop)) if @systemd.watchdog?
|
58
|
+
end
|
59
|
+
|
60
|
+
def booted
|
61
|
+
@launcher.events.log "* systemd: notify ready"
|
62
|
+
begin
|
63
|
+
@systemd.notify_ready
|
64
|
+
rescue
|
65
|
+
@launcher.events.error "! systemd: notify ready failed:\n #{$!.to_s}\n #{$!.backtrace.join("\n ")}"
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def restart(launcher)
|
70
|
+
@launcher.events.log "* systemd: notify reloading"
|
71
|
+
begin
|
72
|
+
@systemd.notify_reloading
|
73
|
+
rescue
|
74
|
+
@launcher.events.error "! systemd: notify reloading failed:\n #{$!.to_s}\n #{$!.backtrace.join("\n ")}"
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def fetch_stats
|
79
|
+
JSON.parse(@launcher.stats)
|
80
|
+
end
|
81
|
+
|
82
|
+
def status
|
83
|
+
Status.new(fetch_stats)
|
84
|
+
end
|
85
|
+
|
86
|
+
# Update systemd status event second or so
|
87
|
+
def status_loop
|
88
|
+
loop do
|
89
|
+
@launcher.events.debug "systemd: notify status"
|
90
|
+
begin
|
91
|
+
@systemd.notify_status(status.to_s)
|
92
|
+
rescue
|
93
|
+
@launcher.events.error "! systemd: notify status failed:\n #{$!.to_s}\n #{$!.backtrace.join("\n ")}"
|
94
|
+
ensure
|
95
|
+
sleep 1
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
# If watchdog is configured we'll send a ping at about half the timeout
|
101
|
+
# configured in systemd as recommended in the docs.
|
102
|
+
def watchdog_loop
|
103
|
+
@launcher.events.log "* systemd: watchdog detected (#{@systemd.watchdog_usec}usec)"
|
104
|
+
|
105
|
+
# Ruby wants seconds, and the docs suggest notifying halfway through the
|
106
|
+
# timeout.
|
107
|
+
sleep_seconds = @systemd.watchdog_usec / 1000.0 / 1000.0 / 2.0
|
108
|
+
|
109
|
+
loop do
|
110
|
+
begin
|
111
|
+
@launcher.events.debug "systemd: notify watchdog"
|
112
|
+
@systemd.notify_watchdog
|
113
|
+
rescue
|
114
|
+
@launcher.events.error "! systemd: notify watchdog failed:\n #{$!.to_s}\n #{$!.backtrace.join("\n ")}"
|
115
|
+
ensure
|
116
|
+
@launcher.events.debug "systemd: sleeping #{sleep_seconds}s"
|
117
|
+
sleep sleep_seconds
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
14
122
|
# Give us a way to talk to systemd.
|
15
123
|
#
|
16
124
|
# It'd be great to use systemd-notify for the whole shebang, but there's a
|
@@ -160,110 +268,4 @@ Puma::Plugin.create do
|
|
160
268
|
end
|
161
269
|
end
|
162
270
|
end
|
163
|
-
|
164
|
-
# Puma creates the plugin when encountering `plugin` in the config.
|
165
|
-
def initialize(loader)
|
166
|
-
# This is a Puma::PluginLoader
|
167
|
-
@loader = loader
|
168
|
-
end
|
169
|
-
|
170
|
-
# We can start doing something when we have a launcher:
|
171
|
-
def start(launcher)
|
172
|
-
@launcher = launcher
|
173
|
-
|
174
|
-
# Log relevant ENV in debug
|
175
|
-
@launcher.events.debug "systemd: NOTIFY_SOCKET=#{ENV["NOTIFY_SOCKET"].inspect}"
|
176
|
-
@launcher.events.debug "systemd: WATCHDOG_PID=#{ENV["WATCHDOG_PID"].inspect}"
|
177
|
-
@launcher.events.debug "systemd: WATCHDOG_USEC=#{ENV["WATCHDOG_USEC"].inspect}"
|
178
|
-
|
179
|
-
# Only install hooks if systemd is present, the systemd is booted by
|
180
|
-
# systemd, and systemd has asked us to notify it of events.
|
181
|
-
@systemd = Systemd.new
|
182
|
-
if @systemd.present? && @systemd.booted? && @systemd.notify?
|
183
|
-
@launcher.events.debug "systemd: detected running inside systemd, registering hooks"
|
184
|
-
register_hooks
|
185
|
-
else
|
186
|
-
@launcher.events.debug "systemd: not running within systemd, doing nothing"
|
187
|
-
end
|
188
|
-
end
|
189
|
-
|
190
|
-
private
|
191
|
-
|
192
|
-
# Are we a single process worker, or do we have worker processes?
|
193
|
-
#
|
194
|
-
# Copied from puma, it's private:
|
195
|
-
# https://github.com/puma/puma/blob/v3.6.0/lib/puma/launcher.rb#L267-L269
|
196
|
-
#
|
197
|
-
def clustered?
|
198
|
-
(@launcher.options[:workers] || 0) > 0
|
199
|
-
end
|
200
|
-
|
201
|
-
def register_hooks
|
202
|
-
(@launcher.config.options[:on_restart] ||= []) << method(:restart)
|
203
|
-
@launcher.events.on_booted(&method(:booted))
|
204
|
-
in_background(&method(:status_loop))
|
205
|
-
in_background(&method(:watchdog_loop)) if @systemd.watchdog?
|
206
|
-
end
|
207
|
-
|
208
|
-
def booted
|
209
|
-
@launcher.events.log "* systemd: notify ready"
|
210
|
-
begin
|
211
|
-
@systemd.notify_ready
|
212
|
-
rescue
|
213
|
-
@launcher.events.error "! systemd: notify ready failed:\n #{$!.to_s}\n #{$!.backtrace.join("\n ")}"
|
214
|
-
end
|
215
|
-
end
|
216
|
-
|
217
|
-
def restart(launcher)
|
218
|
-
@launcher.events.log "* systemd: notify reloading"
|
219
|
-
begin
|
220
|
-
@systemd.notify_reloading
|
221
|
-
rescue
|
222
|
-
@launcher.events.error "! systemd: notify reloading failed:\n #{$!.to_s}\n #{$!.backtrace.join("\n ")}"
|
223
|
-
end
|
224
|
-
end
|
225
|
-
|
226
|
-
def fetch_stats
|
227
|
-
JSON.parse(@launcher.stats)
|
228
|
-
end
|
229
|
-
|
230
|
-
def status
|
231
|
-
Status.new(fetch_stats)
|
232
|
-
end
|
233
|
-
|
234
|
-
# Update systemd status event second or so
|
235
|
-
def status_loop
|
236
|
-
loop do
|
237
|
-
@launcher.events.debug "systemd: notify status"
|
238
|
-
begin
|
239
|
-
@systemd.notify_status(status.to_s)
|
240
|
-
rescue
|
241
|
-
@launcher.events.error "! systemd: notify status failed:\n #{$!.to_s}\n #{$!.backtrace.join("\n ")}"
|
242
|
-
ensure
|
243
|
-
sleep 1
|
244
|
-
end
|
245
|
-
end
|
246
|
-
end
|
247
|
-
|
248
|
-
# If watchdog is configured we'll send a ping at about half the timeout
|
249
|
-
# configured in systemd as recommended in the docs.
|
250
|
-
def watchdog_loop
|
251
|
-
@launcher.events.log "* systemd: watchdog detected (#{@systemd.watchdog_usec}usec)"
|
252
|
-
|
253
|
-
# Ruby wants seconds, and the docs suggest notifying halfway through the
|
254
|
-
# timeout.
|
255
|
-
sleep_seconds = @systemd.watchdog_usec / 1000.0 / 1000.0 / 2.0
|
256
|
-
|
257
|
-
loop do
|
258
|
-
begin
|
259
|
-
@launcher.events.debug "systemd: notify watchdog"
|
260
|
-
@systemd.notify_watchdog
|
261
|
-
rescue
|
262
|
-
@launcher.events.error "! systemd: notify watchdog failed:\n #{$!.to_s}\n #{$!.backtrace.join("\n ")}"
|
263
|
-
ensure
|
264
|
-
@launcher.events.debug "systemd: sleeping #{sleep_seconds}s"
|
265
|
-
sleep sleep_seconds
|
266
|
-
end
|
267
|
-
end
|
268
|
-
end
|
269
271
|
end
|
metadata
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: puma-plugin-systemd
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Samuel Cochran
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain:
|
11
11
|
- |
|
@@ -28,22 +28,28 @@ cert_chain:
|
|
28
28
|
cTRkS42ilkarelc4DnSSO5jw7qFq7Cmf6F9hMx3xdoSWpLf+FvXJRbYrqwZIsmME
|
29
29
|
V8zEtJFhdNOFOdtcTE67qh/aYQe2y/LDnG9ywXHWdSeF4UUjg1WRt8s3OP8=
|
30
30
|
-----END CERTIFICATE-----
|
31
|
-
date: 2020-
|
31
|
+
date: 2020-08-28 00:00:00.000000000 Z
|
32
32
|
dependencies:
|
33
33
|
- !ruby/object:Gem::Dependency
|
34
34
|
name: puma
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
36
36
|
requirements:
|
37
|
-
- - "
|
37
|
+
- - ">="
|
38
38
|
- !ruby/object:Gem::Version
|
39
39
|
version: '3.6'
|
40
|
+
- - "<"
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '5'
|
40
43
|
type: :runtime
|
41
44
|
prerelease: false
|
42
45
|
version_requirements: !ruby/object:Gem::Requirement
|
43
46
|
requirements:
|
44
|
-
- - "
|
47
|
+
- - ">="
|
45
48
|
- !ruby/object:Gem::Version
|
46
49
|
version: '3.6'
|
50
|
+
- - "<"
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '5'
|
47
53
|
- !ruby/object:Gem::Dependency
|
48
54
|
name: json
|
49
55
|
requirement: !ruby/object:Gem::Requirement
|
@@ -100,7 +106,7 @@ dependencies:
|
|
100
106
|
- - "~>"
|
101
107
|
- !ruby/object:Gem::Version
|
102
108
|
version: '5.0'
|
103
|
-
description:
|
109
|
+
description:
|
104
110
|
email: sj26@sj26.com
|
105
111
|
executables: []
|
106
112
|
extensions: []
|
@@ -113,7 +119,7 @@ homepage: https://github.com/sj26/puma-plugin-systemd
|
|
113
119
|
licenses:
|
114
120
|
- MIT
|
115
121
|
metadata: {}
|
116
|
-
post_install_message:
|
122
|
+
post_install_message:
|
117
123
|
rdoc_options: []
|
118
124
|
require_paths:
|
119
125
|
- lib
|
@@ -128,8 +134,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
128
134
|
- !ruby/object:Gem::Version
|
129
135
|
version: '0'
|
130
136
|
requirements: []
|
131
|
-
rubygems_version: 3.
|
132
|
-
signing_key:
|
137
|
+
rubygems_version: 3.1.4
|
138
|
+
signing_key:
|
133
139
|
specification_version: 4
|
134
140
|
summary: 'Puma integration with systemd: notify, status, watchdog'
|
135
141
|
test_files: []
|
metadata.gz.sig
CHANGED