Class: Resque::Plugins::UniqueByArity

Inherits:
Module
  • Object
show all
Defined in:
lib/resque/plugins/unique_by_arity.rb

Overview

If you want your job to support uniqueness either at enqueue-time or at

runtime, or both, and you want that uniqueness based on a specific arity
of arguments, simply include this module into your job class.

NOTE: This module gets instantiated.

    It is a module - class hybrid.
    That's unconventional, and extremely powerful.

class EnqueueAndRunAlone
  @queue = :enqueue_and_run_alone

  def self.perform(arg1, arg2)
    alone_stuff
  end
  include Resque::Plugins::UniqueByArity.new(
    arity_for_uniqueness: 1,
    arity_validation: :warning, # or nil, false, or :error
    unique_at_runtime: true,
    unique_in_queue: true
  )
end

Instance Method Summary collapse

Constructor Details

#initialize(**config) ⇒ UniqueByArity

Returns a new instance of UniqueByArity.



26
27
28
# File 'lib/resque/plugins/unique_by_arity.rb', line 26

def initialize(**config)
  @configuration = Resque::UniqueByArity::Configuration.new(**config)
end

Instance Method Details

#included(base) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/resque/plugins/unique_by_arity.rb', line 30

def included(base)
  return unless @configuration

  @configuration.base_klass_name = base.to_s
  @configuration.validate
  base.send(:extend, Resque::UniqueByArity)
  base.uniqueness_config_reset(@configuration.dup)

  # gem is unique_in_queue, which is a rewrite of resque-loner
  # see: https://github.com/neighborland/resque_solo
  # defines a redis_key method, which we have to override.
  base.send(:include, Resque::Plugins::UniqueInQueue) if @configuration.unique_in_queue || @configuration.unique_across_queues

  # gem is resque-unique_at_runtime, which is a rewrite of resque-lonely_job
  # see: https://github.com/pboling/resque-unique_at_runtime
  base.send(:extend, Resque::Plugins::UniqueAtRuntime) if @configuration.unique_at_runtime

  uniqueness_cop_module = Resque::UniqueByArity::Modulizer.to_mod(@configuration)
  # This will override methods from both plugins above, if configured for both
  base.send(:extend, uniqueness_cop_module)

  base.include Resque::UniqueByArity::Validation unless @configuration.skip_arity_validation?
end