consul_syncer 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 59b87094f77b404c59b018035020eab02be5075c3b3d9dc3e68f28a64d8f70c0
4
- data.tar.gz: c61781655c3a8a67e266da1e97578b9e996d7c4b43248c777ef43e35608ba8ff
3
+ metadata.gz: 875c3e3eee8a06872dde88a612d7dfb8ae33ac59377d441e54afa6585da787b7
4
+ data.tar.gz: 4e1d1d3701fc464772745858109ff04260b0a8568e7ce8ad8eeab6ad6de26032
5
5
  SHA512:
6
- metadata.gz: 4f164174999daa718fe237edbff4a0b1ca3389ab56db5af2ce57cfd7876c20b0d5563de016b649677e0bd14b3d7e7c544c8857a27bb271eae1291fe6a43f02da
7
- data.tar.gz: bfd88ffae1281a70b942fb6f766fbabcc20f4ec49aca1df311a5cf79e4f6c08dc4dbd13f77110b8f38be4260e3e7a4daba8431d5996279739f475d1d0a71de6f
6
+ metadata.gz: ae7f93e5464309a3ed828ba18e7f43bd8ff2ec8a111b513d225f598d2a84c6df80c480722d3f1545a7813c2a34fbf5de10083357432d6f3f9336bf4923a9f082
7
+ data.tar.gz: 3cdeb2e0566669014aa37ae01cebcb453593e993cf9cee5a4155d26649fddb8f5ca38e179cd911979824c08a5fa98d23345bf441c5b2eb8e5937001263aedba0
@@ -1,3 +1,3 @@
1
1
  class ConsulSyncer
2
- VERSION = "1.1.0"
2
+ VERSION = "1.2.0"
3
3
  end
data/lib/consul_syncer.rb CHANGED
@@ -18,9 +18,13 @@ class ConsulSyncer
18
18
  # changing tags means all previous services need to be removed manually since
19
19
  # they can no longer be found
20
20
  def sync(expected_definitions, tags, dry: false)
21
- raise ArgumentError, "Need at least 1 tag to reliably update endpoints" if tags.empty?
21
+ planned = plan(expected_definitions, tags)
22
+ execute planned unless dry
23
+ planned.size # let users know what we did and keep legacy api working
24
+ end
22
25
 
23
- modified = 0
26
+ def plan(expected_definitions, tags)
27
+ raise ArgumentError, "Need at least 1 tag to reliably update endpoints" if tags.empty?
24
28
 
25
29
  # ensure consistent tags to find the endpoints after adding
26
30
  expected_definitions = expected_definitions.dup
@@ -44,6 +48,7 @@ class ConsulSyncer
44
48
 
45
49
  identifying = [:node, :service_id]
46
50
  interesting = [*identifying, :service, :service_address, :address, :tags, :port]
51
+ planned = []
47
52
 
48
53
  expected_definitions.each do |expected|
49
54
  description = "#{expected[:service] || "*"} / #{expected[:service_id] || "*"} on #{expected.fetch(:node)} in Consul"
@@ -60,23 +65,30 @@ class ConsulSyncer
60
65
  @logger.debug "Found #{description}"
61
66
  elsif remove_matching_service!(actual_definitions, expected, identifying)
62
67
  @logger.info "Updating #{description}"
63
- modified += 1
64
- register **expected unless dry
68
+ planned << [:register, [], expected]
65
69
  else
66
70
  @logger.info "Adding #{description}"
67
- modified += 1
68
- register **expected unless dry
71
+ planned << [:register, [], expected]
69
72
  end
70
73
  end
71
74
 
72
75
  # all definitions that are left did not match any expected definitions and are no longer needed
73
76
  actual_definitions.each do |actual|
74
77
  @logger.info "Removing #{actual.fetch(:service)} / #{actual.fetch(:service_id)} on #{actual.fetch(:node)} in Consul"
75
- modified += 1
76
- deregister actual.fetch(:node), actual.fetch(:service_id) unless dry
78
+ planned << [:deregister, [actual.fetch(:node), actual.fetch(:service_id)], nil]
77
79
  end
78
80
 
79
- modified
81
+ planned
82
+ end
83
+
84
+ def execute(planned)
85
+ planned.each do |m, args, kwargs|
86
+ if kwargs
87
+ send m, *args, **kwargs
88
+ else
89
+ send m, *args
90
+ end
91
+ end
80
92
  end
81
93
 
82
94
  private
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: consul_syncer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Grosser
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-06-30 00:00:00.000000000 Z
11
+ date: 2021-09-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday