class Falcon::Service::Supervisor
def do_metrics(message)
def do_metrics(message) Process::Metrics.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
def endpoint @evaluator.endpoint end
def handle(message)
def handle(message) case message[:please] when 'restart' self.do_restart(message) when 'metrics' self.do_metrics(message) end end
def initialize(environment)
def initialize(environment) super @bound_endpoint = nil end
def setup(container)
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 Async.logger.info(self) {"Binding to #{self.endpoint}..."} @bound_endpoint = Async::Reactor.run do Async::IO::SharedEndpoint.bound(self.endpoint) end.wait super end
def stop
def stop @bound_endpoint&.close @bound_endpoint = nil super end