Class: Cluster::Discovery::Consul

Inherits:
Object
  • Object
show all
Defined in:
lib/cluster/discovery/consul.rb

Instance Method Summary collapse

Constructor Details

#initialize(consul_url: nil) ⇒ Consul

Returns a new instance of Consul.



4
5
6
7
8
9
10
11
# File 'lib/cluster/discovery/consul.rb', line 4

def initialize(consul_url: nil)
  unless consul_url.nil?
    Diplomat.configure do |config|
      config.url = consul_url
    end
  end
  @configuration = Diplomat.configuration
end

Instance Method Details

#build_extra_opts(args) ⇒ Array

Build args for Diplomat::Service.get

Parameters:

  • args (Hash)

    the options to pass to Diplomat::Service.get

  • opts (Hash)

    a customizable set of options

Returns:

  • (Array)

    The options to pass to Diplomat::Service.get



76
77
78
79
80
81
82
# File 'lib/cluster/discovery/consul.rb', line 76

def build_extra_opts(args)
  opts = []
  leader = args[:leader] == true ? :first : :all
  opts << leader
  opts << { tag: args[:tags] } unless args[:tags].empty?
  opts
end

#discover(consul_service:, leader: false, tags: [], healthy: true) ⇒ Struct

Discovery nodes using a Consul cluster

Parameters:

  • consul_service: (String)

    Name of the Consul service

  • leader: (Boolean) (defaults to: false)

    (false) Fetch :all or or :first(the leader)

  • tags: (Array) (defaults to: [])

    ([]) List of tags to limit discovery by

Returns:

  • (Struct)

    Object representing the Node in the service



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/cluster/discovery/consul.rb', line 19

def discover(consul_service:, leader: false, tags: [], healthy: true)
  scope, options = build_extra_opts(leader: leader, tags: tags)
  nodes = Diplomat::Service.get(
    consul_service,
    scope,
    options
  )
  nodes = [nodes]
  nodes.flatten!

  nodes = discover_health_nodes(nodes, consul_service) if healthy
  nodes
end

#discover_health_nodes(nodes, consul_service) ⇒ Object



33
34
35
36
37
# File 'lib/cluster/discovery/consul.rb', line 33

def discover_health_nodes(nodes, consul_service)
  nodes = filtered_healthy_nodes(nodes.map(&:Address), consul_service)
  nodes = nodes_passing_checks(nodes)
  service_response(nodes)
end

#filtered_healthy_nodes(node_list, consul_service) ⇒ Object



39
40
41
42
43
# File 'lib/cluster/discovery/consul.rb', line 39

def filtered_healthy_nodes(node_list, consul_service)
  Diplomat::Health.service(consul_service).delete_if do |service|
    !node_list.include?(service['Node']['Address'])
  end
end

#nodes_passing_checks(nodes) ⇒ Object



45
46
47
48
49
# File 'lib/cluster/discovery/consul.rb', line 45

def nodes_passing_checks(nodes)
  nodes.delete_if do |node|
    node['Checks'].any? { |check| check['Status'] != 'passing' }
  end
end

#service_response(nodes) ⇒ Object

rubocop:disable Metrics/MethodLength



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/cluster/discovery/consul.rb', line 51

def service_response(nodes) # rubocop:disable Metrics/MethodLength
  nodes.map do |node|
    n = node['Node']
    s = node['Service']
    OpenStruct.new(
      Node: n['Node'],
      Address: n['Address'],
      ServiceID: s['ID'],
      ServiceName: s['Service'],
      ServiceTags: s['Tags'],
      ServiceAddress: s['Address'],
      ServicePort: s['Port'],
      Checks: node['Checks'],
      _Node: node['Node'],
      _Service: node['Service']
    )
  end
end