Class: Findable::Query::Lock

Inherits:
Object
  • Object
show all
Includes:
Connection
Defined in:
lib/findable/query/lock.rb

Instance Method Summary collapse

Methods included from Connection

#redis

Constructor Details

#initialize(lock_key, thread_key, options = {}) ⇒ Lock

Returns a new instance of Lock.



8
9
10
11
12
# File 'lib/findable/query/lock.rb', line 8

def initialize(lock_key, thread_key, options = {})
  @lock_key = lock_key
  @thread_key = thread_key
  @options = options.symbolize_keys!
end

Instance Method Details

#lockObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/findable/query/lock.rb', line 14

def lock
  if Thread.current[@thread_key]
    yield
  else
    Thread.current[@thread_key] = true
    try_lock!(Time.current)
    begin
      yield
    ensure
      Thread.current[@thread_key] = nil
      unlock!
    end
  end
end

#unlock!Object



29
30
31
# File 'lib/findable/query/lock.rb', line 29

def unlock!
  redis.del(@lock_key)
end