module TResque::WorkerLock

If you want only one instance of your job running at a time, extend it with this module.

Public Instance Methods

around_perform_worker_lock(options) { || ... } click to toggle source
# File lib/tresque/worker_lock.rb, line 40
def around_perform_worker_lock(options)
  yield
ensure
  # Clear the lock. (even with errors)
  clear_worker_lock(options)
end
before_perform_worker_lock(options) click to toggle source

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

# File lib/tresque/worker_lock.rb, line 20
def before_perform_worker_lock(options)
  val = worker_lock_key(options)
  if val
    key = "workerslock:#{val}"
    if Resque.redis.setnx(key, true)
      Resque.redis.expire(key, worker_lock_timeout)
    else
      obj = self.new(options)
      obj.requeue!
    end
  end
end
clear_worker_lock(options) click to toggle source
# File lib/tresque/worker_lock.rb, line 33
def clear_worker_lock(options)
  val = worker_lock_key(options)
  if val
    Resque.redis.del("workerslock:#{val}")
  end
end
on_failure_worker_lock(exception, options) click to toggle source
# File lib/tresque/worker_lock.rb, line 47
def on_failure_worker_lock(exception, options)
  # Clear the lock on DirtyExit
  clear_worker_lock(options)
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/tresque/worker_lock.rb, line 9
def worker_lock_timeout
  3600
end