Module: Delayed::Backend::Base::ClassMethods

Defined in:
lib/delayed_job_unique_key/base.rb

Instance Method Summary collapse

Instance Method Details

#enqueue(*args) ⇒ Object

Add a job to the queue



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/delayed_job_unique_key/base.rb', line 10

def enqueue(*args)
  options = {
      :priority => Delayed::Worker.default_priority
  }.merge!(args.extract_options!)

  options[:payload_object] ||= args.shift

  if args.size > 0
    warn "[DEPRECATION] Passing multiple arguments to `#enqueue` is deprecated. Pass a hash with :priority and :run_at."
    options[:priority] = args.first || options[:priority]
    options[:unique_key] = options[:unique_key]
    options[:run_at] = args[1]
  end

  unless options[:payload_object].respond_to?(:perform)
    raise ArgumentError, 'Cannot enqueue items which do not respond to perform'
  end

  if Delayed::Worker.delay_jobs
    self.new(options).tap do |job|
      Delayed::Worker.lifecycle.run_callbacks(:enqueue, job) do
        job.hook(:enqueue)
        job.save
      end
    end
  else
    Delayed::Job.new(:payload_object => options[:payload_object]).tap do |job|
      job.invoke_job
    end
  end
end