跳至内容 跳至搜索

Active Job 核心

提供通用行为,将被包含到每个继承自 ActiveJob::Base 的 Active Job 对象中。

命名空间
方法
D
N
S

属性

[读写] arguments

作业参数

[读写] enqueue_error

跟踪后端抛出的任何异常,以便调用者可以检查错误。

[读写] enqueued_at

跟踪作业入队的时刻

[读写] exception_executions

Hash 包含此作业针对每个特定 retry_on 声明处理错误的次数。键是 retry_on 声明中列出的异常的字符串表示,其关联值保存对应 retry_on 声明处理其列出的异常之一的执行次数。

[读写] executions

此作业执行的次数(每次重试时都会增加,例如在异常后)。

[读写] job_id

作业标识符

[读写] locale

作业期间使用的 I18n.locale。

[写] priority

作业的优先级(数字越小,优先级越高)。

[读写] provider_job_id

适配器可选提供的 ID

[写] queue_name

作业将驻留的队列。

[读写] scheduled_at

Time 作业应执行的时间

[写] serialized_arguments
[读写] timezone

作业期间使用的时区。

类公共方法

new(*arguments)

创建一个新的作业实例。接受将传递给 perform 方法的参数。

# File activejob/lib/active_job/core.rb, line 93
def initialize(*arguments)
  @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

实例公共方法

deserialize(job_data)

将存储的作业数据附加到当前实例。接收从 serialize 返回的哈希值

示例

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
# File activejob/lib/active_job/core.rb, line 150
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

serialize()

返回一个包含作业数据的哈希值,该哈希值可以安全地传递给排队适配器。

# File activejob/lib/active_job/core.rb, line 107
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 ? scheduled_at.utc.iso8601(9) : nil,
  }
end

successfully_enqueued?()

# File activejob/lib/active_job/core.rb, line 51
def successfully_enqueued?
  @successfully_enqueued
end