Class: Newshound::Jobs::Que

Inherits:
Base
  • Object
show all
Defined in:
lib/newshound/jobs/que.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#format_for_banner

Constructor Details

#initialize(logger: nil) ⇒ Que

Returns a new instance of Que.



8
9
10
# File 'lib/newshound/jobs/que.rb', line 8

def initialize(logger: nil)
  @logger = logger || (defined?(Rails) ? Rails.logger : Logger.new($stdout))
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



6
7
8
# File 'lib/newshound/jobs/que.rb', line 6

def logger
  @logger
end

Instance Method Details

#job_counts_by_typeObject



33
34
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
# File 'lib/newshound/jobs/que.rb', line 33

def job_counts_by_type
  results = ActiveRecord::Base.connection.execute(<<~SQL)
    SELECT job_class,
           COUNT(*) FILTER (WHERE error_count = 0 AND expired_at IS NULL) AS success,
           COUNT(*) FILTER (WHERE error_count > 0 AND expired_at IS NULL) AS failing,
           COUNT(*) FILTER (WHERE expired_at IS NOT NULL) AS expired,
           COUNT(*) AS total
    FROM que_jobs
    WHERE finished_at IS NULL
    GROUP BY job_class
    ORDER BY job_class
  SQL

  results.each_with_object({}) do |row, hash|
    failing = row["failing"].to_i
    expired = row["expired"].to_i

    hash[row["job_class"]] = {
      success: row["success"].to_i,
      failing:,
      expired:,
      failed: failing + expired,
      total: row["total"].to_i
    }
  end
rescue => e
  logger.error "Failed to fetch job counts: #{e.message}"
  {}
end

#queue_statisticsObject

Que separates two trouble states: a failing job has errored but still has retries left, an expired job has run out of them and needs a human. :failed is the union of the two, kept for callers written before the split.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/newshound/jobs/que.rb', line 15

def queue_statistics
  conn = ActiveRecord::Base.connection
  current_time = conn.quote(Time.now)
  beginning_of_day = conn.quote(Date.today.to_time)

  {
    ready: count_jobs("finished_at IS NULL AND expired_at IS NULL AND run_at <= #{current_time}"),
    scheduled: count_jobs("finished_at IS NULL AND expired_at IS NULL AND run_at > #{current_time}"),
    failing: count_jobs("error_count > 0 AND finished_at IS NULL AND expired_at IS NULL"),
    expired: count_jobs("expired_at IS NOT NULL"),
    failed: count_jobs("error_count > 0 AND finished_at IS NULL"),
    finished_today: count_jobs("finished_at >= #{beginning_of_day}")
  }
rescue => e
  logger.error "Failed to fetch Que statistics: #{e.message}"
  {ready: 0, scheduled: 0, failing: 0, expired: 0, failed: 0, finished_today: 0}
end