class Diplomat::Lock

Public Class Methods

acquire(*args) click to toggle source

@note This is sugar, see (acquire)

# File lib/diplomat/lock.rb, line 46
def self.acquire *args
  Diplomat::Lock.new.acquire *args
end
release(*args) click to toggle source

@note This is sugar, see (release)

# File lib/diplomat/lock.rb, line 56
def self.release *args
  Diplomat::Lock.new.release *args
end
wait_to_acquire(*args) click to toggle source

@note This is sugar, see (wait_to_acquire)

# File lib/diplomat/lock.rb, line 51
def self.wait_to_acquire *args
  Diplomat::Lock.new.wait_to_acquire *args
end

Public Instance Methods

acquire(key, session) click to toggle source

Acquire a lock @param key [String] the key @param session [String] the session, generated from Diplomat::Session.create @return [Boolean] If the lock was acquired

# File lib/diplomat/lock.rb, line 10
def acquire key, session
  raw = @conn.put do |req|
    req.url "/v1/kv/#{key}?acquire=#{session}"
  end
  return true if raw.body == 'true'
  return false

end
release(key, session) click to toggle source

Release a lock @param key [String] the key @param session [String] the session, generated from Diplomat::Session.create @return [nil]

# File lib/diplomat/lock.rb, line 38
def release  key, session
  raw = @conn.put do |req|
    req.url "/v1/kv/#{key}?release=#{session}"
  end
  return raw.body
end
wait_to_acquire(key, session, check_interval=10) click to toggle source

wait to aquire a lock @param key [String] the key @param session [String] the session, generated from Diplomat::Session.create @param check_interval [Integer] number of seconds to wait between retries @return [Boolean] If the lock was acquired

# File lib/diplomat/lock.rb, line 24
def wait_to_acquire key, session, check_interval=10
  acquired = false
  while !acquired
    acquired = self.acquire key, session
    sleep(check_interval) if !acquired
    return true if acquired
  end
end