Active Job Core

Provides general behavior that will be included into every Active Job object that inherits from ActiveJob::Base.

Namespace

Module

Methods

Attributes

[RW] arguments

Job arguments

[RW] enqueue_error

Track any exceptions raised by the backend so callers can inspect the errors.

[RW] enqueued_at

Track when a job was enqueued

[RW] exception_executions

Hash that contains the number of times this job handled errors for each specific retry_on declaration. Keys are the string representation of the exceptions listed in the retry_on declaration, while its associated value holds the number of executions where the corresponding retry_on declaration handled one of its listed exceptions.

[RW] executions

Number of times this job has been executed (which increments on every retry, like after an exception).

[RW] job_id

Job Identifier

[RW] locale

I18n.locale to be used during the job.

[W] priority

Priority that the job will have (lower is more priority).

[RW] provider_job_id

ID optionally provided by adapter

[W] queue_name

Queue in which the job will reside.

[R] scheduled_at

Time when the job should be performed

[W] serialized_arguments
[RW] timezone

Timezone to be used during the job.

Class Public methods

new(*arguments)

Creates a new job instance. Takes the arguments that will be passed to the perform method.

📝 Source code
# File activejob/lib/active_job/core.rb, line 95
    def initialize(*arguments)
      @arguments  = arguments
      @job_id     = SecureRandom.uuid
      @queue_name = self.class.queue_name
      @scheduled_at = nil
      @_scheduled_at_time = nil
      @priority   = self.class.priority
      @executions = 0
      @exception_executions = {}
      @timezone   = Time.zone&.name
    end
🔎 See on GitHub

Instance Public methods

deserialize(job_data)

Attaches the stored job data to the current instance. Receives a hash returned from serialize

Examples

class DeliverWebhookJob < ActiveJob::Base
  attr_writer :attempt_number

  def attempt_number
    @attempt_number ||= 0
  end

  def serialize
    super.merge('attempt_number' => attempt_number + 1)
  end

  def deserialize(job_data)
    super
    self.attempt_number = job_data['attempt_number']
  end

  rescue_from(Timeout::Error) do |exception|
    raise exception if attempt_number > 5
    retry_job(wait: 10)
  end
end
📝 Source code
# File activejob/lib/active_job/core.rb, line 153
    def deserialize(job_data)
      self.job_id               = job_data["job_id"]
      self.provider_job_id      = job_data["provider_job_id"]
      self.queue_name           = job_data["queue_name"]
      self.priority             = job_data["priority"]
      self.serialized_arguments = job_data["arguments"]
      self.executions           = job_data["executions"]
      self.exception_executions = job_data["exception_executions"]
      self.locale               = job_data["locale"] || I18n.locale.to_s
      self.timezone             = job_data["timezone"] || Time.zone&.name
      self.enqueued_at          = Time.iso8601(job_data["enqueued_at"]) if job_data["enqueued_at"]
      self.scheduled_at         = Time.iso8601(job_data["scheduled_at"]) if job_data["scheduled_at"]
    end
🔎 See on GitHub

scheduled_at=(value)

📝 Source code
# File activejob/lib/active_job/core.rb, line 177
    def scheduled_at=(value)
      @_scheduled_at_time = if value&.is_a?(Numeric)
        ActiveJob.deprecator.warn(<<~MSG.squish)
          Assigning a numeric/epoch value to scheduled_at is deprecated. Use a Time object instead.
        MSG
        Time.at(value)
      else
        value
      end
      @scheduled_at = value
    end
🔎 See on GitHub

serialize()

Returns a hash with the job data that can safely be passed to the queuing adapter.

📝 Source code
# File activejob/lib/active_job/core.rb, line 110
    def serialize
      {
        "job_class"  => self.class.name,
        "job_id"     => job_id,
        "provider_job_id" => provider_job_id,
        "queue_name" => queue_name,
        "priority"   => priority,
        "arguments"  => serialize_arguments_if_needed(arguments),
        "executions" => executions,
        "exception_executions" => exception_executions,
        "locale"     => I18n.locale.to_s,
        "timezone"   => timezone,
        "enqueued_at" => Time.now.utc.iso8601(9),
        "scheduled_at" => _scheduled_at_time ? _scheduled_at_time.utc.iso8601(9) : nil,
      }
    end
🔎 See on GitHub

successfully_enqueued?()

📝 Source code
# File activejob/lib/active_job/core.rb, line 53
    def successfully_enqueued?
      @successfully_enqueued
    end
🔎 See on GitHub