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)


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

def initialize(job)
  @job = job
end

Instance Attribute Details

#jobSolidQueue::Job (readonly)

Returns:

  • (SolidQueue::Job)


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

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.



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

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.



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

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)


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

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



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

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.



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

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



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

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.



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

def job_id
  job.arguments["job_id"]
end

#serialized_argumentsObject

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



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

def serialized_arguments
  job.arguments["arguments"]
end

#stateObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/models/background_job.rb', line 79

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



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

def to_param
  active_job_id
end

#to_sObject



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

def to_s
  class_name
end