Class: Etcenv::Watcher

Inherits:
Object
  • Object
show all
Defined in:
lib/etcenv/watcher.rb

Constant Summary collapse

WATCH_TIMEOUT =
120

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env, verbose: false) ⇒ Watcher

Returns a new instance of Watcher.



7
8
9
10
11
12
# File 'lib/etcenv/watcher.rb', line 7

def initialize(env, verbose: false)
  @env = env
  @verbose = verbose
  @indices = {}
  @lock = Mutex.new
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



14
15
16
# File 'lib/etcenv/watcher.rb', line 14

def env
  @env
end

#verboseObject (readonly)

Returns the value of attribute verbose.



14
15
16
# File 'lib/etcenv/watcher.rb', line 14

def verbose
  @verbose
end

Instance Method Details

#auto_reload_loopObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/etcenv/watcher.rb', line 32

def auto_reload_loop
  retries = 0
  loop do
    begin
      watch
      $stderr.puts "[watcher] reloading env #{env.root_key}" if verbose
      env.load
      yield env if block_given?
    rescue => e
      retries += 1
      interval = (2**retries) * 0.1

      $stderr.puts "[watcher][error] Failed to reload env #{env.root_key}: #{e.inspect}"
      $stderr.puts "\t#{e.backtrace.join("\n\t")}"
      $stderr.puts "[watcher][error] RETRYING reload #{env.root_key} in #{'%.2f' % interval} sec"
      sleep interval
    else
      retries = 0
    end
  end
end

#etcdObject



16
17
18
# File 'lib/etcenv/watcher.rb', line 16

def etcd
  env.etcd
end

#watchObject



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/etcenv/watcher.rb', line 20

def watch
  ch = Queue.new
  threads = env.keys.map do |key|
    Thread.new(ch, key, env.cluster_index, &method(:watch_thread)).tap do |th|
      th.abort_on_exception = true
    end
  end
  report = ch.pop
  threads.each(&:kill)
  report
end