Module: Resque::Plugins::Queue::Lock

Defined in:
lib/resque/plugins/queue/lock.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.all_queue_locksObject



55
56
57
# File 'lib/resque/plugins/queue/lock.rb', line 55

def self.all_queue_locks
  Resque.redis.keys('queuelock:*')
end

.clear_all_queue_locksObject



59
60
61
# File 'lib/resque/plugins/queue/lock.rb', line 59

def self.clear_all_queue_locks
  all_queue_locks.each{ |queue_lock| Resque.redis.del(queue_lock) }
end

Instance Method Details

#_acquire_lock(*args) ⇒ Object



31
32
33
34
35
# File 'lib/resque/plugins/queue/lock.rb', line 31

def _acquire_lock(*args)
  _reliably do
    Resque.redis.setnx( _namespaced_queue_lock(*args), Time.now )
  end
end

#_namespaced_queue_lock(*args) ⇒ Object



37
38
39
40
# File 'lib/resque/plugins/queue/lock.rb', line 37

def _namespaced_queue_lock(*args)
  lock_name = queue_lock( *Resque::Job.decode(Resque::Job.encode(args)) )
  "queuelock:#{lock_name}"
end

#_release_lock(*args) ⇒ Object



25
26
27
28
29
# File 'lib/resque/plugins/queue/lock.rb', line 25

def _release_lock(*args)
  _reliably do
    Resque.redis.del( _namespaced_queue_lock(*args) )
  end
end

#_reliablyObject



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/resque/plugins/queue/lock.rb', line 42

def _reliably
  tries = 0
  begin
    tries += 1
    yield
  rescue Redis::CannotConnectError
    if tries < 30
      sleep tries
      retry
    end
  end
end

#before_dequeue__queue_lock(*args) ⇒ Object



16
17
18
# File 'lib/resque/plugins/queue/lock.rb', line 16

def before_dequeue__queue_lock(*args)
  _release_lock(*args)
end

#before_enqueue__queue_lock(*args) ⇒ Object



12
13
14
# File 'lib/resque/plugins/queue/lock.rb', line 12

def before_enqueue__queue_lock(*args)
  _acquire_lock(*args)
end

#before_perform__queue_lock(*args) ⇒ Object



20
21
22
# File 'lib/resque/plugins/queue/lock.rb', line 20

def before_perform__queue_lock(*args)
  _release_lock(*args)
end

#queue_lock(*args) ⇒ Object

Override in your job to control the lock key. It is passed the same arguments as ‘perform`, that is, your job’s payload.



8
9
10
# File 'lib/resque/plugins/queue/lock.rb', line 8

def queue_lock(*args)
  "#{name}-#{args.to_s}"
end