Skip to content

Instantly share code, notes, and snippets.

@jemc
Last active November 22, 2017 21:31
Show Gist options
  • Save jemc/66b781a774bad85e0c27 to your computer and use it in GitHub Desktop.
Save jemc/66b781a774bad85e0c27 to your computer and use it in GitHub Desktop.
Controlling supervisord from Ruby using XMLRPC over Unix sockets.
require "net/http"
require "socket"
gem "libxml-xmlrpc"
require "xml/libxml/xmlrpc"
module Supervisor
class << self
attr_accessor :socket_path
def namespace
@namespace ||= :supervisor
end
attr_writer :namespace
def make_socket
Net::BufferedIO.new(UNIXSocket.new(socket_path))
end
def call(method, *args)
method = method.to_s
method = "supervisor.#{method}" unless method.include?('.')
socket = make_socket
req = Net::HTTP::Post.new("/RPC2")
req.body = XML::XMLRPC::Builder.call(method, *args)
req.content_type = "text/xml"
req.exec(socket, "1.1", "/RPC2")
res = nil
loop do
res = Net::HTTPResponse.read_new(socket)
break unless res.is_a? Net::HTTPContinue
end
res.reading_body(socket, true) { }
XML::XMLRPC::Parser.new(res.body).first
end
end
end
require_relative 'supervisor'
Supervisor.socket_path = "/tmp/supervisor.sock" # Use your socket path here
Supervisor.call :getState
#=> {:statename=>"RUNNING", :statecode=>1}
# See http://supervisord.org/api.html for all available XMLRPC methods.
# Use the supervisor_twiddler plugin (https://github.com/mnaberez/supervisor_twiddler) for additional XMLRPC methods.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment