etcd-discovery 0.0.11 → 0.0.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 960dc0430e6a20341f0fef6abf70a983809ac5d5
4
- data.tar.gz: cec0aeba3b3d129be5f8e39cedd8c7e72803a914
3
+ metadata.gz: fe29a8729a9678c7983c4cabb8f7b6cb9e33d460
4
+ data.tar.gz: 9111832380a0338312a6cbef11bead94fcd414d9
5
5
  SHA512:
6
- metadata.gz: 955d3be4ffaabf4e733a940a7e5defa90f1dd4e688477595bd4b2bf67840b416cafe369492af8dd74efa5940234ecdb0757e61db247c7a951f5d103e88e60ee4
7
- data.tar.gz: acd4e8b4d6ba5e228064174c379ace5aec37ffdc6080a46b27f65d948e8ff83acc1f981825b391e570f83d732f4be424d5bcec6502887da3964fccf78de37e04
6
+ metadata.gz: 2140fc22e32b13b396c14f12bfb45f4a58babfe4c77529976d69ea6f99e0e7ae2ddcd6d2a9448adef66033e0a2bf919a4146a16e4060c4713eca8ffe3d0e8d45
7
+ data.tar.gz: 8625911eb2fdd877ab5d125673a91bb59adf7fb7587ce2d843417e2a135324f153b76188cd712b37b4bb1e05c1dbda942f6328deeb1bf7cc620cca574baf554f
@@ -27,12 +27,6 @@ module EtcdDiscovery
27
27
  end
28
28
 
29
29
  def self.register(service, host)
30
- Thread.new {
31
- begin
32
- Service.register(service, host)
33
- rescue => e
34
- puts "Fail to register #{service}: #{e.class} #{e} #{e.backtrace}"
35
- end
36
- }
30
+ Service.register(service, host)
37
31
  end
38
32
  end
@@ -0,0 +1,47 @@
1
+ module EtcdDiscovery
2
+ class Registrar
3
+ attr_reader :state
4
+ attr_reader :thread
5
+ attr_reader :host
6
+
7
+ def initialize(service, host)
8
+ @logger = Logger.new($stdout)
9
+
10
+ if host.is_a? Hash
11
+ @host = Host.new host
12
+ elsif host.is_a? EtcdDiscovery::Host
13
+ @host = host
14
+ else
15
+ raise TypeError, "host should be a Hash or a Etcd::Host, is a #{host.class}"
16
+ end
17
+
18
+ @service = service
19
+ @state = :new
20
+ end
21
+
22
+ def register
23
+ @state = :started
24
+ config = EtcdDiscovery.config
25
+ client = config.client
26
+ value = @host.to_json
27
+ key_name = "/services/#{@service}/#{@host.attributes['name']}"
28
+
29
+ @thread = Thread.new {
30
+ while @state == :started
31
+ begin
32
+ client.set(key_name, value: value, ttl: config.register_ttl)
33
+ rescue => e
34
+ logger.warn "Fail to set #{key_name}: #{e}, #{e.message}, #{e.class}"
35
+ end
36
+ sleep config.register_renew
37
+ end
38
+ logger.warn "Register '#{@service}' stopped"
39
+ }
40
+ self
41
+ end
42
+
43
+ def stop
44
+ @state = :stopped
45
+ end
46
+ end
47
+ end
@@ -24,29 +24,7 @@ module EtcdDiscovery
24
24
  end
25
25
 
26
26
  def self.register(service, host)
27
- logger = Logger.new($stdout)
28
-
29
- if host.is_a? Hash
30
- h = Host.new host
31
- elsif host.is_a? Etcd::Host
32
- h = host
33
- else
34
- raise TypeError, "host should be a Hash or a Etcd::Host, is a #{host.class}"
35
- end
36
-
37
- config = EtcdDiscovery.config
38
- client = config.client
39
- value = h.to_json
40
- key_name = "/services/#{service}/#{h.attributes['name']}"
41
-
42
- while true
43
- begin
44
- client.set(key_name, value: value, ttl: config.register_ttl)
45
- rescue => e
46
- logger.warn "Fail to set #{key_name}: #{e}, #{e.message}, #{e.class}"
47
- end
48
- sleep config.register_renew
49
- end
27
+ EtcdDiscovery::Registrar.new(service, host).register
50
28
  end
