Module: ActiveJob::Core

Extended by:
ActiveSupport::Concern
Included in:
Base
Defined in:
activejob/lib/active_job/core.rb

Overview

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

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Methods included from ActiveSupport::Concern

append_features, class_methods, extended, included

Instance Method Details

#deserialize(job_data) ⇒ Object

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

Examples

class DeliverWebhookJob < ActiveJob::Base
  def serialize
    super.merge('attempt_number' => (@attempt_number || 0) + 1)
  end

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

  rescue_from(TimeoutError) do |exception|
    raise exception if @attempt_number > 5
    retry_job(wait: 10)
  end
end

106
107
108
109
110
111
112
113
# File 'activejob/lib/active_job/core.rb', line 106

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.locale               = job_data['locale'] || I18n.locale.to_s
end

#initialize(*arguments) ⇒ Object

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


66
67
68
69
70
71
# File 'activejob/lib/active_job/core.rb', line 66

def initialize(*arguments)
  @arguments  = arguments
  @job_id     = SecureRandom.uuid
  @queue_name = self.class.queue_name
  @priority   = self.class.priority
end

#serializeObject

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


75
76
77
78
79
80
81
82
83
84
# File 'activejob/lib/active_job/core.rb', line 75

def serialize
  {
    'job_class'  => self.class.name,
    'job_id'     => job_id,
    'queue_name' => queue_name,
    'priority'   => priority,
    'arguments'  => serialize_arguments(arguments),
    'locale'     => I18n.locale.to_s
  }
end