class Diplomat::Lock
Methods for interacting with the Consul lock API endpoint
Public Instance Methods
Acquire a lock @param key [String] the key @param session [String] the session, generated from Diplomat::Session.create
@param value [String] the value for the key @param options [Hash] :dc string for dc specific query @return [Boolean] If the lock was acquired rubocop:disable AbcSize
# File lib/diplomat/lock.rb, line 15 def acquire(key, session, value = nil, options = 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', options[:dc]) if options && options[:dc] req.url concat_url url req.body = value unless value.nil? end raw.body.chomp == 'true' end
Release a lock @param key [String] the key @param session [String] the session, generated from Diplomat::Session.create
@param options [Hash] :dc string for dc specific query @return [nil]
# File lib/diplomat/lock.rb, line 50 def release(key, session, options = 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', options[:dc]) if options && options[:dc] req.url concat_url url end raw.body end
wait to aquire a lock @param key [String] the key @param session [String] the session, generated from Diplomat::Session.create
@param value [String] the value for the key @param check_interval [Integer] number of seconds to wait between retries @param options [Hash] :dc string for dc specific query @return [Boolean] If the lock was acquired
# File lib/diplomat/lock.rb, line 36 def wait_to_acquire(key, session, value = nil, check_interval = 10, options = nil) acquired = false until acquired acquired = acquire(key, session, value, options) sleep(check_interval) unless acquired return true if acquired end end