Module: Arj::Query::ClassMethods

Defined in:
lib/arj/query.rb

Overview

Class methods which are automatically added when Arj::Query is included in a class.

Instance Method Summary collapse

Instance Method Details

#allRelation

Returns a Relation scope object for all jobs.

Returns:



51
52
53
54
55
56
57
58
59
# File 'lib/arj/query.rb', line 51

def all
  raise 'no record class configured' unless Arj.record_class

  if self == Arj
    Relation.new(Arj.record_class.all)
  else
    Relation.new(Arj.record_class.where(job_class: name).all)
  end
end

#available(queue_name: nil, max_executions: nil) ⇒ ActiveJob::Base

Returns a Relation scope for available jobs.

Jobs are considered available if scheduled_at is null or in the past. Jobs are ordered by priority (ascending, nulls last) followed by created_at (ascending).

The following optional criteria may be specified:

  • One or more queues

  • The maximum number of executions (exclusive)

Returns:

  • (ActiveJob::Base)


71
72
73
74
75
76
# File 'lib/arj/query.rb', line 71

def available(queue_name: nil, max_executions: nil)
  relation = where('scheduled_at is null or scheduled_at < ?', Time.zone.now)
  relation = relation.where(queue_name:) if queue_name
  relation = relation.where('executions < ?', max_executions) if max_executions
  relation.order(Arel.sql('CASE WHEN priority IS NULL THEN 1 ELSE 0 END, priority, created_at'))
end