Class: FiberJob::Job

Inherits:
Object
  • Object
show all
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.

Examples:

Basic job definition

class EmailJob < FiberJob::Job
  def perform(user_id, message)
    user = User.find(user_id)
    UserMailer.notification(user, message).deliver_now
  end
end

Job with custom configuration

class ComplexJob < FiberJob::Job
  def initialize
    super
    @queue = :high_priority
    @max_retries = 5
    @timeout = 600  # 10 minutes
  end

  def perform(data)
    # Complex processing logic
  end

  def retry_delay(attempt)
    attempt * 60  # Linear backoff: 1min, 2min, 3min...
  end
end

Direct Known Subclasses

CronJob

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config: nil) ⇒ void

Initializes a new job instance with default configuration. Uses centralized configuration for defaults instead of hardcoded values.

Parameters:

  • config (FiberJob::Config, nil) (defaults to: nil)

    Configuration object to use for defaults



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

#configFiberJob::Config (readonly)

Returns Configuration object for this job instance.

Returns:



50
# File 'lib/fiber_job/job.rb', line 50

attr_accessor :queue, :retry_count, :max_retries, :priority, :timeout, :jid

#jidString?

Returns Unique job identifier.

Returns:

  • (String, nil)

    Unique job identifier



50
# File 'lib/fiber_job/job.rb', line 50

attr_accessor :queue, :retry_count, :max_retries, :priority, :timeout, :jid

#max_retriesInteger

Returns Maximum number of retry attempts before giving up.

Returns:

  • (Integer)

    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

#priorityInteger

Returns Job priority (higher numbers = higher priority).

Returns:

  • (Integer)

    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

#queueSymbol

Returns The queue name where this job will be processed.

Returns:

  • (Symbol)

    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_countInteger

Returns Current number of retry attempts.

Returns:

  • (Integer)

    Current number of retry attempts



50
# File 'lib/fiber_job/job.rb', line 50

attr_accessor :queue, :retry_count, :max_retries, :priority, :timeout, :jid

#timeoutInteger

Returns Maximum execution time in seconds before timeout.

Returns:

  • (Integer)

    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.

Examples:

Enqueue a job

EmailJob.perform_async(user.id, "Welcome message")

Parameters:

  • args (Array)

    Arguments to pass to the job’s perform method

Returns:

  • (String)

    Job ID for tracking



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.

Examples:

Enqueue at specific time

EmailJob.perform_at(tomorrow_9am, user.id, "Daily summary")

Parameters:

  • time (Time, Integer)

    Specific time or timestamp for execution

  • args (Array)

    Arguments to pass to the job’s perform method

Returns:

  • (String)

    Job ID for tracking



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.

Examples:

Enqueue with delay

EmailJob.perform_in(3600, user.id, "Reminder message")  # 1 hour delay

Parameters:

  • delay (Numeric)

    Delay in seconds before execution

  • args (Array)

    Arguments to pass to the job’s perform method

Returns:

  • (String)

    Job ID for tracking



134
135
136
# File 'lib/fiber_job/job.rb', line 134

def self.perform_in(delay, *args)
  Client.enqueue_in(delay, self, *args)
end

.queueSymbol

Returns the queue name for this job class. Creates a temporary instance to get the configured queue name.

Returns:

  • (Symbol)

    The queue name where jobs of this class will be processed



111
112
113
# File 'lib/fiber_job/job.rb', line 111

def self.queue
  new.queue
end

Instance Method Details

#perform(*args) ⇒ void

This method is abstract.

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.

Examples:

Implementation in subclass

def perform(user_id, message)
  user = User.find(user_id)
  # Process the job...
end

Parameters:

  • args (Array)

    Arguments passed to the job

Raises:

  • (NotImplementedError)

    When called on the base Job class



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.

Returns:

  • (Boolean)

    Whether to use priority retry 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.

Examples:

Custom retry delay

def retry_delay(attempt)
  [30, 60, 120, 300][attempt] || 300  # Fixed intervals
end

Parameters:

  • attempt (Integer)

    The current retry attempt number (0-based)

Returns:

  • (Integer)

    Delay in seconds before retry



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