Active Job Core¶ ↑
提供了将包含在每个继承自 ActiveJob::Base 的 Active Job 对象中的通用行为。
命名空间
方法
Attributes
| [RW] | arguments | 任务参数 |
| [RW] | enqueue_error | 跟踪后端引发的任何异常,以便调用者可以检查错误。 |
| [RW] | enqueued_at | 跟踪任务何时入队 |
| [RW] | exception_executions | 包含此任务处理每个特定 retry_on 声明的错误次数的 |
| [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) Link
创建一个新的任务实例。接受将传递给 perform 方法的参数。
# File activejob/lib/active_job/core.rb, line 103 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) Link
将存储的任务数据附加到当前实例。接收 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
# File activejob/lib/active_job/core.rb, line 160 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 = 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
serialize() Link
返回一个包含任务数据的哈希,该哈希可以安全地传递给队列适配器。
# File activejob/lib/active_job/core.rb, line 117 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.utc.iso8601(9) : nil, } end