Module: TResque::QueueLock
- Included in:
- DelayExecutionWorker
- Defined in:
- lib/tresque/queue_lock.rb
Overview
If you want only one instance of your job queued at a time, extend it with this module.
Instance Method Summary collapse
- #after_dequeue_queue_lock(options) ⇒ Object
-
#before_enqueue_queue_lock(options) ⇒ Object
See the documentation for SETNX redis.io/commands/setnx for an explanation of this deadlock free locking pattern.
- #before_perform_queue_lock(options) ⇒ Object
- #clear_queue_lock(options) ⇒ Object
-
#queue_lock_timeout ⇒ Object
Override in your job to control the lock experiation time.
Instance Method Details
#after_dequeue_queue_lock(options) ⇒ Object
66 67 68 |
# File 'lib/tresque/queue_lock.rb', line 66 def after_dequeue_queue_lock() clear_queue_lock() end |
#before_enqueue_queue_lock(options) ⇒ Object
See the documentation for SETNX redis.io/commands/setnx for an explanation of this deadlock free locking pattern
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/tresque/queue_lock.rb', line 22 def before_enqueue_queue_lock() val = queue_lock_key() if val key = "lock:#{val}" now = Time.now.to_i from_now = queue_lock_timeout + 1 key_expire = from_now + 600 # some exra time timeout = now + from_now # return true if we successfully acquired the lock if Resque.redis.setnx(key, timeout) # expire in case of error to make sure it goes away Resque.redis.expire(key, key_expire) return true end # see if the existing timeout is still valid and return false if it is # (we cannot acquire the lock during the timeout period) return false if now <= Resque.redis.get(key).to_i # otherwise set the timeout and ensure that no other worker has # acquired the lock if now > Resque.redis.getset(key, timeout).to_i # expire in case of error to make sure it goes away Resque.redis.expire(key, key_expire) return true else return false end end end |
#before_perform_queue_lock(options) ⇒ Object
62 63 64 |
# File 'lib/tresque/queue_lock.rb', line 62 def before_perform_queue_lock() clear_queue_lock() end |
#clear_queue_lock(options) ⇒ Object
55 56 57 58 59 60 |
# File 'lib/tresque/queue_lock.rb', line 55 def clear_queue_lock() val = queue_lock_key() if val Resque.redis.del("lock:#{val}") end end |
#queue_lock_timeout ⇒ Object
Override in your job to control the lock experiation time. This is the time in seconds that the lock should be considered valid. The default is one hour (3600 seconds).
9 10 11 |
# File 'lib/tresque/queue_lock.rb', line 9 def queue_lock_timeout 3600 end |