Module: Resque::Plugins::ExtfaceLonelyDevice
- Included in:
- Billing::IssueFiscalDoc, Billing::IssuePrintDoc
- Defined in:
- lib/rescue/plugins/extface_lonely_device.rb
Constant Summary collapse
- LOCK_TIMEOUT =
5 days
60 * 60 * 24 * 5
Instance Method Summary collapse
- #around_perform(*args) ⇒ Object
- #before_perform(*args) ⇒ Object
- #can_lock_queue?(*args) ⇒ Boolean
- #lock_timeout ⇒ Object
-
#redis_key(*args) ⇒ Object
Overwrite this method to uniquely identify which mutex should be used for a resque worker.
- #reenqueue(*args) ⇒ Object
- #requeue_interval ⇒ Object
- #unlock_queue(*args) ⇒ Object
Instance Method Details
#around_perform(*args) ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/rescue/plugins/extface_lonely_device.rb', line 58 def around_perform(*args) begin yield ensure unlock_queue(*args) end end |
#before_perform(*args) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/rescue/plugins/extface_lonely_device.rb', line 45 def before_perform(*args) unless can_lock_queue?(*args) # Sleep so the CPU's rest sleep(requeue_interval) # can't get the lock, so re-enqueue the task reenqueue(*args) # and don't perform raise Resque::Job::DontPerform end end |
#can_lock_queue?(*args) ⇒ Boolean
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/rescue/plugins/extface_lonely_device.rb', line 20 def can_lock_queue?(*args) now = Time.now.to_i key = redis_key(*args) timeout = lock_timeout # Per http://redis.io/commands/setnx return true if Resque.redis.setnx(key, timeout) return false if Resque.redis.get(key).to_i > now return true if Resque.redis.getset(key, timeout).to_i <= now return false rescue ActiveRecord::RecordNotFound #redis_key exception p "Not found!!!" sleep 1 reenqueue(*args) #will stop if new redis_key exception end |
#lock_timeout ⇒ Object
6 7 8 |
# File 'lib/rescue/plugins/extface_lonely_device.rb', line 6 def lock_timeout Time.now.to_i + LOCK_TIMEOUT + 1 end |
#redis_key(*args) ⇒ Object
Overwrite this method to uniquely identify which mutex should be used for a resque worker.
16 17 18 |
# File 'lib/rescue/plugins/extface_lonely_device.rb', line 16 def redis_key(*args) "extface_" end |
#reenqueue(*args) ⇒ Object
40 41 42 43 |
# File 'lib/rescue/plugins/extface_lonely_device.rb', line 40 def reenqueue(*args) Resque.enqueue_to(redis_key(*args), self, *args) #Resque.redis.lpush("queue:#{Resque.queue_from_class(self)}", Resque.encode(class: self, args: args)) end |
#requeue_interval ⇒ Object
10 11 12 |
# File 'lib/rescue/plugins/extface_lonely_device.rb', line 10 def requeue_interval self.instance_variable_get(:@requeue_interval) || 1 end |
#unlock_queue(*args) ⇒ Object
36 37 38 |
# File 'lib/rescue/plugins/extface_lonely_device.rb', line 36 def unlock_queue(*args) Resque.redis.del(redis_key(*args)) end |