123456789_123456789_123456789_123456789_123456789_

Module: ActiveJob::Core

Relationships & Source Files
Namespace Children
Modules:
Extension / Inclusion / Inheritance Descendants
Included In:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Defined in: activejob/lib/active_job/core.rb

Overview

================

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

Class Method Summary

::ActiveSupport::Concern - Extended

class_methods

Define class methods from given block.

included

Evaluate given block in context of base class, so that you can write class macros here.

prepended

Evaluate given block in context of base class, so that you can write class macros here.

append_features, prepend_features

Instance Attribute Summary

Instance Method Summary

Instance Attribute Details

#arguments (rw)

Job arguments

[ GitHub ]

  
# File 'activejob/lib/active_job/core.rb', line 20

attr_accessor :arguments

#arguments_serialized? ⇒ Boolean (readonly, private)

[ GitHub ]

  
# File 'activejob/lib/active_job/core.rb', line 214

def arguments_serialized?
  @serialized_arguments
end

#enqueue_error (rw)

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

[ GitHub ]

  
# File 'activejob/lib/active_job/core.rb', line 64

attr_accessor :enqueue_error

#enqueued_at (rw)

Track when a job was enqueued

[ GitHub ]

  
# File 'activejob/lib/active_job/core.rb', line 54

attr_accessor :enqueued_at

#exception_executions (rw)

::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.

[ GitHub ]

  
# File 'activejob/lib/active_job/core.rb', line 45

attr_accessor :exception_executions

#executions (rw)

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

[ GitHub ]

  
# File 'activejob/lib/active_job/core.rb', line 39

attr_accessor :executions

#job_id (rw)

Job Identifier

[ GitHub ]

  
# File 'activejob/lib/active_job/core.rb', line 27

attr_accessor :job_id

#locale (rw)

I18n.locale to be used during the job.

[ GitHub ]

  
# File 'activejob/lib/active_job/core.rb', line 48

attr_accessor :locale

#priority=(value) (writeonly)

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

[ GitHub ]

  
# File 'activejob/lib/active_job/core.rb', line 33

attr_writer :priority

#provider_job_id (rw)

ID optionally provided by adapter

[ GitHub ]

  
# File 'activejob/lib/active_job/core.rb', line 36

attr_accessor :provider_job_id

#queue_name=(value) (writeonly)

Queue in which the job will reside.

[ GitHub ]

  
# File 'activejob/lib/active_job/core.rb', line 30

attr_writer :queue_name

#scheduled_at (rw)

::Time when the job should be performed

[ GitHub ]

  
# File 'activejob/lib/active_job/core.rb', line 24

attr_accessor :scheduled_at

#serialized_arguments=(value) (writeonly)

[ GitHub ]

  
# File 'activejob/lib/active_job/core.rb', line 21

attr_writer :serialized_arguments

#successfully_enqueued=(value) (rw)

This method is for internal use only.

Track whether the adapter received the job successfully.

[ GitHub ]

  
# File 'activejob/lib/active_job/core.rb', line 57

attr_writer :successfully_enqueued # :nodoc:

#successfully_enqueued? ⇒ Boolean (rw)

[ GitHub ]

  
# File 'activejob/lib/active_job/core.rb', line 59

def successfully_enqueued?
  @successfully_enqueued
end

#timezone (rw)

Timezone to be used during the job.

[ GitHub ]

  
# File 'activejob/lib/active_job/core.rb', line 51

attr_accessor :timezone

Instance Method Details

#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
[ GitHub ]

  
# File 'activejob/lib/active_job/core.rb', line 166

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"].dup
  self.locale               = job_data["locale"] || I18n.locale.to_s
  self.timezone             = job_data["timezone"] || Time.zone&.name
  self.enqueued_at          = deserialize_time(job_data["enqueued_at"]) if job_data["enqueued_at"]
  self.scheduled_at         = deserialize_time(job_data["scheduled_at"]) if job_data["scheduled_at"]
end

#deserialize_arguments(serialized_args) (private)

[ GitHub ]

  
# File 'activejob/lib/active_job/core.rb', line 210

def deserialize_arguments(serialized_args)
  Arguments.deserialize(serialized_args)
end

#deserialize_arguments_if_needed (private)

[ GitHub ]

  
# File 'activejob/lib/active_job/core.rb', line 199

def deserialize_arguments_if_needed
  if arguments_serialized?
    @arguments = deserialize_arguments(@serialized_arguments)
    @serialized_arguments = nil
  end
end

#deserialize_time(time) (private)

[ GitHub ]

  
# File 'activejob/lib/active_job/core.rb', line 218

def deserialize_time(time)
  if time.is_a?(Time)
    time
  else
    Time.iso8601(time)
  end
end

#initialize(*arguments, **kwargs)

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

[ GitHub ]

  
# File 'activejob/lib/active_job/core.rb', line 107

def initialize(*arguments, **kwargs)
  arguments << Hash.ruby2_keywords_hash(kwargs) unless kwargs.empty?
  @arguments  = arguments
  @job_id     = SecureRandom.uuid
  @queue_name = self.class.queue_name
  @scheduled_at = nil
  @priority   = self.class.priority
  @executions = 0
  @exception_executions = {}
  @timezone   = Time.zone&.name
end

#serialize

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

[ GitHub ]

  
# File 'activejob/lib/active_job/core.rb', line 121

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"     => locale || I18n.locale.to_s,
    "timezone"   => timezone,
    "enqueued_at" => Time.now.utc.iso8601(9),
    "scheduled_at" => scheduled_at ? scheduled_at.getutc.iso8601(9) : nil,
  }
end

#serialize_arguments(arguments) (private)

[ GitHub ]

  
# File 'activejob/lib/active_job/core.rb', line 206

def serialize_arguments(arguments)
  Arguments.serialize(arguments)
end

#serialize_arguments_if_needed(arguments) (private)

[ GitHub ]

  
# File 'activejob/lib/active_job/core.rb', line 191

def serialize_arguments_if_needed(arguments)
  if arguments_serialized?
    @serialized_arguments
  else
    serialize_arguments(arguments)
  end
end

#set(options = {})

This method is for internal use only.

Configures the job with the given options.

[ GitHub ]

  
# File 'activejob/lib/active_job/core.rb', line 181

def set(options = {}) # :nodoc:
  self.scheduled_at = options[:wait].seconds.from_now if options[:wait]
  self.scheduled_at = options[:wait_until] if options[:wait_until]
  self.queue_name   = self.class.queue_name_from_part(options[:queue]) if options[:queue]
  self.priority     = options[:priority].to_i if options[:priority]

  self
end