-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add systemd notify and watchdog support #2438
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
require_relative "helper" | ||
require_relative "helpers/integration" | ||
|
||
require 'sd_notify' | ||
|
||
class TestIntegrationSystemd < TestIntegration | ||
def setup | ||
skip "Skipped because Systemd support is linux-only" if windows? || osx? | ||
skip UNIX_SKT_MSG unless UNIX_SKT_EXIST | ||
skip_unless_signal_exist? :TERM | ||
skip_on :jruby | ||
|
||
super | ||
|
||
::Dir::Tmpname.create("puma_socket") do |sockaddr| | ||
@sockaddr = sockaddr | ||
@socket = Socket.new(:UNIX, :DGRAM, 0) | ||
socket_ai = Addrinfo.unix(sockaddr) | ||
@socket.bind(socket_ai) | ||
ENV["NOTIFY_SOCKET"] = sockaddr | ||
end | ||
end | ||
|
||
def teardown | ||
return if skipped? | ||
@socket.close if @socket | ||
File.unlink(@sockaddr) if @sockaddr | ||
@socket = nil | ||
@sockaddr = nil | ||
ENV["NOTIFY_SOCKET"] = nil | ||
ENV["WATCHDOG_USEC"] = nil | ||
end | ||
|
||
def socket_message | ||
@socket.recvfrom(15)[0] | ||
end | ||
|
||
def test_systemd_integration | ||
cli_server "test/rackup/hello.ru" | ||
assert_equal(socket_message, "READY=1") | ||
|
||
connection = connect | ||
restart_server connection | ||
assert_equal(socket_message, "RELOADING=1") | ||
assert_equal(socket_message, "READY=1") | ||
|
||
stop_server | ||
assert_equal(socket_message, "STOPPING=1") | ||
end | ||
|
||
def test_systemd_watchdog | ||
ENV["WATCHDOG_USEC"] = "1_000_000" | ||
|
||
cli_server "test/rackup/hello.ru" | ||
assert_equal(socket_message, "READY=1") | ||
|
||
assert_equal(socket_message, "WATCHDOG=1") | ||
|
||
stop_server | ||
assert_match(socket_message, "STOPPING=1") | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm... doesn't it defeat the purpose if you start the watchdog as a separate thread? How would that ever fail? It should either be integrated into the main event loop or at least somehow check whether the service is actually still working.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that's a fair point but I wouldn't know how to do it better. Perhaps this is a good enough implementation for now and it allows for a better implementation in the future. Maybe the thread should perform a request to see if it's served? The question then becomes: which one? Should it be configurable?