Class: BackgroundJob

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
app/models/background_job.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(job) ⇒ BackgroundJob

Returns a new instance of BackgroundJob.

Parameters:

  • job (SolidQueue::Job)


25
26
27
# File 'app/models/background_job.rb', line 25

def initialize(job)
  @job = job
end

Instance Attribute Details

#jobSolidQueue::Job (readonly)

Returns:

  • (SolidQueue::Job)


7
8
9
# File 'app/models/background_job.rb', line 7

def job
  @job
end

Class Method Details

.statesObject

The SolidQueue relation backing each job state, in display order. A job that isn't finished holds exactly one execution; finished jobs carry a finished_at.



13
14
15
16
17
18
19
20
21
22
# File 'app/models/background_job.rb', line 13

def self.states
  {
    pending:     SolidQueue::Job.where.associated(:ready_execution),
    scheduled:   SolidQueue::Job.where.associated(:scheduled_execution),
    in_progress: SolidQueue::Job.where.associated(:claimed_execution),
    blocked:     SolidQueue::Job.where.associated(:blocked_execution),
    failed:      SolidQueue::Job.failed,
    completed:   SolidQueue::Job.finished,
  }
end

Instance Method Details

#backtraceObject

Backtrace lines from the current failure, joined into a single string.



87
88
89
# File 'app/models/background_job.rb', line 87

def backtrace
  failed_execution&.backtrace&.join("\n")
end

#discardable?Boolean

SolidQueue::Job#discard only acts on a ready, claimed, or failed execution; it's a no-op for scheduled, blocked, or finished jobs.

Returns:

  • (Boolean)


59
60
61
# File 'app/models/background_job.rb', line 59

def discardable?
  ready_execution.present? || claimed_execution.present? || failed_execution.present?
end

#durationObject

Human-readable run time once finished, e.g. "1 minute and 35 seconds".



45
46
47
48
49
50
51
52
53
54
55
# File 'app/models/background_job.rb', line 45

def duration
  return unless finished? && enqueued_at

  seconds   = finished_at - enqueued_at
  precision = case seconds
              when 0...1  then 2
              when 1...10 then 1
              else             0
              end
  ActiveSupport::Duration.build(seconds.round(precision)).inspect
end

#enqueued_atObject

Serialized as an ISO8601 string in the payload; parse so views can format it.



35
36
37
# File 'app/models/background_job.rb', line 35

def enqueued_at
  job.arguments["enqueued_at"]&.then { |value| Time.iso8601(value) }
end

#failure_reasonObject

Exception class and message from the current failure, e.g. "RuntimeError: boom".



64
65
66
67
68
# File 'app/models/background_job.rb', line 64

def failure_reason
  return unless (failure = failed_execution)

  "#{failure.exception_class}: #{failure.message}"
end

#job_idObject

ActiveJob's job id, stored in the serialized payload rather than as a column.



30
31
32
# File 'app/models/background_job.rb', line 30

def job_id
  job.arguments["job_id"]
end

#serialized_argumentsObject

The arguments the job was enqueued with, from the serialized payload.



40
41
42
# File 'app/models/background_job.rb', line 40

def serialized_arguments
  job.arguments["arguments"]
end

#stateObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/models/background_job.rb', line 70

def state
  if finished?
    :finished
  elsif failed?
    :failed
  elsif claimed_execution.present?
    :in_progress
  elsif blocked_execution.present?
    :blocked
  elsif scheduled_execution.present?
    :scheduled
  else
    :pending
  end
end

#to_paramObject



91
92
93
# File 'app/models/background_job.rb', line 91

def to_param
  active_job_id
end

#to_sObject



95
96
97
# File 'app/models/background_job.rb', line 95

def to_s
  class_name
end