Class: Zlk::Lock

Inherits:
Object
  • Object
show all
Defined in:
lib/zlk/lock.rb

Constant Summary collapse

LockWaitTimeoutError =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Lock

Returns a new instance of Lock.



7
8
9
# File 'lib/zlk/lock.rb', line 7

def initialize(path)
  @path = sanitize_path(path)
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



5
6
7
# File 'lib/zlk/lock.rb', line 5

def path
  @path
end

Instance Method Details

#acquirable?Boolean

Returns:

  • (Boolean)


37
38
39
40
41
# File 'lib/zlk/lock.rb', line 37

def acquirable?
  with_locker do |locker|
    locker.acquirable?
  end
end

#run_exclusivelyObject



11
12
13
14
15
# File 'lib/zlk/lock.rb', line 11

def run_exclusively
  with_locker do |locker|
    locker.with_lock(wait: true) { yield }
  end
end

#with_timeout(timeout) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/zlk/lock.rb', line 17

def with_timeout(timeout)
  with_locker do |locker|
    begin
      locker.with_lock(wait: timeout) { yield(true) }
    rescue ZK::Exceptions::LockWaitTimeoutError => e
      yield(false)
    end
  end
end

#with_timeout!(timeout) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/zlk/lock.rb', line 27

def with_timeout!(timeout)
  with_timeout(timeout) do |locked|
    if locked
      yield
    else
      fail Zlk::Lock::LockWaitTimeoutError
    end
  end
end