Class: Lusnoc::Watcher
Instance Method Summary collapse
- #build_wait_condition(_url, time_left, max_consul_wait) ⇒ Object
-
#initialize(base_url, timeout: 0, eclass: Lusnoc::TimeoutError, emessage: 'watch timeout') ⇒ Watcher
constructor
A new instance of Watcher.
-
#run(max_consul_wait: nil) ⇒ Object
run Consul blocking request in a loop with timeout support.
Methods included from Helper
Constructor Details
#initialize(base_url, timeout: 0, eclass: Lusnoc::TimeoutError, emessage: 'watch timeout') ⇒ Watcher
Returns a new instance of Watcher.
10 11 12 13 14 15 16 17 18 |
# File 'lib/lusnoc/watcher.rb', line 10 def initialize(base_url, timeout: 0, eclass: Lusnoc::TimeoutError, emessage: 'watch timeout') @base_url = base_url @timeout = timeout @eclass = eclass @emessage = end |
Instance Method Details
#build_wait_condition(_url, time_left, max_consul_wait) ⇒ Object
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/lusnoc/watcher.rb', line 41 def build_wait_condition(_url, time_left, max_consul_wait) if time_left max = [time_left.to_i, max_consul_wait.to_i].max "&wait=#{max}s" elsif max_consul_wait "&wait=#{max_consul_wait.to_i}s" else '' end end |
#run(max_consul_wait: nil) ⇒ Object
run Consul blocking request in a loop with timeout support. break condition yielded by block call with response body
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/lusnoc/watcher.rb', line 22 def run(max_consul_wait: nil) logger.debug "Watch #{@base_url} with #{@timeout.inspect} timeout" last_x_consul_index = 1 Timeouter.loop!(@timeout, eclass: @eclass, message: @emessage) do |t| wait_condition = build_wait_condition(@base_url, t.left, max_consul_wait) url = "#{@base_url}?index=#{last_x_consul_index}#{wait_condition}" resp = Lusnoc.http_get(url, timeout: t.left) return true if yield(resp.body) logger.debug "Watch #{@base_url} response: #{resp.body}" index = [Integer(resp['x-consul-index']), 1].max last_x_consul_index = (index < last_x_consul_index ? 1 : index) sleep 0.4 end end |