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.



36
37
38
39
40
41
# File 'lib/tj_scale_ruby/job_backends/delayed_job.rb', line 36

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



31
32
33
# File 'lib/tj_scale_ruby/job_backends/delayed_job.rb', line 31

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
27
28
29
# 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

  # 5-second buffer past run_at. Compute the cutoff in Ruby (run_at < now - 5s)
  # instead of a SQL "interval" literal, which is PostgreSQL-only and would raise
  # on MySQL/SQLite — silently masking the whole backlog as 0.
  query = ::Delayed::Job.where("run_at < ?", now - 5)
  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