Class: Diplomat::Lock
- Inherits:
-
RestClient
- Object
- RestClient
- Diplomat::Lock
- Includes:
- ApiOptions
- Defined in:
- lib/diplomat/lock.rb
Overview
Methods for interacting with the Consul lock API endpoint
Instance Method Summary collapse
-
#acquire(key, session, value = nil, options = nil) ⇒ Boolean
Acquire a lock rubocop:disable AbcSize.
-
#release(key, session, options = nil) ⇒ nil
Release a lock.
-
#wait_to_acquire(key, session, value = nil, check_interval = 10, options = nil) ⇒ Boolean
wait to aquire a lock.
Methods included from ApiOptions
#check_acl_token, #use_cas, #use_consistency, #valid_transaction_verbs, #valid_value_transactions
Methods inherited from RestClient
access_method?, #concat_url, #initialize, method_missing, respond_to?, respond_to_missing?, #use_named_parameter
Constructor Details
This class inherits a constructor from Diplomat::RestClient
Instance Method Details
#acquire(key, session, value = nil, options = nil) ⇒ Boolean
Acquire a lock rubocop:disable AbcSize
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/diplomat/lock.rb', line 15 def acquire(key, session, value = nil, = nil) raw = @conn.put do |req| url = ["/v1/kv/#{key}"] url += use_named_parameter('acquire', session) url += check_acl_token url += use_named_parameter('dc', [:dc]) if && [:dc] req.url concat_url url req.body = value unless value.nil? end raw.body.chomp == 'true' end |
#release(key, session, options = nil) ⇒ nil
Release a lock
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/diplomat/lock.rb', line 50 def release(key, session, = nil) raw = @conn.put do |req| url = ["/v1/kv/#{key}"] url += use_named_parameter('release', session) url += check_acl_token url += use_named_parameter('dc', [:dc]) if && [:dc] req.url concat_url url end raw.body end |
#wait_to_acquire(key, session, value = nil, check_interval = 10, options = nil) ⇒ Boolean
wait to aquire a lock
36 37 38 39 40 41 42 43 |
# File 'lib/diplomat/lock.rb', line 36 def wait_to_acquire(key, session, value = nil, check_interval = 10, = nil) acquired = false until acquired acquired = acquire(key, session, value, ) sleep(check_interval) unless acquired return true if acquired end end |