module ActiveJob::Core
Provides general behavior that will be included into every Active Job object that inherits from ActiveJob::Base
.
Public Class Methods
new(*arguments)
click to toggle source
Creates a new job instance. Takes the arguments that will be passed to the perform method.
# File lib/active_job/core.rb, line 71 def initialize(*arguments) @arguments = arguments @job_id = SecureRandom.uuid @queue_name = self.class.queue_name @priority = self.class.priority @executions = 0 end
Public Instance Methods
deserialize(job_data)
click to toggle source
Attaches the stored job data to the current instance. Receives a hash returned from 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 lib/active_job/core.rb, line 120 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.locale = job_data["locale"] || I18n.locale.to_s end
serialize()
click to toggle source
Returns a hash with the job data that can safely be passed to the queueing adapter.
# File lib/active_job/core.rb, line 81 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, "locale" => I18n.locale.to_s } end
Private Instance Methods
arguments_serialized?()
click to toggle source
# File lib/active_job/core.rb, line 154 def arguments_serialized? defined?(@serialized_arguments) && @serialized_arguments end
deserialize_arguments(serialized_args)
click to toggle source
# File lib/active_job/core.rb, line 150 def deserialize_arguments(serialized_args) Arguments.deserialize(serialized_args) end
deserialize_arguments_if_needed()
click to toggle source
# File lib/active_job/core.rb, line 139 def deserialize_arguments_if_needed if arguments_serialized? @arguments = deserialize_arguments(@serialized_arguments) @serialized_arguments = nil end end
serialize_arguments(arguments)
click to toggle source
# File lib/active_job/core.rb, line 146 def serialize_arguments(arguments) Arguments.serialize(arguments) end
serialize_arguments_if_needed(arguments)
click to toggle source
# File lib/active_job/core.rb, line 131 def serialize_arguments_if_needed(arguments) if arguments_serialized? @serialized_arguments else serialize_arguments(arguments) end end