module Resque::Plugins::Workers::Lock

Public Instance Methods

around_perform_workers_lock(*args) { || ... } click to toggle source
# File lib/resque/plugins/workers/lock.rb, line 80
def around_perform_workers_lock(*args)
  yield
ensure
  # Clear the lock. (even with errors)
  clear_workers_lock(*args)
end
before_perform_workers_lock(*args) click to toggle source

Called with the job args before perform. If it raises Resque::Job::DontPerform, the job is aborted.

# File lib/resque/plugins/workers/lock.rb, line 58
def before_perform_workers_lock(*args)
  if lock_workers(*args)
    lock_result = get_lock_workers(*args)

    if Resque.redis.msetnx lock_result.zip([true]*lock_result.size).flatten
      lock_result.each do |lock|
        Resque.redis.expire(lock, worker_lock_timeout(*args))
      end
    else
      reenqueue(*args)
    end
  end
end
clear_workers_lock(*args) click to toggle source
# File lib/resque/plugins/workers/lock.rb, line 72
def clear_workers_lock(*args)
  lock_result = get_lock_workers(*args)

  lock_result.each do |lock|
    Resque.redis.del(lock)
  end
end
get_lock_workers(*args) click to toggle source
# File lib/resque/plugins/workers/lock.rb, line 27
def get_lock_workers(*args)
  lock_result = lock_workers(*args)

  if lock_result.kind_of?(Array)
    lock_result.map do |lock|
      "workerslock:#{lock}"
    end
  else
    ["workerslock:#{lock_result}"]
  end
end
lock_workers(*args) click to toggle source

Override in your job to control the workers lock key(s).

# File lib/resque/plugins/workers/lock.rb, line 23
def lock_workers(*args)
  "#{name}-#{args.to_s}"
end
on_failure_workers_lock(exception, *args) click to toggle source
# File lib/resque/plugins/workers/lock.rb, line 87
def on_failure_workers_lock(exception, *args)
  # Clear the lock on DirtyExit
  clear_workers_lock(*args)
end
reenqueue(*args) click to toggle source

Override in your job to change the way how job is reenqueued

# File lib/resque/plugins/workers/lock.rb, line 45
def reenqueue(*args)
  if defined? Resque::Scheduler
    # schedule a job in requeue_perform_delay seconds
    Resque.enqueue_in(requeue_perform_delay, self, *args)
  else
    sleep(requeue_perform_delay)
    Resque.enqueue(self, *args)
  end
  raise Resque::Job::DontPerform
end
requeue_perform_delay() click to toggle source

Override in your job to change the perform requeue delay

# File lib/resque/plugins/workers/lock.rb, line 40
def requeue_perform_delay
  1.0
end
worker_lock_timeout(*) click to toggle source

Override in your job to control the worker lock experiation time. This is the time in seconds that the lock should be considered valid. The default is one hour (3600 seconds).

# File lib/resque/plugins/workers/lock.rb, line 18
def worker_lock_timeout(*)
  3600
end