Module: Raptor::Systemd

Defined in:
lib/raptor/systemd.rb,
sig/generated/raptor/systemd.rbs

Overview

Integration with systemd's service notification protocol and socket-activation file descriptors.

Constant Summary collapse

LISTEN_FDS_START =

Returns:

  • (::Integer)
3
LISTEN_FDNAMES_ENV =

Returns:

  • (::String)
"LISTEN_FDNAMES"
LISTEN_FDS_ENV =

Returns:

  • (::String)
"LISTEN_FDS"
LISTEN_PID_ENV =

Returns:

  • (::String)
"LISTEN_PID"
NOTIFY_SOCKET_ENV =

Returns:

  • (::String)
"NOTIFY_SOCKET"

Class Method Summary collapse

Class Method Details

.clear_listen_envvoid

This method returns an undefined value.

Clears the socket-activation environment variables so children don't act on stale values.



63
64
65
66
67
# File 'lib/raptor/systemd.rb', line 63

def self.clear_listen_env
  ENV.delete(LISTEN_FDNAMES_ENV)
  ENV.delete(LISTEN_FDS_ENV)
  ENV.delete(LISTEN_PID_ENV)
end

.listen_fdsArray<Integer>

Returns the file descriptors passed in via socket activation, or an empty array when systemd has not exported any.

Returns:

  • (Array<Integer>)


50
51
52
53
54
55
# File 'lib/raptor/systemd.rb', line 50

def self.listen_fds
  return [] unless ENV[LISTEN_PID_ENV]&.to_i == Process.pid

  count = ENV[LISTEN_FDS_ENV]&.to_i || 0
  Array.new(count) { |index| LISTEN_FDS_START + index }
end

.notify(message) ⇒ Boolean

Sends message to the systemd notification socket, returning true on success and false when the socket is unset or the send fails.

Parameters:

  • message (String)

    notify protocol payload, e.g. "READY=1"

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/raptor/systemd.rb', line 25

def self.notify(message)
  socket_path = ENV[NOTIFY_SOCKET_ENV]
  return false if socket_path.nil? || socket_path.empty?

  address = if socket_path.start_with?("@")
    Socket.pack_sockaddr_un("\0#{socket_path[1..]}")
  else
    Socket.pack_sockaddr_un(socket_path)
  end

  socket = Socket.new(Socket::AF_UNIX, Socket::SOCK_DGRAM, 0)
  socket.send(message, 0, address)
  true
rescue SystemCallError, IOError
  false
ensure
  socket&.close
end