Class: RedisLock::Semaphore

Inherits:
Object
  • Object
show all
Defined in:
lib/redis_lock/semaphore.rb

Direct Known Subclasses

IfLocked, IfOpen

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lock, args = {}) ⇒ Semaphore

Returns a new instance of Semaphore.



5
6
7
8
# File 'lib/redis_lock/semaphore.rb', line 5

def initialize(lock, args = {})
  @lock = lock
  @args = args
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



3
4
5
# File 'lib/redis_lock/semaphore.rb', line 3

def args
  @args
end

#lockObject (readonly)

Returns the value of attribute lock.



3
4
5
# File 'lib/redis_lock/semaphore.rb', line 3

def lock
  @lock
end

Instance Method Details

#call(&block) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/redis_lock/semaphore.rb', line 10

def call(&block)
  ttl = args[:ttl] || lock.config.default_ttl
  set_opts = args[:set_opts] || {}
  while lock.locked?
    sleep (args[:wait] || 1)
  end
  lock.set(ttl, set_opts)
  out = _perform(&block)
  lock.unlock!
  out
end