Class: BackgroundJob

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

Defined Under Namespace

Modules: Scopes

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)


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

def initialize(job)
  @job = job
end

Instance Attribute Details

#jobSolidQueue::Job (readonly)

Returns:

  • (SolidQueue::Job)


17
18
19
# File 'app/models/background_job.rb', line 17

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.



23
24
25
26
27
28
29
30
31
32
# File 'app/models/background_job.rb', line 23

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,
  }.transform_values { |jobs| jobs.extending(Scopes) }
end

Instance Method Details

#backtraceObject

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



101
102
103
# File 'app/models/background_job.rb', line 101

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)


69
70
71
# File 'app/models/background_job.rb', line 69

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".



55
56
57
58
59
60
61
62
63
64
65
# File 'app/models/background_job.rb', line 55

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.



45
46
47
# File 'app/models/background_job.rb', line 45

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

#failure_classObject



80
81
82
# File 'app/models/background_job.rb', line 80

def failure_class
  failed_execution.exception_class
end

#failure_reasonObject

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



74
75
76
77
78
# File 'app/models/background_job.rb', line 74

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.



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

def job_id
  job.arguments["job_id"]
end

#serialized_argumentsObject

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



50
51
52
# File 'app/models/background_job.rb', line 50

def serialized_arguments
  job.arguments["arguments"]
end

#stateObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/models/background_job.rb', line 84

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



105
106
107
# File 'app/models/background_job.rb', line 105

def to_param
  active_job_id
end

#to_sObject



109
110
111
# File 'app/models/background_job.rb', line 109

def to_s
  class_name
end