Module: HireFire::Macro::Delayed::Job

Extended by:
Job
Included in:
Job
Defined in:
lib/hirefire/macro/delayed_job.rb

Instance Method Summary collapse

Instance Method Details

#queue(*queues) ⇒ Integer

Returns the job quantity for the provided queue(s).

Examples:

Delayed::Job Macro Usage


# all queues using ActiveRecord mapper.
HireFire::Macro::Delayed::Job.queue(:mapper => :active_record)

# all queues using ActiveRecord <= 2.3.x mapper.
HireFire::Macro::Delayed::Job.queue(:mapper => :active_record_2)

# only "email" queue with Mongoid mapper.
HireFire::Macro::Delayed::Job.queue("email", :mapper => :mongoid)

# "audio" and "video" queues with ActiveRecord mapper.
HireFire::Macro::Delayed::Job.queue("audio", "video", :mapper => :active_record)

# all queues with a maximum priority of 20
HireFire::Macro::Delayed::Job.queue(:max_priority => 20, :mapper => :active_record)

# all queues with a minimum priority of 5
HireFire::Macro::Delayed::Job.queue(:min_priority => 5, :mapper => :active_record)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/hirefire/macro/delayed_job.rb', line 35

def queue(*queues)
  queues.flatten!

  if queues.last.is_a?(Hash)
    options = queues.pop
  else
    options = {}
  end

  case options[:mapper]
  when :active_record
    c = ::Delayed::Job
    c = c.where(:failed_at => nil)
    c = c.where("run_at <= ?", Time.now.utc)
    c = c.where("priority >= ?", options[:min_priority]) if options.key?(:min_priority)
    c = c.where("priority <= ?", options[:max_priority]) if options.key?(:max_priority)
    c = c.where(:queue => queues) unless queues.empty?
    c.count.tap { ActiveRecord::Base.clear_active_connections! }
  when :active_record_2
    c = ::Delayed::Job
    c = c.scoped(:conditions => ["run_at <= ? AND failed_at is NULL", Time.now.utc])
    c = c.scoped(:conditions => ["priority >= ?", options[:min_priority]]) if options.key?(:min_priority)
    c = c.scoped(:conditions => ["priority <= ?", options[:max_priority]]) if options.key?(:max_priority)
    # There is no queue column in delayed_job <= 2.x
    c.count.tap do
      if ActiveRecord::Base.respond_to?(:clear_active_connections!)
        ActiveRecord::Base.clear_active_connections!
      end
    end
  when :mongoid
    c = ::Delayed::Job
    c = c.where(:failed_at => nil)
    c = c.where(:run_at.lte => Time.now.utc)
    c = c.where(:priority.gte => options[:min_priority]) if options.key?(:min_priority)
    c = c.where(:priority.lte => options[:max_priority]) if options.key?(:max_priority)
    c = c.where(:queue.in => queues) unless queues.empty?
    c.count
  else
    raise %{Must pass in :mapper => :active_record or :mapper => :mongoid\n} +
      %{For example: HireFire::Macro::Delayed::Job.queue("worker", :mapper => :active_record)}
  end
end