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.



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/consul/async/consul_endpoint.rb', line 151

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.



149
150
151
# File 'lib/consul/async/consul_endpoint.rb', line 149

def conf
  @conf
end

#default_valueObject (readonly)

Returns the value of attribute default_value.



149
150
151
# File 'lib/consul/async/consul_endpoint.rb', line 149

def default_value
  @default_value
end

#enforce_json_200Object (readonly)

Returns the value of attribute enforce_json_200.



149
150
151
# File 'lib/consul/async/consul_endpoint.rb', line 149

def enforce_json_200
  @enforce_json_200
end

#last_resultObject (readonly)

Returns the value of attribute last_result.



149
150
151
# File 'lib/consul/async/consul_endpoint.rb', line 149

def last_result
  @last_result
end

#pathObject (readonly)

Returns the value of attribute path.



149
150
151
# File 'lib/consul/async/consul_endpoint.rb', line 149

def path
  @path
end

#query_paramsObject (readonly)

Returns the value of attribute query_params.



149
150
151
# File 'lib/consul/async/consul_endpoint.rb', line 149

def query_params
  @query_params
end

#queueObject (readonly)

Returns the value of attribute queue.



149
150
151
# File 'lib/consul/async/consul_endpoint.rb', line 149

def queue
  @queue
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



149
150
151
# File 'lib/consul/async/consul_endpoint.rb', line 149

def start_time
  @start_time
end

#statsObject (readonly)

Returns the value of attribute stats.



149
150
151
# File 'lib/consul/async/consul_endpoint.rb', line 149

def stats
  @stats
end

#x_consul_indexObject (readonly)

Returns the value of attribute x_consul_index.



149
150
151
# File 'lib/consul/async/consul_endpoint.rb', line 149

def x_consul_index
  @x_consul_index
end

Instance Method Details

#_enable_network_debugObject



173
174
175
176
177
178
179
180
181
182
183
# File 'lib/consul/async/consul_endpoint.rb', line 173

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



189
190
191
# File 'lib/consul/async/consul_endpoint.rb', line 189

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

#on_response(&block) ⇒ Object



185
186
187
# File 'lib/consul/async/consul_endpoint.rb', line 185

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

#ready?Boolean

Returns:

  • (Boolean)


193
194
195
# File 'lib/consul/async/consul_endpoint.rb', line 193

def ready?
  @ready
end

#terminateObject



197
198
199
# File 'lib/consul/async/consul_endpoint.rb', line 197

def terminate
  @stopping = true
end