module Resque::Single::ClassMethods
Public Instance Methods
after_dequeue_lock(*args)
click to toggle source
When job is dequeued we should remove lock
# File lib/resque/single.rb, line 90 def after_dequeue_lock(*args) unlock(*args) if args.any? end
after_dequeue_meta(*args)
click to toggle source
Fail metadata if dequeue succeed
# File lib/resque/single.rb, line 95 def after_dequeue_meta(*args) if (meta_id = args.first) && (meta = get_meta(meta_id)) meta.fail! end end
before_dequeue_lock(*args)
click to toggle source
Before dequeue check if job is running
# File lib/resque/single.rb, line 83 def before_dequeue_lock(*args) (meta_id = args.first) && (meta = get_meta(meta_id)) && !meta.working? end
dequeue(*args)
click to toggle source
Dequeue unique job
# File lib/resque/single.rb, line 120 def dequeue(*args) Resque.dequeue(self, meta_id(*args), *args) end
enqueued?(*args)
click to toggle source
Is job already in queue or in process?
# File lib/resque/single.rb, line 102 def enqueued?(*args) # if lock exists and timeout not exceeded if locked?(*args) get_meta(meta_id(*args)) else nil end end
execute(*)
click to toggle source
# File lib/resque/single.rb, line 73 def execute(*) raise NotImplementedError, "You should implement `execute' method" end
lock(meta_id, *args)
click to toggle source
LockID should be independent from MetaID
# File lib/resque/single.rb, line 54 def lock(meta_id, *args) "lock:#{name}-#{Digest::SHA1.hexdigest(obj_to_string(lock_on[*args]))}" end
lock_on(&block)
click to toggle source
Get or set proc returning unique arguments
# File lib/resque/single.rb, line 45 def lock_on(&block) if block_given? @unique = block else @unique ||= proc { |*args| args } end end
locked?(*args)
click to toggle source
Returns true if resque job is in locked state
# File lib/resque/single.rb, line 112 def locked?(*args) key = lock(nil, *args) now = Time.now.to_i Resque.redis.exists(key) && now <= Resque.redis.get(key).to_i end
meta()
click to toggle source
get meta object associated with job
# File lib/resque/single.rb, line 64 def meta get_meta(@meta_id) end
meta_id(*args)
click to toggle source
Overriding meta_id
here so now it generates the same MetaID for Jobs with same args
# File lib/resque/single.rb, line 59 def meta_id(*args) Digest::SHA1.hexdigest(['resque-unique', self, lock_on[*args]].join) end
on_failure_lock(e, *args)
click to toggle source
When job is failed we should remove lock
# File lib/resque/single.rb, line 78 def on_failure_lock(e, *args) unlock(*args) end
perform(meta_id, *args)
click to toggle source
default ‘perform` method override
# File lib/resque/single.rb, line 69 def perform(meta_id, *args) execute(*args) end
retry_args(*args)
click to toggle source
# File lib/resque/single.rb, line 39 def retry_args(*args) args.shift args end
scheduled(queue, klass, *args)
click to toggle source
# File lib/resque/single.rb, line 35 def scheduled(queue, klass, *args) klass.constantize.enqueue(*args) end
Private Instance Methods
obj_to_string(obj)
click to toggle source
# File lib/resque/single.rb, line 138 def obj_to_string(obj) case obj when Hash s = [] obj.keys.sort.each do |k| s << obj_to_string(k) s << obj_to_string(obj[k]) end s.to_s when Array s = [] obj.each { |a| s << obj_to_string(a) } s.to_s else obj.to_s end end
unlock(*args)
click to toggle source
Remove lock for job with given args
# File lib/resque/single.rb, line 134 def unlock(*args) Resque.redis.del(lock(*args)) end