1
0
Fork 0
mirror of https://github.com/puma/puma.git synced 2022-11-09 13:48:40 -05:00
puma--puma/lib/puma/systemd.rb
Ewoud Kohl van Wijngaarden 288a4cf756
Add systemd notify and watchdog support (#2438)
* Adds systemd notification support

* Improve systemd notification support

This takes the work by @acmh and improves on it. This is done by
squashing all commits and rebasing it. Then the following changes were
made:

* Dropped SD_NOTIFY env var. There is aleady the NOTIFY_SOCKET env var
  presented by systemd and is redundant.
* Move code is pushed in Puma::Systemd
* on_reload now emits RELOADING=1 notification to systemd
* Drop lower bound check on usec. Systemd can only be configured in
  seconds and it's hard to misconfigure. The actual code should be safe.
* Clean up integration tests and skip on JRuby

Co-authored-by: Artur Montenegro <artur.montenegro@tempest.com.br>
2020-10-26 16:02:31 -06:00

46 lines
940 B
Ruby

# frozen_string_literal: true
require 'sd_notify'
module Puma
class Systemd
def initialize(events)
@events = events
end
def hook_events
@events.on_booted { SdNotify.ready }
@events.on_stopped { SdNotify.stopping }
@events.on_restart { SdNotify.reloading }
end
def start_watchdog
return unless SdNotify.watchdog?
ping_f = watchdog_sleep_time
log "Pinging systemd watchdog every #{ping_f.round(1)} sec"
Thread.new do
loop do
sleep ping_f
SdNotify.watchdog
end
end
end
private
def watchdog_sleep_time
usec = Integer(ENV["WATCHDOG_USEC"])
sec_f = usec / 1_000_000.0
# "It is recommended that a daemon sends a keep-alive notification message
# to the service manager every half of the time returned here."
sec_f / 2
end
def log(str)
@events.log str
end
end
end