Async::ContainerSourceAsyncContainerNotifyServer

class Server

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

Nested

Definitions

BOOLEAN_FIELDS

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

Implementation

BOOLEAN_FIELDS = 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

def self.load(message)

Parse a message, according to the sd_notify protocol.

Signature

parameter message String

The message to parse.

returns Hash

The parsed message.

Implementation

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

def self.generate_path

Generate a new unique path for the UNIX socket.

Signature

returns String

The path for the UNIX socket.

Implementation

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

def self.open(path = self.generate_path)

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

Implementation

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

def initialize(path)

Initialize the server with the given path.

Signature

parameter path String

The path to the UNIX socket.

Implementation

def initialize(path)
	@path = path
end

attr :path

Signature

attribute String

The path to the UNIX socket.

def bind

Generate a bound context for receiving messages.

Signature

returns Context

The bound context.

Implementation

def bind
	Context.new(@path)
end