Class: Consul::Async::ServiceInstance

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

Overview

The ServiceInstance has shortcuts (such as service_address method), but is basically a Hash.

Instance Method Summary collapse

Constructor Details

#initialize(obj) ⇒ ServiceInstance

Returns a new instance of ServiceInstance.



502
503
504
# File 'lib/consul/async/consul_template.rb', line 502

def initialize(obj)
  merge!(obj)
end

Instance Method Details

#current_weightObject

Return the weights applied on instance according to current status



552
553
554
555
# File 'lib/consul/async/consul_template.rb', line 552

def current_weight
  current_status = status
  weights[current_status.capitalize] || 0
end

#node_metaObject

Return [‘Node’]



507
508
509
# File 'lib/consul/async/consul_template.rb', line 507

def node_meta
  self['Node']['Meta'] || {}
end

#service_addressObject

Return [‘Service’] if defined, the address of node otherwise



512
513
514
515
516
# File 'lib/consul/async/consul_template.rb', line 512

def service_address
  val = self['Service']['Address']
  val = self['Node']['Address'] unless !val.nil? && val != ''
  val
end

#service_metaObject

Return a defined hash of string valued Service.Meta



519
520
521
# File 'lib/consul/async/consul_template.rb', line 519

def service_meta
  self['Service']['Meta'] || {}
end

#service_or_node_meta_value(key) ⇒ Object

If given key exists in Service.Meta returns it, otherwise the same key from return Node.Meta, otherwise return nil



525
526
527
# File 'lib/consul/async/consul_template.rb', line 525

def service_or_node_meta_value(key)
  service_meta[key] || node_meta[key]
end

#statusObject

Return the global state of a Service, will return passing|warning|critical



530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
# File 'lib/consul/async/consul_template.rb', line 530

def status
  ret = 'passing'
  checks = self['Checks']
  return ret unless checks

  checks.each do |chk|
    st = chk['Status']
    if st == 'critical'
      ret = st
    elsif st == 'warning' && ret == 'passing'
      ret = st
    end
  end
  ret
end

#weightsObject

Return Consul weights even if Consul version < 1.2.3 with same semantics



547
548
549
# File 'lib/consul/async/consul_template.rb', line 547

def weights
  self['Service']['Weights'] || { 'Passing' => 1, 'Warning' => 1 }
end