Class: BackgroundJob
- Inherits:
-
Object
- Object
- BackgroundJob
- Includes:
- ActiveModel::Model
- Defined in:
- app/models/background_job.rb
Defined Under Namespace
Modules: Scopes
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.
34 35 36 |
# File 'app/models/background_job.rb', line 34 def initialize(job) @job = job end |
Instance Attribute Details
#job ⇒ SolidQueue::Job (readonly)
16 17 18 |
# File 'app/models/background_job.rb', line 16 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.
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
#backtrace ⇒ Object
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.
68 69 70 |
# File 'app/models/background_job.rb', line 68 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".
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_at ⇒ Object
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_reason ⇒ Object
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.}" end |
#job_id ⇒ Object
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_arguments ⇒ Object
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 |
#state ⇒ Object
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_param ⇒ Object
100 101 102 |
# File 'app/models/background_job.rb', line 100 def to_param active_job_id end |
#to_s ⇒ Object
104 105 106 |
# File 'app/models/background_job.rb', line 104 def to_s class_name end |