Class: Async::Container::Notify::Socket

Inherits:
Client
  • Object
show all
Defined in:
lib/async/container/notify/socket.rb

Overview

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.

Constant Summary collapse

NOTIFY_SOCKET =

The name of the environment variable which contains the path to the notification socket.

"NOTIFY_SOCKET"
MAXIMUM_MESSAGE_SIZE =

The maximum allowed size of the UDP message.

4096

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Client

#ready!, #reloading!, #restarting!, #status!, #stopping!

Constructor Details

#initialize(path) ⇒ Socket

Initialize the notification client.



30
31
32
33
# File 'lib/async/container/notify/socket.rb', line 30

def initialize(path)
	@path = path
	@address = Addrinfo.unix(path, ::Socket::SOCK_DGRAM)
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



36
37
38
# File 'lib/async/container/notify/socket.rb', line 36

def path
  @path
end

#The path to the UNIX socket used for sending messages to the controller.(pathtotheUNIXsocketused) ⇒ Object (readonly)



36
# File 'lib/async/container/notify/socket.rb', line 36

attr :path

Class Method Details

.open!(environment = ENV) ⇒ Object

Open a notification client attached to the current NOTIFY_SOCKET if possible.



22
23
24
25
26
# File 'lib/async/container/notify/socket.rb', line 22

def self.open!(environment = ENV)
	if path = environment.delete(NOTIFY_SOCKET)
		self.new(path)
	end
end

Instance Method Details

#dump(message) ⇒ Object

Dump a message in the format requied by ‘sd_notify`.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/async/container/notify/socket.rb', line 40

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

#error!(text, **message) ⇒ Object

Send the specified error. ‘sd_notify` requires an `errno` key, which defaults to `-1` to indicate a generic error.



73
74
75
76
77
# File 'lib/async/container/notify/socket.rb', line 73

def error!(text, **message)
	message[:errno] ||= -1
	
	super
end

#send(**message) ⇒ Object

Send the given message.



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/async/container/notify/socket.rb', line 59

def send(**message)
	data = dump(message)
	
	if data.bytesize > MAXIMUM_MESSAGE_SIZE
		raise ArgumentError, "Message length #{data.bytesize} exceeds #{MAXIMUM_MESSAGE_SIZE}: #{message.inspect}"
	end
	
	@address.connect do |peer|
		peer.sendmsg(data)
	end
end