class Socket
Implements the systemd NOTIFY_SOCKET process readiness protocol. See https://www.freedesktop.org/software/systemd/man/sd_notify.html for more details of the underlying protocol.
Definitions
NOTIFY_SOCKET = 'NOTIFY_SOCKET'
The name of the environment variable which contains the path to the notification socket.
MAXIMUM_MESSAGE_SIZE = 4096
The maximum allowed size of the UDP message.
def self.open!(environment = ENV)
Open a notification client attached to the current NOTIFY_SOCKET = 'NOTIFY_SOCKET'
if possible.
Implementation
def self.open!(environment = ENV)
if path = environment.delete(NOTIFY_SOCKET)
self.new(path)
end
end
def initialize(path)
Initialize the notification client.
Signature
-
parameter
path
String
The path to the UNIX socket used for sending messages to the process manager.
Implementation
def initialize(path)
@path = path
@address = Addrinfo.unix(path, ::Socket::SOCK_DGRAM)
end
def dump(message)
Dump a message in the format requied by sd_notify
.
Signature
-
parameter
message
Hash
Keys and values should be string convertible objects. Values which are
true
/false
are converted to1
/0
respectively.
Implementation
def dump(message)
buffer = String.new
message.each do |key, value|
# Conversions required by NOTIFY_SOCKET specifications:
if value == true
value = 1
elsif value == false
value = 0
end
buffer << "#{key.to_s.upcase}=#{value}\n"
end
return buffer
end
def send(**message)
Send the given message.
Signature
-
parameter
message
Hash
Implementation
def send(**message)
data = dump(message)
if data.bytesize > MAXIMUM_MESSAGE_SIZE
raise ArgumentError, "Message length #{message.bytesize} exceeds #{MAXIMUM_MESSAGE_SIZE}: #{message.inspect}"
end
@address.connect do |peer|
peer.sendmsg(data)
end
end
def error!(text, **message)
Send the specified error.
sd_notify
requires an errno
key, which defaults to -1
to indicate a generic error.
Implementation
def error!(text, **message)
message[:errno] ||= -1
send(status: text, **message)
end