Class: Cluster::Discovery::Consul
- Inherits:
-
Object
- Object
- Cluster::Discovery::Consul
- Defined in:
- lib/cluster/discovery/consul.rb
Instance Method Summary collapse
-
#build_extra_opts(args) ⇒ Array
Build args for Diplomat::Service.get.
-
#discover(consul_service:, leader: false, tags: [], healthy: true) ⇒ Struct
Discovery nodes using a Consul cluster.
- #discover_health_nodes(nodes, consul_service) ⇒ Object
- #filtered_healthy_nodes(node_list, consul_service) ⇒ Object
-
#initialize(consul_url: nil) ⇒ Consul
constructor
A new instance of Consul.
- #nodes_passing_checks(nodes) ⇒ Object
-
#service_response(nodes) ⇒ Object
rubocop:disable Metrics/MethodLength.
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
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
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, = build_extra_opts(leader: leader, tags: ) nodes = Diplomat::Service.get( consul_service, scope, ) 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 |