Class: BackgroundJob
- Inherits:
-
Object
- Object
- BackgroundJob
- Includes:
- ActiveModel::Model
- Defined in:
- app/models/background_job.rb
Instance Attribute Summary collapse
- #job ⇒ SolidQueue::Job readonly
Class Method Summary collapse
-
.states ⇒ Object
The SolidQueue relation backing each job state, in display order.
Instance Method Summary collapse
-
#backtrace ⇒ Object
Backtrace lines from the current failure, joined into a single string.
-
#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.
-
#duration ⇒ Object
Human-readable run time once finished, e.g.
-
#enqueued_at ⇒ Object
Serialized as an ISO8601 string in the payload; parse so views can format it.
-
#failure_reason ⇒ Object
Exception class and message from the current failure, e.g.
-
#initialize(job) ⇒ BackgroundJob
constructor
A new instance of BackgroundJob.
-
#job_id ⇒ Object
ActiveJob's job id, stored in the serialized payload rather than as a column.
-
#serialized_arguments ⇒ Object
The arguments the job was enqueued with, from the serialized payload.
- #state ⇒ Object
- #to_param ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(job) ⇒ BackgroundJob
Returns a new instance of BackgroundJob.
25 26 27 |
# File 'app/models/background_job.rb', line 25 def initialize(job) @job = job end |
Instance Attribute Details
#job ⇒ SolidQueue::Job (readonly)
7 8 9 |
# File 'app/models/background_job.rb', line 7 def job @job end |
Class Method Details
.states ⇒ Object
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
#backtrace ⇒ Object
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.
59 60 61 |
# File 'app/models/background_job.rb', line 59 def discardable? ready_execution.present? || claimed_execution.present? || failed_execution.present? end |
#duration ⇒ Object
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_at ⇒ Object
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_reason ⇒ Object
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.}" end |
#job_id ⇒ Object
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_arguments ⇒ Object
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 |
#state ⇒ Object
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_param ⇒ Object
91 92 93 |
# File 'app/models/background_job.rb', line 91 def to_param active_job_id end |
#to_s ⇒ Object
95 96 97 |
# File 'app/models/background_job.rb', line 95 def to_s class_name end |