Module: ActiveJob::Core
Overview
Active Job Core
Provides general behavior that will be included into every Active Job object that inherits from ActiveJob::Base.
Defined Under Namespace
Modules: ClassMethods
Instance Attribute Summary collapse
-
#arguments ⇒ Object
Job arguments.
-
#enqueue_error ⇒ Object
Track any exceptions raised by the backend so callers can inspect the errors.
-
#enqueued_at ⇒ Object
Track when a job was enqueued.
-
#exception_executions ⇒ Object
Hash that contains the number of times this job handled errors for each specific retry_on declaration.
-
#executions ⇒ Object
Number of times this job has been executed (which increments on every retry, like after an exception).
-
#job_id ⇒ Object
Job Identifier.
-
#locale ⇒ Object
I18n.locale to be used during the job.
-
#priority ⇒ Object
writeonly
Priority that the job will have (lower is more priority).
-
#provider_job_id ⇒ Object
ID optionally provided by adapter.
-
#queue_name ⇒ Object
writeonly
Queue in which the job will reside.
-
#scheduled_at ⇒ Object
Time when the job should be performed.
-
#serialized_arguments ⇒ Object
writeonly
Sets the attribute serialized_arguments.
-
#successfully_enqueued ⇒ Object
writeonly
Track whether the adapter received the job successfully.
-
#timezone ⇒ Object
Timezone to be used during the job.
Instance Method Summary collapse
-
#deserialize(job_data) ⇒ Object
Attaches the stored job data to the current instance.
-
#initialize(*arguments) ⇒ Object
Creates a new job instance.
-
#serialize ⇒ Object
Returns a hash with the job data that can safely be passed to the queuing adapter.
-
#set(options = {}) ⇒ Object
Configures the job with the given options.
- #successfully_enqueued? ⇒ Boolean
Instance Attribute Details
#arguments ⇒ Object
Job arguments
19 20 21 |
# File 'lib/active_job/core.rb', line 19 def arguments @arguments end |
#enqueue_error ⇒ Object
Track any exceptions raised by the backend so callers can inspect the errors.
63 64 65 |
# File 'lib/active_job/core.rb', line 63 def enqueue_error @enqueue_error end |
#enqueued_at ⇒ Object
Track when a job was enqueued
53 54 55 |
# File 'lib/active_job/core.rb', line 53 def enqueued_at @enqueued_at end |
#exception_executions ⇒ Object
Hash that contains the number of times this job handled errors for each specific retry_on declaration. Keys are the string representation of the exceptions listed in the retry_on declaration, while its associated value holds the number of executions where the corresponding retry_on declaration handled one of its listed exceptions.
44 45 46 |
# File 'lib/active_job/core.rb', line 44 def exception_executions @exception_executions end |
#executions ⇒ Object
Number of times this job has been executed (which increments on every retry, like after an exception).
38 39 40 |
# File 'lib/active_job/core.rb', line 38 def executions @executions end |
#job_id ⇒ Object
Job Identifier
26 27 28 |
# File 'lib/active_job/core.rb', line 26 def job_id @job_id end |
#locale ⇒ Object
I18n.locale to be used during the job.
47 48 49 |
# File 'lib/active_job/core.rb', line 47 def locale @locale end |
#priority=(value) ⇒ Object (writeonly)
Priority that the job will have (lower is more priority).
32 33 34 |
# File 'lib/active_job/core.rb', line 32 def priority=(value) @priority = value end |
#provider_job_id ⇒ Object
ID optionally provided by adapter
35 36 37 |
# File 'lib/active_job/core.rb', line 35 def provider_job_id @provider_job_id end |
#queue_name=(value) ⇒ Object (writeonly)
Queue in which the job will reside.
29 30 31 |
# File 'lib/active_job/core.rb', line 29 def queue_name=(value) @queue_name = value end |
#scheduled_at ⇒ Object
Time when the job should be performed
23 24 25 |
# File 'lib/active_job/core.rb', line 23 def scheduled_at @scheduled_at end |
#serialized_arguments=(value) ⇒ Object (writeonly)
Sets the attribute serialized_arguments
20 21 22 |
# File 'lib/active_job/core.rb', line 20 def serialized_arguments=(value) @serialized_arguments = value end |
#successfully_enqueued=(value) ⇒ Object (writeonly)
Track whether the adapter received the job successfully.
56 57 58 |
# File 'lib/active_job/core.rb', line 56 def successfully_enqueued=(value) @successfully_enqueued = value end |
#timezone ⇒ Object
Timezone to be used during the job.
50 51 52 |
# File 'lib/active_job/core.rb', line 50 def timezone @timezone end |
Instance Method Details
#deserialize(job_data) ⇒ Object
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
160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File '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 |
#initialize(*arguments) ⇒ Object
Creates a new job instance. Takes the arguments that will be passed to the perform method.
103 104 105 106 107 108 109 110 111 112 |
# File '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 |
#serialize ⇒ Object
Returns a hash with the job data that can safely be passed to the queuing adapter.
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File '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 |
#set(options = {}) ⇒ Object
Configures the job with the given options.
175 176 177 178 179 180 181 182 |
# File 'lib/active_job/core.rb', line 175 def set( = {}) # :nodoc: self.scheduled_at = [:wait].seconds.from_now if [:wait] self.scheduled_at = [:wait_until] if [:wait_until] self.queue_name = self.class.queue_name_from_part([:queue]) if [:queue] self.priority = [:priority].to_i if [:priority] self end |
#successfully_enqueued? ⇒ Boolean
58 59 60 |
# File 'lib/active_job/core.rb', line 58 def successfully_enqueued? @successfully_enqueued end |