class Async::Container::Notify::Socket
Implements the systemd NOTIFY_SOCKET
process readiness protocol. See <www.freedesktop.org/software/systemd/man/sd_notify.html> for more details of the underlying protocol.
Constants
- MAXIMUM_MESSAGE_SIZE
The maximum allowed size of the UDP message.
- NOTIFY_SOCKET
The name of the environment variable which contains the path to the notification socket.
Public Class Methods
Initialize the notification client. @parameter path [String] The path to the UNIX socket used for sending messages to the process manager.
# File lib/async/container/notify/socket.rb, line 50 def initialize(path) @path = path @endpoint = IO::Endpoint.unix(path, ::Socket::SOCK_DGRAM) end
Open a notification client attached to the current {NOTIFY_SOCKET} if possible.
# File lib/async/container/notify/socket.rb, line 42 def self.open!(environment = ENV) if path = environment.delete(NOTIFY_SOCKET) self.new(path) end end
Public Instance Methods
Dump a message in the format requied by `sd_notify`. @parameter message [Hash] Keys and values should be string convertible objects. Values which are `true`/`false` are converted to `1`/`0` respectively.
# File lib/async/container/notify/socket.rb, line 57 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
Send the specified error. `sd_notify` requires an `errno` key, which defaults to `-1` to indicate a generic error.
# File lib/async/container/notify/socket.rb, line 92 def error!(text, **message) message[:errno] ||= -1 send(status: text, **message) end
Send the given message. @parameter message [Hash]
# File lib/async/container/notify/socket.rb, line 76 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 Sync do @endpoint.connect do |peer| peer.send(data) end end end