puma-plugin-systemd 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 331bad09b8a7f0e62675cd82edb25ac4154f062aa68c0a0d8281018ba276dd8b
4
- data.tar.gz: 81e80990542efce424e76f4155a7bb4e416d63036163ac6b33aebbe88f20adc1
3
+ metadata.gz: 58415395213c4ab44c816dd21f22aee85da0f86adb50e94d1ba4bd627d7d66b3
4
+ data.tar.gz: 88cb0b6f956b3fa4547671aa365fd6287c2af8ba6c795efdb27d4a0f3f4b2e66
5
5
  SHA512:
6
- metadata.gz: c38c6cc61c8695eaa7b28406899d95c75e1a65a4754f98a947948e87ea3329544c303defd99eba30263e896e3f2712524e2bec0730ce159cddf16216715540fc
7
- data.tar.gz: d3991a2398c378bfe02eb49a6e813180998d609a14d929d48a69cda5f2ba5732f120909b08ff287a8a373dc3a33503e45f93a6e2f4a6515ccba505027dc8898f
6
+ metadata.gz: f728a069997b449299d828fae771ba33a89efab3675df5518cbb952819155f3159aae22a4cab535d48688d65774eca89c6d2d553b723f7291e8e749378d782a3
7
+ data.tar.gz: 6f0fa1f9a1f2132ecba2990cfe44b394fe3d886d18e741a97ea192fa5bd22b47a02084f226c8e1b5da948ade8d976cf9ede36f27acbf74c6d2db91fe488b757e
Binary file
data.tar.gz.sig CHANGED
Binary file
data/README.md CHANGED
@@ -31,7 +31,7 @@ Add it to your puma config:
31
31
  ```ruby
32
32
  # config/puma.rb
33
33
 
34
- bind "http://127.0.0.1:9292"
34
+ bind "tcp://127.0.0.1:9292"
35
35
 
36
36
  workers 2
37
37
  threads 8, 16
@@ -115,6 +115,27 @@ release a new version, update the version number in `version.rb`, and then run
115
115
  `bundle exec rake release`, which will create a git tag for the version, push
116
116
  git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
117
117
 
118
+ ### Testing
119
+
120
+ There are few automated tests yet.
121
+
122
+ There is an `example` directory which contains a Dockerfile and basic
123
+ configuration for testing. To use it:
124
+
125
+ ```
126
+ # Build an image with systemd and puma configured
127
+ docker build -t puma-plugin-systemd-example -f example/Dockerfile .
128
+
129
+ # Start a new container from the image in the background
130
+ docker run --name puma-plugin-systemd --privileged --detach --rm puma-plugin-systemd
131
+
132
+ # Show puma systemd integration
133
+ docker exec puma-plugin-systemd-example systemctl status puma
134
+
135
+ # Stop the container
136
+ docker exec puma-plugin-systemd-example halt
137
+ ```
138
+
118
139
  ## Contributing
119
140
 
120
141
  Bug reports and pull requests are welcome on GitHub at
@@ -28,12 +28,19 @@ class Puma::Plugin::Systemd
28
28
  @launcher.events.debug "systemd: WATCHDOG_PID=#{ENV["WATCHDOG_PID"].inspect}"
29
29
  @launcher.events.debug "systemd: WATCHDOG_USEC=#{ENV["WATCHDOG_USEC"].inspect}"
30
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.
31
+ # Only install hooks if the system is booted by systemd, and systemd has
32
+ # asked us to notify it of events.
33
33
  @systemd = Systemd.new
34
- if @systemd.present? && @systemd.booted? && @systemd.notify?
34
+ if @systemd.booted? && @systemd.notify?
35
35
  @launcher.events.debug "systemd: detected running inside systemd, registering hooks"
36
+
36
37
  register_hooks
38
+
39
+ # In clustered mode, we can start the status loop early and watch the
40
+ # workers boot
41
+ start_status_loop_thread if clustered?
42
+
43
+ start_watchdog_loop_thread if @systemd.watchdog?
37
44
  else
38
45
  @launcher.events.debug "systemd: not running within systemd, doing nothing"
39
46
  end
@@ -51,10 +58,8 @@ class Puma::Plugin::Systemd
51
58
  end
52
59
 
53
60
  def register_hooks
54
- (@launcher.config.options[:on_restart] ||= []) << method(:restart)
55
61
  @launcher.events.on_booted(&method(:booted))
56
- in_background(&method(:status_loop))
57
- in_background(&method(:watchdog_loop)) if @systemd.watchdog?
62
+ (@launcher.config.options[:on_restart] ||= []) << method(:restart)
58
63
  end
59
64
 
60
65
  def booted
@@ -64,6 +69,10 @@ class Puma::Plugin::Systemd
64
69
  rescue
65
70
  @launcher.events.error "! systemd: notify ready failed:\n #{$!.to_s}\n #{$!.backtrace.join("\n ")}"
66
71
  end
72
+
73
+ # In single mode, we can only start the status loop once the server is
74
+ # started after booted
75
+ start_status_loop_thread
67
76
  end
68
77
 
69
78
  def restart(launcher)
@@ -97,6 +106,12 @@ class Puma::Plugin::Systemd
97
106
  end
98
107
  end
99
108
 
109
+ def start_status_loop_thread
110
+ # This is basically what Puma::Plugins.add_background / fire_background
111
+ # does, but at a time of our choosing.
112
+ @status_loop_thread ||= Thread.new(&method(:status_loop))
113
+ end
114
+
100
115
  # If watchdog is configured we'll send a ping at about half the timeout
101
116
  # configured in systemd as recommended in the docs.
102
117
  def watchdog_loop
@@ -119,6 +134,10 @@ class Puma::Plugin::Systemd
119
134
  end
120
135
  end
121
136
 
137
+ def start_watchdog_loop_thread
138
+ @watchdog_loop_thread ||= Thread.new(&method(:watchdog_loop))
139
+ end
140
+
122
141
  # Give us a way to talk to systemd.
123
142
  #
124
143
  # It'd be great to use systemd-notify for the whole shebang, but there's a
@@ -137,29 +156,14 @@ class Puma::Plugin::Systemd
137
156
  # https://www.freedesktop.org/software/systemd/man/sd-daemon.html
138
157
  #
139
158
  class Systemd
140
- # Do we have a systemctl binary? This is a good indicator whether systemd
141
- # is installed at all.
142
- def present?
143
- ENV["PATH"].split(":").any? { |dir| File.exists?(File.join(dir, "systemctl")) }
144
- end
145
-
146
159
  # Is the system currently booted with systemd?
147
160
  #
148
- # We could check for the systemd run directory directly, but we can't be
149
- # absolutely sure of it's location and breaks encapsulation. We can be sure
150
- # that systemctl is present on a systemd system and understand whether
151
- # systemd is running. The systemd-notify binary actually recommends this.
152
- #
153
- # An alternate way to check for this state is to call systemctl(1) with
154
- # the is-system-running command. It will return "offline" if the system
155
- # was not booted with systemd.
156
- #
157
161
  # See also sd_booted:
158
162
  #
159
163
  # https://www.freedesktop.org/software/systemd/man/sd_booted.html
160
164
  #
161
165
  def booted?
162
- IO.popen(["systemctl", "is-system-running"], &:read).chomp != "offline"
166
+ File.directory?("/run/systemd/system/")
163
167
  end
164
168
 
165
169
  # Are we running within a systemd unit that expects us to notify?
@@ -260,11 +264,27 @@ class Puma::Plugin::Systemd
260
264
  end
261
265
  end
262
266
 
267
+ def pool_capacity
268
+ if clustered?
269
+ @stats["worker_status"].map { |s| s["last_status"].fetch("pool_capacity", 0) }.inject(0, &:+)
270
+ else
271
+ @stats.fetch("pool_capacity", 0)
272
+ end
273
+ end
274
+
275
+ def max_threads
276
+ if clustered?
277
+ @stats["worker_status"].map { |s| s["last_status"].fetch("max_threads", 0) }.inject(0, &:+)
278
+ else
279
+ @stats.fetch("max_threads", 0)
280
+ end
281
+ end
282
+
263
283
  def to_s
264
284
  if clustered?
265
- "puma #{Puma::Const::VERSION} cluster: #{booted_workers}/#{workers} workers: #{running} threads, #{backlog} backlog"
285
+ "puma #{Puma::Const::VERSION} cluster: #{booted_workers}/#{workers} workers: #{running}/#{max_threads} threads, #{pool_capacity} available, #{backlog} backlog"
266
286
  else
267
- "puma #{Puma::Const::VERSION}: #{running} threads, #{backlog} backlog"
287
+ "puma #{Puma::Const::VERSION}: #{running}/#{max_threads} threads, #{pool_capacity} available, #{backlog} backlog"
268
288
  end
269
289
  end
270
290
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: puma-plugin-systemd
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Cochran
@@ -28,7 +28,7 @@ cert_chain:
28
28
  cTRkS42ilkarelc4DnSSO5jw7qFq7Cmf6F9hMx3xdoSWpLf+FvXJRbYrqwZIsmME
29
29
  V8zEtJFhdNOFOdtcTE67qh/aYQe2y/LDnG9ywXHWdSeF4UUjg1WRt8s3OP8=
30
30
  -----END CERTIFICATE-----
31
- date: 2020-08-28 00:00:00.000000000 Z
31
+ date: 2020-08-29 00:00:00.000000000 Z
32
32
  dependencies:
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: puma
metadata.gz.sig CHANGED
Binary file