diplomat 0.5.0 → 0.5.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -2
- data/lib/diplomat/lock.rb +7 -6
- data/lib/diplomat/session.rb +5 -3
- data/lib/diplomat/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3f1c9c12e4e39b571bceb79ece16ea11b65e86a1
|
4
|
+
data.tar.gz: 20aab29fd76ec1ccca03f7051a2055d90e2e7c47
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 90217e6d23c9fa287a1d53a0e6d9d4abb7e2700222f232b397e154004994880160a0ca97ae27019ba0811f2e054d190d61542a20292fa7c670ed9feeb847e7e8
|
7
|
+
data.tar.gz: 9666756e0d00d492342a4f5898885d459724c8fdbfe5fc36754abcab80fa11faa1a9d4819191183b96a94fff4ae3fa5630fc05708ff58dca12bb0eea31336280
|
data/README.md
CHANGED
@@ -87,7 +87,7 @@ foo_service = Diplomat::Service.get('foo', :all)
|
|
87
87
|
Creating a session:
|
88
88
|
|
89
89
|
```ruby
|
90
|
-
sessionid = Diplomat::Session.create({:
|
90
|
+
sessionid = Diplomat::Session.create({:Node => "server1", :Name => "my-lock"})
|
91
91
|
# => "fc5ca01a-c317-39ea-05e8-221da00d3a12"
|
92
92
|
```
|
93
93
|
Or destroying a session:
|
@@ -101,7 +101,7 @@ Diplomat::Session.destroy("fc5ca01a-c317-39ea-05e8-221da00d3a12")
|
|
101
101
|
Acquire a lock:
|
102
102
|
|
103
103
|
```ruby
|
104
|
-
sessionid = Diplomat::Session.create({:
|
104
|
+
sessionid = Diplomat::Session.create({:Node => "server1", :Name => "my-lock"})
|
105
105
|
lock_acquired = Diplomat::Lock.acquire("/key/to/lock", sessionid)
|
106
106
|
# => true
|
107
107
|
```
|
data/lib/diplomat/lock.rb
CHANGED
@@ -6,25 +6,26 @@ module Diplomat
|
|
6
6
|
# Acquire a lock
|
7
7
|
# @param key [String] the key
|
8
8
|
# @param session [String] the session, generated from Diplomat::Session.create
|
9
|
+
# @param value [String] the value for the key
|
9
10
|
# @return [Boolean] If the lock was acquired
|
10
|
-
def acquire key, session
|
11
|
+
def acquire key, session, value=nil
|
11
12
|
raw = @conn.put do |req|
|
12
13
|
req.url "/v1/kv/#{key}?acquire=#{session}"
|
14
|
+
req.body = value unless value.nil?
|
13
15
|
end
|
14
|
-
|
15
|
-
return false
|
16
|
-
|
16
|
+
raw.body == 'true'
|
17
17
|
end
|
18
18
|
|
19
19
|
# wait to aquire a lock
|
20
20
|
# @param key [String] the key
|
21
21
|
# @param session [String] the session, generated from Diplomat::Session.create
|
22
|
+
# @param value [String] the value for the key
|
22
23
|
# @param check_interval [Integer] number of seconds to wait between retries
|
23
24
|
# @return [Boolean] If the lock was acquired
|
24
|
-
def wait_to_acquire key, session, check_interval=10
|
25
|
+
def wait_to_acquire key, session, value=nil, check_interval=10
|
25
26
|
acquired = false
|
26
27
|
while !acquired
|
27
|
-
acquired = self.acquire key, session
|
28
|
+
acquired = self.acquire key, session, value
|
28
29
|
sleep(check_interval) if !acquired
|
29
30
|
return true if acquired
|
30
31
|
end
|
data/lib/diplomat/session.rb
CHANGED
@@ -4,12 +4,14 @@ module Diplomat
|
|
4
4
|
class Session < Diplomat::RestClient
|
5
5
|
|
6
6
|
# Create a new session
|
7
|
-
# @param value [
|
7
|
+
# @param value [Object] hash or json representation of the session arguments
|
8
8
|
# @return [String] The sesssion id
|
9
|
-
def create value
|
9
|
+
def create value=nil
|
10
|
+
# TODO: only certain keys are recognised in a session create request,
|
11
|
+
# should raise an error on others.
|
10
12
|
raw = @conn.put do |req|
|
11
13
|
req.url "/v1/session/create"
|
12
|
-
req.body = value
|
14
|
+
req.body = (if value.kind_of?(String) then value else JSON.generate(value) end) unless value.nil?
|
13
15
|
end
|
14
16
|
body = JSON.parse(raw.body)
|
15
17
|
return body["ID"]
|
data/lib/diplomat/version.rb
CHANGED