consul-rb 0.0.2 → 0.0.4
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/consul/client/http.rb +38 -15
- data/lib/consul/kv.rb +5 -0
- data/lib/consul/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4dba1c408a1ba96cbe19fa128f36c912f6e15747
|
4
|
+
data.tar.gz: a63f2fe473601e515155ea70c68312db8ac5a4ee
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5654b4a3e177648bba8a11e241a6b54463e5b1dd52f4ac3bc4eca078b2324a572ac1431d9aaf8e5dee5c828f96faae572a4562ffd0faa31c3088b49d84443189
|
7
|
+
data.tar.gz: 609103b6d2e859f6efe27f14e7380be248ed31dc1863e2ed8a0abf39ed28f5f778f5cd05ab36585eda1fcbb4ad8385863291d172eb4aa3b82bcb4403af30e24c
|
data/lib/consul/client/http.rb
CHANGED
@@ -8,6 +8,8 @@ module Consul
|
|
8
8
|
NotFoundException = Class.new(StandardError)
|
9
9
|
ResponseException = Class.new(StandardError)
|
10
10
|
RpcErrorException = Class.new(StandardError)
|
11
|
+
TimeoutException = Class.new(StandardError)
|
12
|
+
InavlidArgument = Class.new(StandardError)
|
11
13
|
|
12
14
|
class HTTP
|
13
15
|
def initialize(host:, port:, logger:)
|
@@ -16,15 +18,18 @@ module Consul
|
|
16
18
|
@logger = logger
|
17
19
|
end
|
18
20
|
|
19
|
-
def get(request_uri)
|
21
|
+
def get(request_uri, recurse: false, raw: false)
|
20
22
|
url = base_uri + request_uri
|
21
23
|
logger.debug("GET #{url}")
|
22
24
|
|
23
25
|
uri = URI.parse(url)
|
26
|
+
uri.query ||= ""
|
27
|
+
uri.query += "&recurse" if recurse
|
28
|
+
uri.query += "&raw" if raw
|
24
29
|
|
25
30
|
response = http_request(:get, uri)
|
26
31
|
|
27
|
-
parse_body(response)
|
32
|
+
raw ? response.body : parse_body(response)
|
28
33
|
end
|
29
34
|
|
30
35
|
def exists?(request_uri)
|
@@ -49,17 +54,29 @@ module Consul
|
|
49
54
|
uri.query += "&index=#{index}&wait=#{wait}"
|
50
55
|
uri.query += "&recurse" if recurse
|
51
56
|
|
57
|
+
wait_timeout = 0
|
58
|
+
case wait
|
59
|
+
when /^(.+)s$/
|
60
|
+
wait_timeout = $1.to_i + 5
|
61
|
+
when /^(.+)m$/
|
62
|
+
wait_timeout = ($1.to_i * 60) + 5
|
63
|
+
else
|
64
|
+
raise InavlidArgument
|
65
|
+
end
|
66
|
+
|
52
67
|
while true do
|
53
68
|
begin
|
54
69
|
logger.debug("GET #{uri}")
|
55
70
|
|
56
|
-
response = http_request(:get, uri)
|
71
|
+
response = http_request(:get, uri, nil, wait_timeout)
|
57
72
|
if response['x-consul-index'].to_i > index
|
58
|
-
return { index: response['x-consul-index'], body: parse_body(response) }
|
73
|
+
return { index: response['x-consul-index'].to_i, body: parse_body(response) }
|
59
74
|
end
|
60
75
|
rescue RpcErrorException
|
61
76
|
logger.warn("Got 500 while watching #{uri}")
|
62
77
|
sleep 5
|
78
|
+
rescue TimeoutException
|
79
|
+
logger.warn("Got timeout while watching #{uri}")
|
63
80
|
end
|
64
81
|
end
|
65
82
|
end
|
@@ -87,23 +104,29 @@ module Consul
|
|
87
104
|
JSON.parse("[#{response.body}]")[0]
|
88
105
|
end
|
89
106
|
|
90
|
-
def http_request(method, uri, data = nil)
|
107
|
+
def http_request(method, uri, data = nil, timeout = 60)
|
91
108
|
method = {
|
92
109
|
get: Net::HTTP::Get,
|
93
110
|
put: Net::HTTP::Put,
|
94
111
|
}.fetch(method)
|
95
112
|
|
96
|
-
http
|
97
|
-
|
113
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
114
|
+
http.read_timeout = timeout
|
115
|
+
request = method.new(uri.request_uri)
|
98
116
|
request.body = data
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
117
|
+
|
118
|
+
begin
|
119
|
+
response = http.request(request)
|
120
|
+
|
121
|
+
if response.code.to_i == 404
|
122
|
+
raise NotFoundException, "#{response.code} on #{uri}"
|
123
|
+
elsif response.code.to_i >= 500
|
124
|
+
raise RpcErrorException, "#{response.code} on #{uri}"
|
125
|
+
elsif response.code.to_i >= 400
|
126
|
+
raise ResponseException, "#{response.code} on #{uri}"
|
127
|
+
end
|
128
|
+
rescue Net::ReadTimeout
|
129
|
+
raise TimeoutException, "timeout on #{uri}"
|
107
130
|
end
|
108
131
|
|
109
132
|
response
|
data/lib/consul/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: consul-rb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexander Piavlo
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-10-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake-compiler
|
@@ -35,6 +35,7 @@ files:
|
|
35
35
|
- consul-rb.gemspec
|
36
36
|
- lib/consul/client.rb
|
37
37
|
- lib/consul/client/http.rb
|
38
|
+
- lib/consul/kv.rb
|
38
39
|
- lib/consul/version.rb
|
39
40
|
homepage: https://github.com/SupersonicAds/consul-rb
|
40
41
|
licenses:
|