Module: TResque::WorkerLock

Included in:
DelayExecutionWorker
Defined in:
lib/tresque/worker_lock.rb

Overview

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

Instance Method Summary collapse

Instance Method Details

#around_perform_worker_lock(options) ⇒ Object



40
41
42
43
44
45
# 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) ⇒ Object

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



20
21
22
23
24
25
26
27
28
29
30
31
# 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) ⇒ Object



33
34
35
36
37
38
# 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) ⇒ Object



47
48
49
50
# 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_timeoutObject

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).



9
10
11
# File 'lib/tresque/worker_lock.rb', line 9

def worker_lock_timeout
  3600
end