Module: Resque::Plugins::UniqueInQueue::ClassMethods

Defined in:
lib/resque/plugins/unique_in_queue.rb

Instance Method Summary collapse

Instance Method Details

#lock_after_execution_periodObject

The default ttl of a persisting key is 0, i.e. immediately deleted. Set lock_after_execution_period to block the execution of the job for a certain amount of time (in seconds). For example:

class FooJob

include Resque::Plugins::UniqueInQueue
@lock_after_execution_period = 40

end



47
48
49
50
# File 'lib/resque/plugins/unique_in_queue.rb', line 47

def lock_after_execution_period
  instance_variable_get(:@lock_after_execution_period) ||
      instance_variable_set(:lock_after_execution_period, Resque::UniqueInQueue.configuration&.lock_after_execution_period)
end

#redis_key(payload) ⇒ Object

Payload is what Resque stored for this job along with the job’s class name: a hash containing string keys ‘class’ and ‘args’



27
28
29
30
31
32
33
34
35
36
# File 'lib/resque/plugins/unique_in_queue.rb', line 27

def redis_key(payload)
  payload = Resque.decode(Resque.encode(payload))
  job  = payload['class']
  args = payload['args']
  args.map! do |arg|
    arg.is_a?(Hash) ? arg.sort : arg
  end

  Digest::MD5.hexdigest Resque.encode(class: job, args: args)
end

#ttlObject

The default ttl of a locking key is -1 (forever). To expire the lock after a certain amount of time, set a ttl (in seconds). For example:

class FooJob

include Resque::Plugins::UniqueInQueue
@ttl = 40

end



60
61
62
63
# File 'lib/resque/plugins/unique_in_queue.rb', line 60

def ttl
  instance_variable_get(:@ttl) ||
      instance_variable_set(:ttl, Resque::UniqueInQueue.configuration&.ttl)
end

#unique_in_queue_key_baseObject

Should not generally be overridden per each class because it wouldn’t

make sense.

It wouldn’t be able to determine or enforce uniqueness across queues,

and general cleanup of stray keys would be nearly impossible.


69
70
71
72
# File 'lib/resque/plugins/unique_in_queue.rb', line 69

def unique_in_queue_key_base
  instance_variable_get(:@unique_in_queue_key_base) ||
      instance_variable_set(:@unique_in_queue_key_base, Resque::UniqueInQueue.configuration&.unique_in_queue_key_base)
end

#unique_in_queue_redis_key(queue, item) ⇒ Object



21
22
23
# File 'lib/resque/plugins/unique_in_queue.rb', line 21

def unique_in_queue_redis_key(queue, item)
  "#{unique_in_queue_key_base}:queue:#{queue}:job:#{Resque::UniqueInQueue::Queue.const_for(item).redis_key(item)}"
end