etcd-discovery 0.0.12 → 0.1.0
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 +4 -4
- data/lib/etcd-discovery/registrar.rb +31 -6
- data/spec/etcd-discovery/registrar_spec.rb +44 -25
- data/spec/spec_helper.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9c01bf46ee0a9c4812bf9ac80fa816b6b5bcd541
|
4
|
+
data.tar.gz: 66c258afe4d3387737216e9c26fc7f1522d92d4e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4253017c7feb7d976bb37d0e0e81512decb5a10f4c56ce19b888fb11d41106a9e77d2fb7fbd7f9316f4962685078bbd2533c631dfb372ba96766638cadfd169a
|
7
|
+
data.tar.gz: c9d54aa78d9a5beeaa846a8a0fd1dab8ea9d14ea8fac04490d2468f1303e405ad39c46646c8e6e425fcfc43fdb042bae3ea36b81ccacefb6d2781fa75d5d974a
|
@@ -1,4 +1,15 @@
|
|
1
1
|
module EtcdDiscovery
|
2
|
+
class InvalidStateError < StandardError
|
3
|
+
def initialize(current, expected)
|
4
|
+
@current = current
|
5
|
+
@expected = expected
|
6
|
+
end
|
7
|
+
|
8
|
+
def message
|
9
|
+
"Registrar is #{@current}, expected #{@expected}"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
2
13
|
class Registrar
|
3
14
|
attr_reader :state
|
4
15
|
attr_reader :thread
|
@@ -21,27 +32,41 @@ module EtcdDiscovery
|
|
21
32
|
|
22
33
|
def register
|
23
34
|
@state = :started
|
24
|
-
config = EtcdDiscovery.config
|
25
|
-
client = config.client
|
26
35
|
value = @host.to_json
|
27
|
-
key_name = "/services/#{@service}/#{@host.attributes['name']}"
|
28
36
|
|
29
37
|
@thread = Thread.new {
|
38
|
+
@logger.warn "Register '#{@service}' started"
|
30
39
|
while @state == :started
|
31
40
|
begin
|
32
|
-
client.set(
|
41
|
+
client.set(key, value: value, ttl: config.register_ttl)
|
33
42
|
rescue => e
|
34
|
-
logger.warn "Fail to set #{
|
43
|
+
@logger.warn "Fail to set #{key}: #{e}, #{e.message}, #{e.class}"
|
35
44
|
end
|
36
45
|
sleep config.register_renew
|
37
46
|
end
|
38
|
-
logger.warn "Register '#{@service}' stopped"
|
47
|
+
@logger.warn "Register '#{@service}' stopped"
|
39
48
|
}
|
40
49
|
self
|
41
50
|
end
|
42
51
|
|
43
52
|
def stop
|
53
|
+
raise InvalidStateError.new(@state, :started) if @state != :started
|
54
|
+
@logger.debug "Set state to :stopped"
|
44
55
|
@state = :stopped
|
56
|
+
@logger.debug "Delete #{key}"
|
57
|
+
client.delete(key)
|
58
|
+
end
|
59
|
+
|
60
|
+
def client
|
61
|
+
config.client
|
62
|
+
end
|
63
|
+
|
64
|
+
def config
|
65
|
+
EtcdDiscovery.config
|
66
|
+
end
|
67
|
+
|
68
|
+
def key
|
69
|
+
"/services/#{@service}/#{@host.attributes['name']}"
|
45
70
|
end
|
46
71
|
end
|
47
72
|
end
|
@@ -1,43 +1,62 @@
|
|
1
1
|
require 'spec_helper.rb'
|
2
2
|
|
3
3
|
RSpec.describe EtcdDiscovery::Registrar do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
4
|
+
context "without a running client" do
|
5
|
+
describe '.new' do
|
6
|
+
context 'with a Hash' do
|
7
|
+
subject { EtcdDiscovery::Registrar.new "service", {"name" => "example.com", "ports" => {"http" => 80}} }
|
8
|
+
it "should transform the has in Host" do
|
9
|
+
expect(subject.host.attributes["name"]).to eq "example.com"
|
10
|
+
end
|
9
11
|
end
|
10
|
-
end
|
11
12
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
context 'with a EtcdDiscovery::Host' do
|
14
|
+
subject { EtcdDiscovery::Registrar.new "service", EtcdDiscovery::Host.new({"name" => "example.com", "ports" => {"http" => 80}}) }
|
15
|
+
it "should defines the state to new" do
|
16
|
+
expect(subject.state).to eq :new
|
17
|
+
end
|
16
18
|
end
|
17
19
|
end
|
18
20
|
end
|
19
21
|
|
20
|
-
|
22
|
+
context "with an unregistered client" do
|
21
23
|
subject { EtcdDiscovery::Registrar.new "service", EtcdDiscovery::Host.new({"name" => "example.com", "ports" => {"http" => 80}}) }
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
24
|
+
|
25
|
+
describe '#stop' do
|
26
|
+
it "should raise an exception if service not registered" do
|
27
|
+
expect{ subject.stop }.to raise_exception EtcdDiscovery::InvalidStateError
|
28
|
+
end
|
26
29
|
end
|
30
|
+
|
31
|
+
its(:client) { is_expected.to eq EtcdDiscovery.config.client }
|
32
|
+
its(:key) { is_expected.to eq "/services/service/example.com" }
|
27
33
|
end
|
28
34
|
|
29
|
-
|
30
|
-
subject { EtcdDiscovery::Registrar.new
|
31
|
-
|
32
|
-
|
33
|
-
|
35
|
+
context "with a registered client" do
|
36
|
+
subject { EtcdDiscovery::Registrar.new("service", EtcdDiscovery::Host.new({"name" => "example.com", "ports" => {"http" => 80}})) }
|
37
|
+
|
38
|
+
before(:each) do subject.register ; sleep 0.2 ; end
|
39
|
+
after(:each) do subject.stop if subject.state == :started ; end
|
40
|
+
|
41
|
+
describe '#register' do
|
42
|
+
its(:thread) { is_expected.not_to eq nil }
|
43
|
+
it "s thread should be alived" do
|
44
|
+
expect(subject.thread.alive?).to eq true
|
45
|
+
end
|
34
46
|
end
|
35
47
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
48
|
+
describe "#stop" do
|
49
|
+
it "should stop its registering thread" do
|
50
|
+
subject.stop
|
51
|
+
sleep 0.2
|
52
|
+
expect(subject.thread.alive?).to eq false
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should remote the etcd key of the service" do
|
56
|
+
expect(EtcdDiscovery.get("service").length).to eq 1
|
57
|
+
subject.stop
|
58
|
+
expect { EtcdDiscovery.get("service") }.to raise_exception EtcdDiscovery::ServiceNotFound
|
59
|
+
end
|
41
60
|
end
|
42
61
|
end
|
43
62
|
end
|
data/spec/spec_helper.rb
CHANGED
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
|
4
|
+
version: 0.1.0
|
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: 2015-01-
|
11
|
+
date: 2015-01-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: etcd
|