Active Job 內核
提供將包含在繼承自 ActiveJob::Base
的每個 Active Job 物件的一般行為
命名空間
方法
屬性
[RW] | arguments | 工作引數 |
[RW] | enqueue_error | 追蹤後端引發的任何例外,以便呼叫者可以檢查錯誤。 |
[RW] | enqueued_at | 追蹤工作何時加入佇列 |
[RW] | exception_executions |
|
[RW] | executions | 工作執行的次數(在每次重試時增加,例如在發生例外後)。 |
[RW] | job_id | 工作識別碼 |
[RW] | locale | 工作期間要使用的 I18n.locale。 |
[W] | priority | 工作的優先順序(較低優先順序越高)。 |
[RW] | provider_job_id | 適配器可能會提供的 ID |
[W] | queue_name | 工作將駐留的佇列。 |
[RW] | scheduled_at |
|
[W] | serialized_arguments | |
[RW] | timezone | 工作期間要使用的時區。 |
類別公開方法
new(*arguments) 鏈結
建立新的工作執行個體。接受將傳遞給 perform 方法的引數。
來源:顯示 | 在 GitHub 上
# 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
來源:顯示 | 在 GitHub 上
# 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() 鏈結
傳回包含工作資料的雜湊,可安全地傳遞給入隊列適配器。
來源:顯示 | 在 GitHub 上
# 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?() 鏈結
來源:顯示 | 在 GitHub 上
# File activejob/lib/active_job/core.rb, line 51 def successfully_enqueued? @successfully_enqueued end