Class: Vivarium::ApiServer

Inherits:
Object
  • Object
show all
Defined in:
lib/vivarium/api_server.rb

Overview

Minimal HTTP/1.1 server over a Unix domain socket exposing the daemon control API.

Constant Summary collapse

STREAM_POLL_TIMEOUT =
1.0

Instance Method Summary collapse

Constructor Details

#initialize(socket_path:, event_log:, registry:, daemon_pid: Process.pid) ⇒ ApiServer

Returns a new instance of ApiServer.



67
68
69
70
71
72
# File 'lib/vivarium/api_server.rb', line 67

def initialize(socket_path:, event_log:, registry:, daemon_pid: Process.pid)
  @socket_path = socket_path
  @event_log = event_log
  @registry = registry
  @daemon_pid = daemon_pid
end

Instance Method Details

#startObject



74
75
76
77
78
79
80
81
# File 'lib/vivarium/api_server.rb', line 74

def start
  FileUtils.mkdir_p(File.dirname(@socket_path))
  File.unlink(@socket_path) if File.exist?(@socket_path)
  @server = UNIXServer.new(@socket_path)
  File.chmod(0o666, @socket_path)
  @thread = Thread.new { accept_loop }
  self
end

#stopObject



83
84
85
86
87
88
89
# File 'lib/vivarium/api_server.rb', line 83

def stop
  @server&.close
rescue StandardError
  nil
ensure
  File.unlink(@socket_path) if @socket_path && File.exist?(@socket_path)
end