Class: Consul::Async::ConsulEndpoint

Inherits:
Object
  • Object
show all
Defined in:
lib/consul/async/consul_endpoint.rb

Overview

This class represents a specific path in Consul HTTP API It also stores x_consul_index and keep track on updates of API So, it basically performs all the optimizations to keep updated with Consul internal state.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(conf, path, enforce_json_200 = true, query_params = {}, default_value = '[]', agent = nil) ⇒ ConsulEndpoint

Returns a new instance of ConsulEndpoint.



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/consul/async/consul_endpoint.rb', line 145

def initialize(conf, path, enforce_json_200 = true, query_params = {}, default_value = '[]', agent = nil)
  @conf = conf.create(path, agent: agent)
  @default_value = default_value
  @path = path
  @queue = EM::Queue.new
  @x_consul_index = 0
  @s_callbacks = []
  @e_callbacks = []
  @enforce_json_200 = enforce_json_200
  @start_time = Time.now.utc
  @consecutive_errors = 0
  @query_params = query_params
  @stopping = false
  @stats = EndPointStats.new
  @last_result = ConsulResult.new(default_value, false, HttpResponse.new(nil), 0, stats, 1, fake: true)
  on_response { |result| @stats.on_response result }
  on_error { |http| @stats.on_error http }
  _enable_network_debug if conf.debug && conf.debug[:network]
  fetch
  queue << 0
end

Instance Attribute Details

#confObject (readonly)

Returns the value of attribute conf.



144
145
146
# File 'lib/consul/async/consul_endpoint.rb', line 144

def conf
  @conf
end

#default_valueObject (readonly)

Returns the value of attribute default_value.



144
145
146
# File 'lib/consul/async/consul_endpoint.rb', line 144

def default_value
  @default_value
end

#enforce_json_200Object (readonly)

Returns the value of attribute enforce_json_200.



144
145
146
# File 'lib/consul/async/consul_endpoint.rb', line 144

def enforce_json_200
  @enforce_json_200
end

#last_resultObject (readonly)

Returns the value of attribute last_result.



144
145
146
# File 'lib/consul/async/consul_endpoint.rb', line 144

def last_result
  @last_result
end

#pathObject (readonly)

Returns the value of attribute path.



144
145
146
# File 'lib/consul/async/consul_endpoint.rb', line 144

def path
  @path
end

#query_paramsObject (readonly)

Returns the value of attribute query_params.



144
145
146
# File 'lib/consul/async/consul_endpoint.rb', line 144

def query_params
  @query_params
end

#queueObject (readonly)

Returns the value of attribute queue.



144
145
146
# File 'lib/consul/async/consul_endpoint.rb', line 144

def queue
  @queue
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



144
145
146
# File 'lib/consul/async/consul_endpoint.rb', line 144

def start_time
  @start_time
end

#statsObject (readonly)

Returns the value of attribute stats.



144
145
146
# File 'lib/consul/async/consul_endpoint.rb', line 144

def stats
  @stats
end

#x_consul_indexObject (readonly)

Returns the value of attribute x_consul_index.



144
145
146
# File 'lib/consul/async/consul_endpoint.rb', line 144

def x_consul_index
  @x_consul_index
end

Instance Method Details

#_enable_network_debugObject



167
168
169
170
171
172
173
174
175
176
177
# File 'lib/consul/async/consul_endpoint.rb', line 167

def _enable_network_debug
  on_response do |result|
    state = result.x_consul_index.to_i < 1 ? '[WARN]' : '[ OK ]'
    stats = result.stats
    warn "[DBUG]#{state}#{result.modified? ? '[MODFIED]' : '[NO DIFF]'}" \
    "[s:#{stats.successes},err:#{stats.errors}]" \
    "[#{stats.body_bytes_human.ljust(8)}][#{stats.bytes_per_sec_human.ljust(9)}]"\
    " #{path.ljust(48)} idx:#{result.x_consul_index}, next in #{result.retry_in} s"
  end
  on_error { |http| warn "[ERROR]: #{path}: #{http.error.inspect}" }
end

#on_error(&block) ⇒ Object



183
184
185
# File 'lib/consul/async/consul_endpoint.rb', line 183

def on_error(&block)
  @e_callbacks << block
end

#on_response(&block) ⇒ Object



179
180
181
# File 'lib/consul/async/consul_endpoint.rb', line 179

def on_response(&block)
  @s_callbacks << block
end

#ready?Boolean

Returns:

  • (Boolean)


187
188
189
# File 'lib/consul/async/consul_endpoint.rb', line 187

def ready?
  @ready
end

#terminateObject



191
192
193
# File 'lib/consul/async/consul_endpoint.rb', line 191

def terminate
  @stopping = true
end