class AutoConsul::Runner::AgentProcess

Constants

STOP_SIGNAL
VALID_STOP_STATUSES
VALID_VERIFY_STATUSES

Attributes

args[R]
exit_code[R]
pid[R]
status[R]
stop_queue[R]
stop_thread[R]
thread[R]

Public Class Methods

new(args) click to toggle source
# File lib/auto-consul/runner.rb, line 18
def initialize args
  @args = args.dup.freeze
  @callbacks = {}
end

Public Instance Methods

handle_signals!() click to toggle source
# File lib/auto-consul/runner.rb, line 47
def handle_signals!
  if @stop_queue.nil?
    @stop_queue = Queue.new
    @stop_thread = Thread.new do
      while true
        @stop_queue.pop
        stop!
      end
    end
    ['INT', 'TERM'].each do |sig|
      Signal.trap(sig) do
        @stop_queue << sig
      end
    end
  end
  nil
end
launch!() click to toggle source
# File lib/auto-consul/runner.rb, line 31
def launch!
  set_status :starting
  @thread = Thread.new do
    Thread.current.abort_on_exception = true
    run_agent
  end
end
on_down(&action) click to toggle source
# File lib/auto-consul/runner.rb, line 27
def on_down &action
  register_callback :down, &action
end
on_stopping(&action) click to toggle source
# File lib/auto-consul/runner.rb, line 80
def on_stopping &action
  register_callback :stopping, &action
end
on_up(&action) click to toggle source
# File lib/auto-consul/runner.rb, line 23
def on_up &action
  register_callback :up, &action
end
register_callback(on_status, &action) click to toggle source
# File lib/auto-consul/runner.rb, line 94
def register_callback on_status, &action
  (@callbacks[on_status] ||= []) << action
end
run!() click to toggle source
# File lib/auto-consul/runner.rb, line 98
def run!
  launch!
  verify_up!
  status
end
run_agent() click to toggle source
# File lib/auto-consul/runner.rb, line 39
def run_agent
  handle_signals!
  @pid = spawn(*(['consul', 'agent'] + args), :pgroup => true)
  result = Process.waitpid2(@pid)
  @exit_code = result[1].exitstatus
  set_status :down
end
run_callbacks(on_status) click to toggle source
# File lib/auto-consul/runner.rb, line 120
def run_callbacks on_status
  if callbacks = @callbacks[on_status]
    callbacks.each do |callback|
      callback.call self
    end
  end
end
set_status(new_status) click to toggle source
# File lib/auto-consul/runner.rb, line 128
def set_status new_status
  @status = new_status
  run_callbacks new_status
  new_status
end
stop!() click to toggle source
# File lib/auto-consul/runner.rb, line 87
def stop!
  raise "The consul agent is not running (no pid)" if pid.nil?
  raise "The consul agent is not running (status #{status.to_s})." unless VALID_STOP_STATUSES.include? status
  set_status :stopping
  Process.kill STOP_SIGNAL, pid
end
verify_up!() click to toggle source
# File lib/auto-consul/runner.rb, line 67
def verify_up!
  sleep INITIAL_VERIFY_SLEEP
  tries = 0
  while VALID_VERIFY_STATUSES.include?(status) and tries < RETRIES
    sleep SLEEP_INTERVAL ** tries if tries > 0
    if system('consul', 'info')
      set_status :up
    else
      tries += 1
    end
  end
end
wait() click to toggle source
# File lib/auto-consul/runner.rb, line 104
def wait
  if (t = thread).nil?
    raise "The consul agent has not started within this runner."
  end
  t.join
  exit_code
end
while_up(&action) click to toggle source
# File lib/auto-consul/runner.rb, line 112
def while_up &action
  on_up do |obj|
    thread = Thread.new { action.call obj }
    obj.on_stopping {|x| thread.kill }
    obj.on_down {|x| thread.kill }
  end
end