Module: Resque::Plugins::UniqueJob
- Includes:
- Helpers
- Defined in:
- lib/resque/plugins/unique_job.rb,
lib/resque/plugins/unique_job/version.rb
Constant Summary collapse
- Version =
'0.0.1'
Instance Method Summary collapse
- #before_perform_unique_job(*args) ⇒ Object
- #destroy_matching_keys(queue, args) ⇒ Object
- #enqueue(*args) ⇒ Object
- #unique_key(args) ⇒ Object
Instance Method Details
#before_perform_unique_job(*args) ⇒ Object
26 27 28 |
# File 'lib/resque/plugins/unique_job.rb', line 26 def before_perform_unique_job(*args) Resque.redis.del(unique_redis_key(args)) end |
#destroy_matching_keys(queue, args) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/resque/plugins/unique_job.rb', line 34 def destroy_matching_keys(queue, args) queue = "queue:#{queue}" klass = self.to_s if args.empty? redis.lrange(queue, 0, -1).each do |string| if decode(string)['class'] == klass key = unique_redis_key(decode(string)['args']) Resque.redis.del(key) end end else Resque.redis.del(unique_redis_key(args)) end end |
#enqueue(*args) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/resque/plugins/unique_job.rb', line 8 def enqueue(*args) key = unique_redis_key(args) ttl = Resque.redis.ttl(key) # if it's already enQ'd if Resque.redis.getset(key, "1") if expire = unique_redis_expiration # reset existing expiration Resque.redis.expire(key, ttl) end else # set new expiration if expire = unique_redis_expiration Resque.redis.expire(key, expire) end super end end |
#unique_key(args) ⇒ Object
30 31 32 |
# File 'lib/resque/plugins/unique_job.rb', line 30 def unique_key(args) Digest::MD5.hexdigest(encode(:class => self.to_s, :args => args)) end |