class Sv::Api

Attributes

socket_path[R]

Public Class Methods

new(socket_path) click to toggle source
# File lib/sv/api.rb, line 12
def initialize(socket_path)
  @socket_path = File.realdirpath socket_path
end

Public Instance Methods

active_groups() click to toggle source
# File lib/sv/api.rb, line 103
def active_groups
  running_states = ["STOPPING", "RUNNING"]
  running_jobs = jobs.select { |j| running_states.include? j.statename }
  running_jobs.map { |j| j.group }.uniq
end
add_group(name) click to toggle source
# File lib/sv/api.rb, line 55
def add_group(name)
  ok, output = call_safe("supervisor.addProcessGroup", name)
  if not ok
    msg = "adding group #{name} failed: #{output.faultString}"
    raise Error, msg if not output.faultString =~ /\AALREADY_ADDED/
  end
end
close_connection() click to toggle source
# File lib/sv/api.rb, line 113
def close_connection
  @rpc = nil
end
health_check() click to toggle source
# File lib/sv/api.rb, line 123
def health_check
  jobs = self.jobs
  not_running_jobs = jobs.reject { |j| ["RUNNING", "STARTING"].include? j.statename }
  if not_running_jobs.size > 0
    names = not_running_jobs.map { |j| j.name }
    str = names[0..4].join(", ")
    msg = "jobs not running: #{str}"
    raise Error, msg
  end
  puts "OK"
end
job_stopped?(group, name) click to toggle source
# File lib/sv/api.rb, line 63
def job_stopped?(group, name)
  job = call "supervisor.getProcessInfo", "#{group}:#{name}"
  job["state"] == 0 ? true : false
end
jobs() click to toggle source
# File lib/sv/api.rb, line 98
def jobs
  jobs_array = call "supervisor.getAllProcessInfo"
  jobs = jobs_array.map { |j| OpenStruct.new(j) }
end
pid() click to toggle source
# File lib/sv/api.rb, line 68
def pid
  call "supervisor.getPID"
end
print_status() click to toggle source
remove_group(name) click to toggle source
# File lib/sv/api.rb, line 50
def remove_group(name)
  call "supervisor.stopProcessGroup", name
  call "supervisor.removeProcessGroup", name
end
reopen_logs() click to toggle source
# File lib/sv/api.rb, line 117
def reopen_logs
  Process.kill("USR2", pid)
rescue => e
  raise Error, "unable to reopen logs"
end
reread_config() click to toggle source
# File lib/sv/api.rb, line 109
def reread_config
  call "supervisor.reloadConfig"
end
shutdown() click to toggle source
# File lib/sv/api.rb, line 20
def shutdown
  call "supervisor.stopAllProcesses", false
  sleep 2
  stopping = jobs.select { |j| j.statename == "STOPPING" }
  stopping.each do |j|
    puts "killing #{j.group}:#{j.name}"
    begin
      Process.kill("KILL", j.pid)
    rescue => e
      puts "warn #{j.pid}: #{e.message}"
    end
  end
  sleep 1
  call "supervisor.shutdown"
  close_connection
end
start_job(group, name) click to toggle source
# File lib/sv/api.rb, line 37
def start_job(group, name)
  call "supervisor.startProcess", "#{group}:#{name}"
end
start_jobs(wait: true) click to toggle source
# File lib/sv/api.rb, line 16
def start_jobs(wait: true)
  wait ? start_jobs_in_foreground : start_jobs_in_background
end
start_jobs_in_background() click to toggle source
# File lib/sv/api.rb, line 86
def start_jobs_in_background
  call "supervisor.startAllProcesses", "wait=false"
end
start_jobs_in_foreground() click to toggle source
# File lib/sv/api.rb, line 90
def start_jobs_in_foreground
  jobs.each do |j|
    printf "#{j.name}: starting"
    start_job j.group, j.name
    puts "\r#{j.name}: started "
  end
end
stop_job(group, name, wait: true) click to toggle source
# File lib/sv/api.rb, line 41
def stop_job(group, name, wait: true)
  return if job_stopped?(group, name)
  ok, output = call_safe "supervisor.stopProcess", "#{group}:#{name}", wait
  if not ok
    msg = "stopping job #{name} failed: #{output.faultString}"
    raise Error, msg if not output.faultString =~ /\ANOT_RUNNING/
  end
end

Private Instance Methods

call(*args) click to toggle source
# File lib/sv/api.rb, line 141
def call(*args)
  output = rpc.call(*args)
  return output
rescue XMLRPC::FaultException => e
  puts e.message
  raise ::Sv::Error, "error running command #{args[0]}"
end
call_safe(*args) click to toggle source
# File lib/sv/api.rb, line 149
def call_safe(*args)
  rpc.call2(*args)
end
job_cwd(pid) click to toggle source
# File lib/sv/api.rb, line 168
def job_cwd(pid)
  File.readlink "/proc/#{pid}/cwd"
end
rpc() click to toggle source
# File lib/sv/api.rb, line 137
def rpc
  @rpc ||= ::XMLRPC::Client.new(socket_path, "/RPC2")
end
uptime(started_at) click to toggle source
# File lib/sv/api.rb, line 153
def uptime(started_at)
  return "-" if started_at.to_i == 0
  uptime = (Time.now.to_i - started_at).to_i
  mm, ss = uptime.divmod(60)
  hh, mm = mm.divmod(60)
  dd, hh = hh.divmod(24)
  if dd == 1
    "%d day, %02d::%02d::%02d" % [dd, hh, mm, ss]
  elsif dd > 1
    "%d days, %02d::%02d::%02d" % [dd, hh, mm, ss]
  else
    "%02d::%02d::%02d" % [hh, mm, ss]
  end
end