Class: Etcenv::Watcher
- Inherits:
-
Object
- Object
- Etcenv::Watcher
- Defined in:
- lib/etcenv/watcher.rb
Constant Summary collapse
- WATCH_TIMEOUT =
120
Instance Attribute Summary collapse
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#verbose ⇒ Object
readonly
Returns the value of attribute verbose.
Instance Method Summary collapse
- #auto_reload_loop ⇒ Object
- #etcd ⇒ Object
-
#initialize(env, verbose: false) ⇒ Watcher
constructor
A new instance of Watcher.
- #watch ⇒ Object
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
#env ⇒ Object (readonly)
Returns the value of attribute env.
14 15 16 |
# File 'lib/etcenv/watcher.rb', line 14 def env @env end |
#verbose ⇒ Object (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_loop ⇒ Object
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 |
#etcd ⇒ Object
16 17 18 |
# File 'lib/etcenv/watcher.rb', line 16 def etcd env.etcd end |
#watch ⇒ Object
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 |