Class: FiberJob::Job
- Inherits:
-
Object
- Object
- FiberJob::Job
- Defined in:
- lib/fiber_job/job.rb
Overview
Base class for all background jobs in the FiberJob system. Provides interface for job execution, retry logic, and scheduling capabilities.
All job classes should inherit from this class and implement the #perform method to define their specific behavior.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#config ⇒ FiberJob::Config
readonly
Configuration object for this job instance.
-
#jid ⇒ String?
Unique job identifier.
-
#max_retries ⇒ Integer
Maximum number of retry attempts before giving up.
-
#priority ⇒ Integer
Job priority (higher numbers = higher priority).
-
#queue ⇒ Symbol
The queue name where this job will be processed.
-
#retry_count ⇒ Integer
Current number of retry attempts.
-
#timeout ⇒ Integer
Maximum execution time in seconds before timeout.
Class Method Summary collapse
-
.perform_async(*args) ⇒ String
Enqueues the job for immediate asynchronous execution.
-
.perform_at(time, *args) ⇒ String
Enqueues the job for execution at a specific time.
-
.perform_in(delay, *args) ⇒ String
Enqueues the job for execution after a specified delay.
-
.queue ⇒ Symbol
Returns the queue name for this job class.
Instance Method Summary collapse
-
#initialize(config: nil) ⇒ void
constructor
Initializes a new job instance with default configuration.
-
#perform(*args) ⇒ void
abstract
Executes the job with the provided arguments.
-
#priority_retry? ⇒ Boolean
Determines if failed jobs should be retried with priority.
-
#retry_delay(attempt) ⇒ Integer
Calculates the delay before retrying a failed job.
Constructor Details
#initialize(config: nil) ⇒ void
Initializes a new job instance with default configuration. Uses centralized configuration for defaults instead of hardcoded values.
58 59 60 61 62 63 64 65 |
# File 'lib/fiber_job/job.rb', line 58 def initialize(config: nil) @config = config || FiberJob.config @queue = :default @retry_count = 0 @max_retries = 3 @priority = 0 @timeout = 300 # 5 minutes end |
Instance Attribute Details
#config ⇒ FiberJob::Config (readonly)
Returns Configuration object for this job instance.
50 |
# File 'lib/fiber_job/job.rb', line 50 attr_accessor :queue, :retry_count, :max_retries, :priority, :timeout, :jid |
#jid ⇒ String?
Returns Unique job identifier.
50 |
# File 'lib/fiber_job/job.rb', line 50 attr_accessor :queue, :retry_count, :max_retries, :priority, :timeout, :jid |
#max_retries ⇒ Integer
Returns Maximum number of retry attempts before giving up.
50 |
# File 'lib/fiber_job/job.rb', line 50 attr_accessor :queue, :retry_count, :max_retries, :priority, :timeout, :jid |
#priority ⇒ Integer
Returns Job priority (higher numbers = higher priority).
50 |
# File 'lib/fiber_job/job.rb', line 50 attr_accessor :queue, :retry_count, :max_retries, :priority, :timeout, :jid |
#queue ⇒ Symbol
Returns The queue name where this job will be processed.
50 51 52 |
# File 'lib/fiber_job/job.rb', line 50 def queue @queue end |
#retry_count ⇒ Integer
Returns Current number of retry attempts.
50 |
# File 'lib/fiber_job/job.rb', line 50 attr_accessor :queue, :retry_count, :max_retries, :priority, :timeout, :jid |
#timeout ⇒ Integer
Returns Maximum execution time in seconds before timeout.
50 |
# File 'lib/fiber_job/job.rb', line 50 attr_accessor :queue, :retry_count, :max_retries, :priority, :timeout, :jid |
Class Method Details
.perform_async(*args) ⇒ String
Enqueues the job for immediate asynchronous execution.
122 123 124 |
# File 'lib/fiber_job/job.rb', line 122 def self.perform_async(*args) Client.enqueue(self, *args) end |
.perform_at(time, *args) ⇒ String
Enqueues the job for execution at a specific time.
146 147 148 |
# File 'lib/fiber_job/job.rb', line 146 def self.perform_at(time, *args) Client.enqueue_at(time, self, *args) end |
.perform_in(delay, *args) ⇒ String
Enqueues the job for execution after a specified delay.
134 135 136 |
# File 'lib/fiber_job/job.rb', line 134 def self.perform_in(delay, *args) Client.enqueue_in(delay, self, *args) end |
.queue ⇒ Symbol
Returns the queue name for this job class. Creates a temporary instance to get the configured queue name.
111 112 113 |
# File 'lib/fiber_job/job.rb', line 111 def self.queue new.queue end |
Instance Method Details
#perform(*args) ⇒ void
Subclasses must implement this method
This method returns an undefined value.
Executes the job with the provided arguments. This method must be implemented by all job subclasses.
80 81 82 |
# File 'lib/fiber_job/job.rb', line 80 def perform(*args) raise NotImplementedError, 'Subclasses must implement perform' end |
#priority_retry? ⇒ Boolean
Determines if failed jobs should be retried with priority. Priority retries are processed before regular jobs in the queue.
103 104 105 |
# File 'lib/fiber_job/job.rb', line 103 def priority_retry? true end |
#retry_delay(attempt) ⇒ Integer
Calculates the delay before retrying a failed job. Uses exponential backoff with random jitter by default.
94 95 96 97 |
# File 'lib/fiber_job/job.rb', line 94 def retry_delay(attempt) # Default exponential backoff: 2^attempt + random jitter (2 ** attempt) + rand(10) end |