Module: Shipitron::ConsulLock

Extended by:
ConsulLock
Included in:
ConsulLock, Server::DeployApplication
Defined in:
lib/shipitron/consul_lock.rb

Defined Under Namespace

Classes: UnableToLock

Instance Method Summary collapse

Instance Method Details

#with_lock(key:) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/shipitron/consul_lock.rb', line 9

def with_lock(key:)
  sessionid = nil
  locked = false
  sessionid = Diplomat::Session.create(Name: "#{key}.lock")
  locked = Diplomat::Lock.acquire(key, sessionid)

  if locked
    yield
  else
    raise UnableToLock
  end
ensure
  if sessionid != nil
    Diplomat::Lock.release(key, sessionid) if locked
    Diplomat::Session.destroy(sessionid)
  end
end