Class: ConsulSyncer
- Inherits:
-
Object
- Object
- ConsulSyncer
- Defined in:
- lib/consul_syncer.rb,
lib/consul_syncer/version.rb,
lib/consul_syncer/wrapper.rb,
lib/consul_syncer/endpoint.rb
Overview
-
parses json responses
-
fails with descriptive output when a request fails
Defined Under Namespace
Constant Summary collapse
- VERSION =
"0.1.0"
Instance Method Summary collapse
-
#initialize(url, logger: Logger.new(STDOUT)) ⇒ ConsulSyncer
constructor
A new instance of ConsulSyncer.
-
#sync(expected_definitions, tags) ⇒ Object
changing tags means all previous services need to be removed manually since they can no longer be found.
Constructor Details
#initialize(url, logger: Logger.new(STDOUT)) ⇒ ConsulSyncer
Returns a new instance of ConsulSyncer.
12 13 14 15 |
# File 'lib/consul_syncer.rb', line 12 def initialize(url, logger: Logger.new(STDOUT)) @consul = Wrapper.new(Faraday.new(url)) @logger = logger end |
Instance Method Details
#sync(expected_definitions, tags) ⇒ Object
changing tags means all previous services need to be removed manually since they can no longer be found
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/consul_syncer.rb', line 19 def sync(expected_definitions, ) raise ArgumentError, "Need at least 1 tag to reliably update endpoints" if .empty? modified = 0 # ensure consistent tags to find the endpoints after adding expected_definitions = expected_definitions.dup expected_definitions.each do |d| d[:tags] += d[:tags].sort! end actual_definitions = consul_endpoints().map do |consul_endpoint| { node: consul_endpoint.node, address: consul_endpoint.ip, service: consul_endpoint.name, service_id: consul_endpoint.service_id, tags: consul_endpoint..sort, port: consul_endpoint.port } end = [:node, :service] interesting = [*, :address, :tags, :port] expected_definitions.each do |expected| description = "#{expected.fetch(:service)} on #{expected.fetch(:node)} in Consul" if remove_matching_service!(actual_definitions, expected, interesting) @logger.info "Found #{description}" elsif remove_matching_service!(actual_definitions, expected, ) @logger.info "Updating #{description}" modified += 1 register expected else @logger.info "Adding #{description}" modified += 1 register expected end end # all definitions that are left did not match any expected definitions and are no longer needed actual_definitions.each do |actual| @logger.info "Removing #{actual.fetch(:service)} on #{actual.fetch(:node)} in Consul" modified += 1 deregister actual.fetch(:node), actual.fetch(:service_id) end modified end |