Class: Async::Container::Notify::Server

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

Overview

A simple UDP server that can be used to receive messages from a child process, tracking readiness, status changes, etc.

Defined Under Namespace

Classes: Context

Constant Summary collapse

MAXIMUM_MESSAGE_SIZE =
4096
BOOLEAN_FIELDS =

Fields from the systemd sd_notify protocol that use “0”/“1” for boolean values. See: www.freedesktop.org/software/systemd/man/sd_notify.html

Set[
	:ready,
	:reloading,
	:stopping,
	:fdstore,
	:fdstoreremove,
	:fdpoll,
	:barrier,
	:watchdog, # Note: also accepts "trigger" as a string value.
	:healthy, # Extension: not in standard systemd protocol.
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Server

Initialize the server with the given path.



83
84
85
# File 'lib/async/container/notify/server.rb', line 83

def initialize(path)
	@path = path
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



88
89
90
# File 'lib/async/container/notify/server.rb', line 88

def path
  @path
end

#The path to the UNIX socket.(pathtotheUNIXsocket.) ⇒ Object (readonly)



88
# File 'lib/async/container/notify/server.rb', line 88

attr :path

Class Method Details

.generate_pathObject

Generate a new unique path for the UNIX socket.



68
69
70
71
72
73
# File 'lib/async/container/notify/server.rb', line 68

def self.generate_path
	File.expand_path(
		"async-container-#{::Process.pid}-#{SecureRandom.hex(8)}.ipc",
		Dir.tmpdir
	)
end

.load(message) ⇒ Object

Parse a message, according to the ‘sd_notify` protocol.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/async/container/notify/server.rb', line 37

def self.load(message)
	lines = message.split("\n")
	
	lines.pop if lines.last == ""
	
	pairs = lines.map do |line|
		key, value = line.split("=", 2)
		
		key = key.downcase.to_sym
		
		if BOOLEAN_FIELDS.include?(key)
			# Convert "0"/"1" to boolean for known systemd boolean fields:
			if value == "0"
				value = false
			elsif value == "1"
				value = true
			end
		elsif key == :errno and value =~ /\A\-?\d+\z/
			# Convert errno to integer:
			value = Integer(value)
		end
		
		next [key, value]
	end
	
	return Hash[pairs]
end

.open(path = self.generate_path) ⇒ Object

Open a new server instance with a temporary and unique path.



76
77
78
# File 'lib/async/container/notify/server.rb', line 76

def self.open(path = self.generate_path)
	self.new(path)
end

Instance Method Details

#bindObject

Generate a bound context for receiving messages.



93
94
95
# File 'lib/async/container/notify/server.rb', line 93

def bind
	Context.new(@path)
end