Class: Postburner::Job Abstract
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Postburner::Job
- Includes:
- Callbacks, Commands, Execution, Insertion, Logging, Properties, Statistics
- Defined in:
- app/models/postburner/job.rb
Overview
Subclass and implement #perform to define job behavior
Add ‘cancelled_at` field to block job execution if present (See opensource/postburner#16)
Job won’t run unless queued_at is set and is prior to execution time
Subclasses must implement the #perform method
Base class for all Postburner background jobs using Single Table Inheritance (STI).
Postburner::Job provides a PostgreSQL-backed job queue system built on Beanstalkd. Every job is stored as an ActiveRecord model, enabling database queries, foreign key relationships, and full audit trails.
Direct Known Subclasses
Defined Under Namespace
Classes: AlreadyProcessed, BadFormat, MalformedResponse, OrphanedJobError, PrematurePerform
Instance Attribute Summary collapse
-
#args ⇒ Hash
readonly
JSONB arguments passed to perform.
-
#attempt_count ⇒ Integer
readonly
Number of execution attempts.
-
#attempting_at ⇒ Time
readonly
Time first attempt started.
-
#attempts ⇒ Array<Time>
readonly
Array of attempt timestamps.
-
#bkid ⇒ Integer
readonly
Beanstalkd job ID (nil in test mode).
-
#duration ⇒ Float
readonly
Milliseconds between processing_at and processed_at.
-
#errata ⇒ Array<Hash>
readonly
Array of error details with timestamps.
-
#error_count ⇒ Integer
readonly
Number of errors encountered.
-
#id ⇒ Integer
readonly
Primary key.
-
#lag ⇒ Float
readonly
Milliseconds between intended_at and attempting_at.
-
#log_count ⇒ Integer
readonly
Number of log entries.
-
#logs ⇒ Array<Hash>
readonly
Array of log entries with timestamps.
-
#processed_at ⇒ Time
readonly
Time job completed successfully.
-
#processing_at ⇒ Time
readonly
Time current/last processing started.
-
#queued_at ⇒ Time
readonly
Time job was queued.
-
#removed_at ⇒ Time
readonly
Time job was removed from queue.
-
#run_at ⇒ Time
readonly
Scheduled execution time (nil for immediate).
-
#sid ⇒ String
readonly
Unique UUID identifier.
-
#type ⇒ String
readonly
STI type column for job subclass.
Class Method Summary collapse
-
.find_sti_class(type_name) ⇒ Class
Resolves an STI type name to a class, falling back to OrphanedJob when the original class no longer exists.
Instance Method Summary collapse
-
#bk ⇒ Beaneater::Job?
Returns the Beanstalkd job object for direct queue operations.
-
#bk! ⇒ Beaneater::Job?
Returns the Beanstalkd job object with cache invalidation.
-
#bk=(job) ⇒ Object
Sets the Beanstalkd job object directly.
-
#destroy ⇒ self
Destroys the job record and removes it from Beanstalkd queue.
-
#destroy! ⇒ self
Destroys the job record and removes it from Beanstalkd queue, raising on failure.
-
#orphaned? ⇒ Boolean
Returns whether this job instance is an orphaned placeholder.
-
#perform(args = nil) ⇒ void
abstract
Abstract method to be implemented by subclasses.
Methods included from Statistics
#elapsed_ms, #intended_at, #stats
Methods included from Execution
Methods included from Insertion
#queue!, #requeue!, #will_insert?
Methods included from Commands
#delete!, #extend!, #kick!, #remove!
Methods included from Logging
#log, #log!, #log_exception, #log_exception!
Methods included from Properties
#priority, #queue_name, #retry_delay_for_attempt, #should_retry?, #ttr, #tube_name
Instance Attribute Details
#args ⇒ Hash (readonly)
JSONB arguments passed to perform
72 73 74 |
# File 'app/models/postburner/job.rb', line 72 def args @args end |
#attempt_count ⇒ Integer (readonly)
Number of execution attempts
72 73 74 |
# File 'app/models/postburner/job.rb', line 72 def attempt_count @attempt_count end |
#attempting_at ⇒ Time (readonly)
Time first attempt started
72 73 74 |
# File 'app/models/postburner/job.rb', line 72 def attempting_at @attempting_at end |
#attempts ⇒ Array<Time> (readonly)
Array of attempt timestamps
72 73 74 |
# File 'app/models/postburner/job.rb', line 72 def attempts @attempts end |
#bkid ⇒ Integer (readonly)
Beanstalkd job ID (nil in test mode)
72 73 74 |
# File 'app/models/postburner/job.rb', line 72 def bkid @bkid end |
#duration ⇒ Float (readonly)
Milliseconds between processing_at and processed_at
72 73 74 |
# File 'app/models/postburner/job.rb', line 72 def duration @duration end |
#errata ⇒ Array<Hash> (readonly)
Array of error details with timestamps
72 73 74 |
# File 'app/models/postburner/job.rb', line 72 def errata @errata end |
#error_count ⇒ Integer (readonly)
Number of errors encountered
72 73 74 |
# File 'app/models/postburner/job.rb', line 72 def error_count @error_count end |
#id ⇒ Integer (readonly)
Primary key
72 73 74 |
# File 'app/models/postburner/job.rb', line 72 def id @id end |
#lag ⇒ Float (readonly)
Milliseconds between intended_at and attempting_at
72 73 74 |
# File 'app/models/postburner/job.rb', line 72 def lag @lag end |
#log_count ⇒ Integer (readonly)
Number of log entries
72 73 74 |
# File 'app/models/postburner/job.rb', line 72 def log_count @log_count end |
#logs ⇒ Array<Hash> (readonly)
Array of log entries with timestamps
72 73 74 |
# File 'app/models/postburner/job.rb', line 72 def logs @logs end |
#processed_at ⇒ Time (readonly)
Time job completed successfully
72 73 74 |
# File 'app/models/postburner/job.rb', line 72 def processed_at @processed_at end |
#processing_at ⇒ Time (readonly)
Time current/last processing started
72 73 74 |
# File 'app/models/postburner/job.rb', line 72 def processing_at @processing_at end |
#queued_at ⇒ Time (readonly)
Time job was queued
72 73 74 |
# File 'app/models/postburner/job.rb', line 72 def queued_at @queued_at end |
#removed_at ⇒ Time (readonly)
Time job was removed from queue
72 73 74 |
# File 'app/models/postburner/job.rb', line 72 def removed_at @removed_at end |
#run_at ⇒ Time (readonly)
Scheduled execution time (nil for immediate)
72 73 74 |
# File 'app/models/postburner/job.rb', line 72 def run_at @run_at end |
#sid ⇒ String (readonly)
Unique UUID identifier
72 73 74 |
# File 'app/models/postburner/job.rb', line 72 def sid @sid end |
#type ⇒ String (readonly)
STI type column for job subclass
72 73 74 |
# File 'app/models/postburner/job.rb', line 72 def type @type end |
Class Method Details
.find_sti_class(type_name) ⇒ Class
Resolves an STI type name to a class, falling back to OrphanedJob when the original class no longer exists. This tolerates rows whose ‘type` column references a deleted or renamed job class, preventing ActiveRecord::SubclassNotFound from crashing callers (e.g. the admin queued-jobs page).
100 101 102 103 104 |
# File 'app/models/postburner/job.rb', line 100 def self.find_sti_class(type_name) super rescue ActiveRecord::SubclassNotFound Postburner::OrphanedJob end |
Instance Method Details
#bk ⇒ Beaneater::Job?
Returns the Beanstalkd job object for direct queue operations.
Provides access to the underlying Beaneater job object for advanced Beanstalkd operations. Caches the job object in an instance variable.
200 201 202 203 204 205 |
# File 'app/models/postburner/job.rb', line 200 def bk return @__job if @__job return unless self.bkid.present? @__job = Postburner.connection.beanstalk.jobs.find(self.bkid) end |
#bk! ⇒ Beaneater::Job?
Returns the Beanstalkd job object with cache invalidation.
Same as #bk but clears the cached instance variable first, forcing a fresh lookup from Beanstalkd. Use when job state may have changed.
233 234 235 236 |
# File 'app/models/postburner/job.rb', line 233 def bk! @__job = nil self.bk end |
#bk=(job) ⇒ Object
Sets the Beanstalkd job object directly.
Used by the worker to pass the reserved Beanstalkd job to the model, ensuring that operations like Commands#extend!, Commands#delete!, and Commands#kick! use the actual reserved job rather than a fresh lookup via peek.
217 218 219 |
# File 'app/models/postburner/job.rb', line 217 def bk=(job) @__job = job end |
#destroy ⇒ self
This is a standard Rails method with Beanstalkd integration via callback
Destroys the job record and removes it from Beanstalkd queue.
Uses before_destroy callback to call Commands#delete! which removes the job from Beanstalkd before destroying the ActiveRecord model.
|
|
# File 'app/models/postburner/job.rb', line 158
|
#destroy! ⇒ self
This is a standard Rails method with Beanstalkd integration via callback
Destroys the job record and removes it from Beanstalkd queue, raising on failure.
Uses before_destroy callback to call Commands#delete! which removes the job from Beanstalkd before destroying the ActiveRecord model.
|
|
# File 'app/models/postburner/job.rb', line 170
|
#orphaned? ⇒ Boolean
Returns whether this job instance is an orphaned placeholder.
Always false on the base class. Overridden to return true in OrphanedJob.
113 |
# File 'app/models/postburner/job.rb', line 113 def orphaned? = false |
#perform(args = nil) ⇒ void
Subclasses must implement this method
Use Logging#log or Logging#log! within perform to add entries to the job’s audit trail
Exceptions will be caught, logged to errata, and re-raised
Args are always accessible via self.args regardless of method signature
This method returns an undefined value.
Abstract method to be implemented by subclasses.
This method contains the actual work logic for the job. It is called by Execution#perform! within the processing callbacks. Any exceptions raised will be logged and re-raised, causing the job to fail.
154 155 156 |
# File 'app/models/postburner/job.rb', line 154 def perform(args=nil) raise NotImplementedError, "Subclasses must implement the perform method" end |