Class: Puma::Plugin::Systemd::Systemd

Inherits:
Object
  • Object
show all
Defined in:
lib/puma/plugin/systemd.rb

Overview

Give us a way to talk to systemd.

It’d be great to use systemd-notify for the whole shebang, but there’s a critical error introducing a race condition:

https://github.com/systemd/systemd/issues/2739

systemd-notify docs:

https://www.freedesktop.org/software/systemd/man/systemd-notify.html

We could use sd-daemon (sd_notify and friends) but they require a C extension, and are really just fancy wrappers for blatting datagrams at a socket advertised via ENV. See the docs:

https://www.freedesktop.org/software/systemd/man/sd-daemon.html

Instance Method Summary collapse

Instance Method Details

#booted?Boolean

Is the system currently booted with systemd?

See also sd_booted:

https://www.freedesktop.org/software/systemd/man/sd_booted.html

Returns:

  • (Boolean)


165
166
167
# File 'lib/puma/plugin/systemd.rb', line 165

def booted?
  File.directory?("/run/systemd/system/")
end

#notify?Boolean

Are we running within a systemd unit that expects us to notify?

Returns:

  • (Boolean)


170
171
172
# File 'lib/puma/plugin/systemd.rb', line 170

def notify?
  ENV.include?("NOTIFY_SOCKET")
end

#notify_pidObject

Tell systemd we are now the main pid



194
195
196
# File 'lib/puma/plugin/systemd.rb', line 194

def notify_pid
  notify("MAINPID=#{$$}")
end

#notify_readyObject

Tell systemd we’re fully started and ready to handle requests



199
200
201
# File 'lib/puma/plugin/systemd.rb', line 199

def notify_ready
  notify("READY=1")
end

#notify_reloadingObject

Tell systemd we’re restarting



209
210
211
# File 'lib/puma/plugin/systemd.rb', line 209

def notify_reloading
  notify("RELOADING=1")
end

#notify_status(status) ⇒ Object

Tell systemd our status



204
205
206
# File 'lib/puma/plugin/systemd.rb', line 204

def notify_status(status)
  notify("STATUS=#{status}")
end

#notify_watchdogObject

Tell systemd we’re still alive



214
215
216
# File 'lib/puma/plugin/systemd.rb', line 214

def notify_watchdog
  notify("WATCHDOG=1")
end

#watchdog?Boolean

Returns:

  • (Boolean)


222
223
224
225
# File 'lib/puma/plugin/systemd.rb', line 222

def watchdog?
  ENV.include?("WATCHDOG_USEC") &&
    (!ENV.include?("WATCHDOG_PID") || ENV["WATCHDOG_PID"].to_i == $$)
end

#watchdog_usecObject

How long between pings until the watchdog will think we’re unhealthy?



228
229
230
# File 'lib/puma/plugin/systemd.rb', line 228

def watchdog_usec
  ENV["WATCHDOG_USEC"].to_i
end