Last active
November 22, 2017 21:31
-
-
Save jemc/66b781a774bad85e0c27 to your computer and use it in GitHub Desktop.
Controlling supervisord from Ruby using XMLRPC over Unix sockets.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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