Class: Populus::WatchThread

Inherits:
Object
  • Object
show all
Defined in:
lib/populus/watch_thread.rb

Class Method Summary collapse

Class Method Details

.consul_watch(*args) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/populus/watch_thread.rb', line 9

def self.consul_watch(*args)
  wait = Thread.fork do
    begin
      stdin, stdout, stderr = *Open3.popen3(
        Populus.consul_bin, 'watch', *args, '/bin/cat'
      )
      stdin.close
      name = args[args.index('-name') + 1]
      while l = stdout.gets
        Populus.logger.debug "accept JSON: %s" % l
        data = JSON.parse l

        accepters = Populus::Pool.find_events_by_name(name)
        accepters.each do |accepter|
          begin
            accepter.accept(data)
          rescue => e
            Populus.logger.warn "Error on event: %s, %s. Skip." % [e.class, e.message]
          end
        end
      end
    rescue => e
      Populus.logger.error e
    end
  end
  return wait
end