Class: Diplomat::Lock

Inherits:
RestClient show all
Defined in:
lib/diplomat/lock.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from RestClient

#initialize

Constructor Details

This class inherits a constructor from Diplomat::RestClient

Class Method Details

.acquire(*args) ⇒ Object

Note:

This is sugar, see (#acquire)



46
47
48
# File 'lib/diplomat/lock.rb', line 46

def self.acquire *args
  Diplomat::Lock.new.acquire *args
end

.release(*args) ⇒ Object

Note:

This is sugar, see (#release)



56
57
58
# File 'lib/diplomat/lock.rb', line 56

def self.release *args
  Diplomat::Lock.new.release *args
end

.wait_to_acquire(*args) ⇒ Object

Note:

This is sugar, see (#wait_to_acquire)



51
52
53
# File 'lib/diplomat/lock.rb', line 51

def self.wait_to_acquire *args
  Diplomat::Lock.new.wait_to_acquire *args
end

Instance Method Details

#acquire(key, session) ⇒ Boolean

Acquire a lock



10
11
12
13
14
15
16
17
# 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) ⇒ nil

Release a lock



38
39
40
41
42
43
# 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) ⇒ Boolean

wait to aquire a lock



24
25
26
27
28
29
30
31
# 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