class ProcessWrapper

Constants

HTTPCHECK_INTERVAL

Attributes

pid[R]

Public Class Methods

new(opts = {}, verbose = false) click to toggle source
# File lib/nsq-cluster/process_wrapper.rb, line 6
def initialize(opts = {}, verbose = false)
  @verbose = verbose
end

Public Instance Methods

another_instance_is_running?() click to toggle source
# File lib/nsq-cluster/process_wrapper.rb, line 37
def another_instance_is_running?
  if respond_to?(:http_port)
    http_port_open?
  else
    false
  end
end
args() click to toggle source
# File lib/nsq-cluster/process_wrapper.rb, line 51
def args
  raise 'you have to override this in a subclass as well, buddy'
end
block_until_running() click to toggle source
# File lib/nsq-cluster/process_wrapper.rb, line 65
def block_until_running
  if respond_to?(:http_port) && respond_to?(:host)
    wait_for_http_port
  else
    raise "Can't block without http port and host"
  end
end
block_until_stopped() click to toggle source
# File lib/nsq-cluster/process_wrapper.rb, line 74
def block_until_stopped
  if respond_to?(:http_port) && respond_to?(:host)
    wait_for_no_http_port
  else
    raise "Can't block without http port and host"
  end
end
command() click to toggle source
# File lib/nsq-cluster/process_wrapper.rb, line 46
def command
  raise 'you have to override this in a subclass, hotshot'
end
destroy() click to toggle source
# File lib/nsq-cluster/process_wrapper.rb, line 27
def destroy
  stop(async: true) if running?
end
output() click to toggle source
# File lib/nsq-cluster/process_wrapper.rb, line 56
def output
  if @verbose
    :out
  else
    '/dev/null'
  end
end
running?() click to toggle source
# File lib/nsq-cluster/process_wrapper.rb, line 32
def running?
  !!@pid
end
start(opts = {}) click to toggle source
# File lib/nsq-cluster/process_wrapper.rb, line 11
def start(opts = {})
  raise "#{command} is already running" if running? || another_instance_is_running?
  @pid = spawn(command, *args, [:out, :err] => output)
  block_until_running unless opts[:async]
end
stop(opts = {}) click to toggle source
# File lib/nsq-cluster/process_wrapper.rb, line 18
def stop(opts = {})
  raise "#{command} is not running" unless running?
  Process.kill('TERM', @pid)
  Process.waitpid(@pid)
  @pid = nil
  block_until_stopped unless opts[:async]
end

Private Instance Methods

http_port_open?() click to toggle source
# File lib/nsq-cluster/process_wrapper.rb, line 100
def http_port_open?
  begin
    response = Net::HTTP.get_response(URI("http://#{host}:#{http_port}/ping"))
    return response.is_a?(Net::HTTPSuccess)
  rescue Errno::ECONNREFUSED
    return false
  end
end
wait_for_http_port() click to toggle source
# File lib/nsq-cluster/process_wrapper.rb, line 84
def wait_for_http_port
  until http_port_open? do
    sleep HTTPCHECK_INTERVAL
  end
  puts "HTTP port #{http_port} responded to /ping." if @verbose
end
wait_for_no_http_port() click to toggle source
# File lib/nsq-cluster/process_wrapper.rb, line 92
def wait_for_no_http_port
  until !http_port_open? do
    sleep HTTPCHECK_INTERVAL
  end
  puts "HTTP port #{http_port} stopped responding to /ping." if @verbose
end