51
29
  end
52
30
  end
@@ -0,0 +1,43 @@
1
+ require 'spec_helper.rb'
2
+
3
+ RSpec.describe EtcdDiscovery::Registrar do
4
+ describe '.new' do
5
+ context 'with a Hash' do
6
+ subject { EtcdDiscovery::Registrar.new "service", {"name" => "example.com", "ports" => {"http" => 80}} }
7
+ it "should transform the has in Host" do
8
+ expect(subject.host.attributes["name"]).to eq "example.com"
9
+ end
10
+ end
11
+
12
+ context 'with a EtcdDiscovery::Host' do
13
+ subject { EtcdDiscovery::Registrar.new "service", EtcdDiscovery::Host.new({"name" => "example.com", "ports" => {"http" => 80}}) }
14
+ it "should defines the state to new" do
15
+ expect(subject.state).to eq :new
16
+ end
17
+ end
18
+ end
19
+
20
+ describe '#register' do
21
+ subject { EtcdDiscovery::Registrar.new "service", EtcdDiscovery::Host.new({"name" => "example.com", "ports" => {"http" => 80}}) }
22
+ it "should start a registering thread" do
23
+ subject.register
24
+ expect(subject.thread).not_to eq nil
25
+ expect(subject.thread.alive?).to eq true
26
+ end
27
+ end
28
+
29
+ describe '#stop' do
30
+ subject { EtcdDiscovery::Registrar.new "service", EtcdDiscovery::Host.new({"name" => "example.com", "ports" => {"http" => 80}}) }
31
+ it "should change the state to stopped" do
32
+ subject.stop
33
+ expect(subject.state).to eq :stopped
34
+ end
35
+
36
+ it "should stop its registering thread" do
37
+ subject.register
38
+ subject.stop
39
+ sleep 1
40
+ expect(subject.thread.alive?).to eq false
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,8 @@
1
+ require "etcd-discovery"
2
+
3
+ require "rubygems"
4
+ require "bundler/setup"
5
+ Bundler.require(:default, :development, :test)
6
+
7
+ EtcdDiscovery.config.register_renew = 0.5
8
+
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: etcd-discovery
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.11
4
+ version: 0.0.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Léo Unbekandt
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-03 00:00:00.000000000 Z
11
+ date: 2015-01-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: etcd
@@ -25,7 +25,7 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  description: Ruby implementation of a service discovery tool based on etcd
28
- email: leo.unbekandt@appsdeck.eu
28
+ email: leo.unbekandt@scalingo.com
29
29
  executables: []
30
30
  extensions: []
31
31
  extra_rdoc_files: []
@@ -34,8 +34,11 @@ files:
34
34
  - lib/etcd-discovery/client.rb
35
35
  - lib/etcd-discovery/config.rb
36
36
  - lib/etcd-discovery/host.rb
37
+ - lib/etcd-discovery/registrar.rb
37
38
  - lib/etcd-discovery/service.rb
38
- homepage: http://github.com/Appsdeck/etcd-discovery-ruby
39
+ - spec/etcd-discovery/registrar_spec.rb
40
+ - spec/spec_helper.rb
41
+ homepage: http://github.com/Scalingo/etcd-discovery-ruby
39
42
  licenses:
40
43
  - BSD
41
44
  metadata: {}
@@ -55,8 +58,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
55
58
  version: '0'
56
59
  requirements: []
57
60
  rubyforge_project:
58
- rubygems_version: 2.2.2
61
+ rubygems_version: 2.4.5
59
62
  signing_key:
60
63
  specification_version: 4
61
64
  summary: Service discovery based on etcd
62
65
  test_files: []
66
+ has_rdoc: