跳到內容 跳到搜尋

Active Job 內核

提供將包含在繼承自 ActiveJob::Base 的每個 Active Job 物件的一般行為

命名空間
方法
D
N
S

屬性

[RW] arguments

工作引數

[RW] enqueue_error

追蹤後端引發的任何例外,以便呼叫者可以檢查錯誤。

[RW] enqueued_at

追蹤工作何時加入佇列

[RW] exception_executions

Hash 包含工作對各特定 retry_on 宣告處理錯誤的次數。鍵是 retry_on 宣告中列出的例外的字串表示形式,而其關聯值則包含對應的 retry_on 宣告處理其中列出例外之一的執行次數。

[RW] executions

工作執行的次數(在每次重試時增加,例如在發生例外後)。

[RW] job_id

工作識別碼

[RW] locale

工作期間要使用的 I18n.locale。

[W] priority

工作的優先順序(較低優先順序越高)。

[RW] provider_job_id

適配器可能會提供的 ID

[W] queue_name

工作將駐留的佇列。

[RW] scheduled_at

Time 工作應該執行的時間

[W] serialized_arguments
[RW] 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