class Falcon::Service::Supervisor
Implements a host supervisor which can restart the host services and provide various metrics about the running processes.
def do_metrics(message)
def do_metrics(message) Process::Metrics::General.capture(pid: Process.ppid, ppid: Process.ppid) end
def do_restart(message)
def do_restart(message) # Tell the parent of this process group to spin up a new process group/container. # Wait for that to start accepting new connections. # Stop accepting connections. # Wait for existing connnections to drain. # Terminate this process group. signal = message[:signal] || :INT Process.kill(signal, Process.ppid) end
def endpoint
The endpoint which the supervisor will bind to.
def endpoint @evaluator.endpoint end
def handle(message)
Handle an incoming request.
def handle(message) case message[:please] when 'restart' self.do_restart(message) when 'metrics' self.do_metrics(message) end end
def initialize(...)
Initialize the supervisor using the given environment.
def initialize(...) super @bound_endpoint = nil end
def setup(container)
Start the supervisor process which accepts connections from the bound endpoint and processes JSON formatted messages.
def setup(container) container.run(name: self.name, restart: true, count: 1) do |instance| Async do @bound_endpoint.accept do |peer| stream = Async::IO::Stream.new(peer) while message = stream.gets("\0") response = handle(JSON.parse(message, symbolize_names: true)) stream.puts(response.to_json, separator: "\0") end end instance.ready! end end super end
def start
def start Console.logger.info(self) {"Binding to #{self.endpoint}..."} @bound_endpoint = Sync{self.endpoint.bound} super end
def stop
def stop @bound_endpoint&.close @bound_endpoint = nil super end