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

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

Constant Summary collapse

NOTIFY_SOCKET =
'NOTIFY_SOCKET'
MAXIMUM_MESSAGE_SIZE =
4096

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Client

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

Constructor Details

#initialize(path) ⇒ Socket

Returns a new instance of Socket.



43
44
45
46
# File 'lib/async/container/notify/socket.rb', line 43

def initialize(path)
  @path = path
  @endpoint = IO::Endpoint.unix(path, ::Socket::SOCK_DGRAM)
end

Class Method Details

.open!(environment = ENV) ⇒ Object



37
38
39
40
41
# File 'lib/async/container/notify/socket.rb', line 37

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

Instance Method Details

#dump(message) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/async/container/notify/socket.rb', line 48

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



79
80
81
82
83
# File 'lib/async/container/notify/socket.rb', line 79

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

#send(**message) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/async/container/notify/socket.rb', line 65

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