Class: TjScaleRuby::JobBackends::DelayedJob

Inherits:
Object
  • Object
show all
Defined in:
lib/tj_scale_ruby/job_backends/delayed_job.rb

Overview

Counts runnable Delayed::Job rows in the shared database. Requires the host app to bundle delayed_job_active_record.

Class Method Summary collapse

Class Method Details

.backend_nameObject



9
10
11
# File 'lib/tj_scale_ruby/job_backends/delayed_job.rb', line 9

def backend_name
  "delayed_job"
end

.oldest_pending_queue_seconds(now = Time.zone.now) ⇒ Object

Approximate age in seconds of the oldest runnable job (run_at vs now). Zero if none.



33
34
35
36
37
38
# File 'lib/tj_scale_ruby/job_backends/delayed_job.rb', line 33

def oldest_pending_queue_seconds(now = Time.zone.now)
  oldest = pending_jobs_relation(now).minimum(:run_at)
  return 0 if oldest.nil?

  [(now - oldest).to_f.ceil, 0].max
end

.pending_job_count(now = Time.zone.now) ⇒ Object



28
29
30
# File 'lib/tj_scale_ruby/job_backends/delayed_job.rb', line 28

def pending_job_count(now = Time.zone.now)
  pending_jobs_relation(now).count
end

.pending_jobs_relation(now = Time.zone.now) ⇒ Object

Relation for runnable Delayed::Job rows (priority filters applied).



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/tj_scale_ruby/job_backends/delayed_job.rb', line 14

def pending_jobs_relation(now = Time.zone.now)
  ensure_loaded!
  min_priority = TjScaleRuby.configuration.min_priority
  max_priority = TjScaleRuby.configuration.max_priority

  query = ::Delayed::Job.where("run_at + interval '5 seconds' < ?", now)
  query = query.where("failed_at IS NULL")
  query = query.where("locked_at IS NULL")
  query = query.where("priority >= ?", min_priority) unless min_priority.zero?
  query = query.where("priority <= ?", max_priority) unless max_priority.zero?

  query
end