Class: AutoConsul::Runner::AgentProcess

Inherits:
Object
  • Object
show all
Defined in:
lib/auto-consul/runner.rb

Constant Summary collapse

VALID_VERIFY_STATUSES =
[nil, :starting]
VALID_STOP_STATUSES =
[nil, :starting, :up, :stopping]
STOP_SIGNAL =
"SIGINT"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ AgentProcess

Returns a new instance of AgentProcess.



18
19
20
21
# File 'lib/auto-consul/runner.rb', line 18

def initialize args
  @args = args.dup.freeze
  @callbacks = {}
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



10
11
12
# File 'lib/auto-consul/runner.rb', line 10

def args
  @args
end

#exit_codeObject (readonly)

Returns the value of attribute exit_code.



11
12
13
# File 'lib/auto-consul/runner.rb', line 11

def exit_code
  @exit_code
end

#pidObject (readonly)

Returns the value of attribute pid.



12
13
14
# File 'lib/auto-consul/runner.rb', line 12

def pid
  @pid
end

#statusObject (readonly)

Returns the value of attribute status.



13
14
15
# File 'lib/auto-consul/runner.rb', line 13

def status
  @status
end

#stop_queueObject (readonly)

Returns the value of attribute stop_queue.



15
16
17
# File 'lib/auto-consul/runner.rb', line 15

def stop_queue
  @stop_queue
end

#stop_threadObject (readonly)

Returns the value of attribute stop_thread.



16
17
18
# File 'lib/auto-consul/runner.rb', line 16

def stop_thread
  @stop_thread
end

#threadObject (readonly)

Returns the value of attribute thread.



14
15
16
# File 'lib/auto-consul/runner.rb', line 14

def thread
  @thread
end

Instance Method Details

#handle_signals!Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 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!Object



31
32
33
34
35
36
37
# 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) ⇒ Object



27
28
29
# File 'lib/auto-consul/runner.rb', line 27

def on_down &action
  register_callback :down, &action
end

#on_stopping(&action) ⇒ Object



80
81
82
# File 'lib/auto-consul/runner.rb', line 80

def on_stopping &action
  register_callback :stopping, &action
end

#on_up(&action) ⇒ Object



23
24
25
# File 'lib/auto-consul/runner.rb', line 23

def on_up &action
  register_callback :up, &action
end

#register_callback(on_status, &action) ⇒ Object



94
95
96
# File 'lib/auto-consul/runner.rb', line 94

def register_callback on_status, &action
  (@callbacks[on_status] ||= []) << action
end

#run!Object



98
99
100
101
102
# File 'lib/auto-consul/runner.rb', line 98

def run!
  launch!
  verify_up!
  status
end

#run_agentObject



39
40
41
42
43
44
45
# 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) ⇒ Object



120
121
122
123
124
125
126
# 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) ⇒ Object



128
129
130
131
132
# File 'lib/auto-consul/runner.rb', line 128

def set_status new_status
  @status = new_status
  run_callbacks new_status
  new_status
end

#stop!Object



87
88
89
90
91
92
# 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!Object



67
68
69
70
71
72
73
74
75
76
77
78
# 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

#waitObject



104
105
106
107
108
109
110
# 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) ⇒ Object



112
113
114
115
116
117
118
# 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