Class: JustOneLock::BlockingLocker

Inherits:
BaseLocker show all
Defined in:
lib/just_one_lock/blocking_locker.rb

Constant Summary collapse

DEFAULT_TIMEOUT =
1

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseLocker

#already_locked

Constructor Details

#initialize(timeout: DEFAULT_TIMEOUT) ⇒ BlockingLocker

Returns a new instance of BlockingLocker.



8
9
10
# File 'lib/just_one_lock/blocking_locker.rb', line 8

def initialize(timeout: DEFAULT_TIMEOUT)
  @timeout = timeout
end

Instance Attribute Details

#timeoutObject

Returns the value of attribute timeout.



6
7
8
# File 'lib/just_one_lock/blocking_locker.rb', line 6

def timeout
  @timeout
end

Instance Method Details

#lock(lock_path, &block) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/just_one_lock/blocking_locker.rb', line 12

def lock(lock_path, &block)
  result = nil

  File.open(lock_path, File::RDWR|File::CREAT, 0644) do |f|
    Timeout::timeout(@timeout, JustOneLock::AlreadyLocked) { f.flock(File::LOCK_EX) }

    result = run(f, lock_path, &block)
  end

  result
end