2017-07-09 13:49:52 -04:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 09:40:03 -04:00
|
|
|
|
2014-08-25 10:34:50 -04:00
|
|
|
module ActiveJob
|
2015-04-29 21:46:12 -04:00
|
|
|
# Provides general behavior that will be included into every Active Job
|
|
|
|
# object that inherits from ActiveJob::Base.
|
2014-08-25 10:34:50 -04:00
|
|
|
module Core
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2018-10-05 16:42:27 -04:00
|
|
|
# Job arguments
|
|
|
|
attr_accessor :arguments
|
|
|
|
attr_writer :serialized_arguments
|
2014-08-25 10:34:50 -04:00
|
|
|
|
2018-10-05 16:42:27 -04:00
|
|
|
# Timestamp when the job should be performed
|
|
|
|
attr_accessor :scheduled_at
|
2014-08-25 10:34:50 -04:00
|
|
|
|
2018-10-05 16:42:27 -04:00
|
|
|
# Job Identifier
|
|
|
|
attr_accessor :job_id
|
2014-08-25 10:34:50 -04:00
|
|
|
|
2018-10-05 16:42:27 -04:00
|
|
|
# Queue in which the job will reside.
|
|
|
|
attr_writer :queue_name
|
2015-04-26 13:05:08 -04:00
|
|
|
|
2018-10-05 16:42:27 -04:00
|
|
|
# Priority that the job will have (lower is more priority).
|
|
|
|
attr_writer :priority
|
2015-03-18 05:48:26 -04:00
|
|
|
|
2018-10-05 16:42:27 -04:00
|
|
|
# ID optionally provided by adapter
|
|
|
|
attr_accessor :provider_job_id
|
2015-07-07 15:52:28 -04:00
|
|
|
|
2018-10-05 16:42:27 -04:00
|
|
|
# Number of times this job has been executed (which increments on every retry, like after an exception).
|
|
|
|
attr_accessor :executions
|
2016-07-29 16:54:55 -04:00
|
|
|
|
2018-11-23 14:31:14 -05:00
|
|
|
# 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.
|
|
|
|
attr_accessor :exception_executions
|
|
|
|
|
2018-10-05 16:42:27 -04:00
|
|
|
# I18n.locale to be used during the job.
|
|
|
|
attr_accessor :locale
|
2018-02-22 09:14:42 -05:00
|
|
|
|
2018-10-05 16:42:27 -04:00
|
|
|
# Timezone to be used during the job.
|
|
|
|
attr_accessor :timezone
|
2014-08-25 10:34:50 -04:00
|
|
|
|
2019-02-25 04:18:39 -05:00
|
|
|
# Track when a job was enqueued
|
2019-02-12 12:49:12 -05:00
|
|
|
attr_accessor :enqueued_at
|
|
|
|
|
2021-01-19 09:57:46 -05:00
|
|
|
# Track whether the adapter received the job successfully.
|
|
|
|
attr_writer :successfully_enqueued # :nodoc:
|
|
|
|
|
|
|
|
def successfully_enqueued?
|
|
|
|
@successfully_enqueued
|
|
|
|
end
|
|
|
|
|
|
|
|
# Track any exceptions raised by the backend so callers can inspect the errors.
|
|
|
|
attr_accessor :enqueue_error
|
|
|
|
|
2014-11-03 22:22:11 -05:00
|
|
|
# These methods will be included into any Active Job object, adding
|
|
|
|
# helpers for de/serialization and creation of job instances.
|
2014-08-25 10:34:50 -04:00
|
|
|
module ClassMethods
|
|
|
|
# Creates a new job instance from a hash created with +serialize+
|
|
|
|
def deserialize(job_data)
|
2016-08-06 12:40:07 -04:00
|
|
|
job = job_data["job_class"].constantize.new
|
2014-12-30 13:26:39 -05:00
|
|
|
job.deserialize(job_data)
|
2014-08-25 10:34:50 -04:00
|
|
|
job
|
|
|
|
end
|
|
|
|
|
|
|
|
# Creates a job preconfigured with the given options. You can call
|
|
|
|
# perform_later with the job arguments to enqueue the job with the
|
|
|
|
# preconfigured options
|
|
|
|
#
|
|
|
|
# ==== Options
|
|
|
|
# * <tt>:wait</tt> - Enqueues the job with the specified delay
|
|
|
|
# * <tt>:wait_until</tt> - Enqueues the job at the time specified
|
|
|
|
# * <tt>:queue</tt> - Enqueues the job on the specified queue
|
2015-03-18 05:48:26 -04:00
|
|
|
# * <tt>:priority</tt> - Enqueues the job with the specified priority
|
2014-08-25 10:34:50 -04:00
|
|
|
#
|
|
|
|
# ==== Examples
|
|
|
|
#
|
|
|
|
# VideoJob.set(queue: :some_queue).perform_later(Video.last)
|
|
|
|
# VideoJob.set(wait: 5.minutes).perform_later(Video.last)
|
2014-09-27 02:07:24 -04:00
|
|
|
# VideoJob.set(wait_until: Time.now.tomorrow).perform_later(Video.last)
|
2014-08-25 10:34:50 -04:00
|
|
|
# VideoJob.set(queue: :some_queue, wait: 5.minutes).perform_later(Video.last)
|
2014-09-27 02:07:24 -04:00
|
|
|
# VideoJob.set(queue: :some_queue, wait_until: Time.now.tomorrow).perform_later(Video.last)
|
2015-03-18 05:48:26 -04:00
|
|
|
# VideoJob.set(queue: :some_queue, wait: 5.minutes, priority: 10).perform_later(Video.last)
|
2016-10-28 23:05:58 -04:00
|
|
|
def set(options = {})
|
2014-08-25 10:34:50 -04:00
|
|
|
ConfiguredJob.new(self, options)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-10-31 14:46:43 -04:00
|
|
|
# Creates a new job instance. Takes the arguments that will be
|
|
|
|
# passed to the perform method.
|
2014-08-25 10:34:50 -04:00
|
|
|
def initialize(*arguments)
|
|
|
|
@arguments = arguments
|
|
|
|
@job_id = SecureRandom.uuid
|
|
|
|
@queue_name = self.class.queue_name
|
2015-03-18 05:48:26 -04:00
|
|
|
@priority = self.class.priority
|
2016-07-29 16:54:55 -04:00
|
|
|
@executions = 0
|
2019-01-07 11:18:17 -05:00
|
|
|
@exception_executions = {}
|
2020-06-04 00:19:22 -04:00
|
|
|
@timezone = Time.zone&.name
|
2014-08-25 10:34:50 -04:00
|
|
|
end
|
2020-01-18 21:35:55 -05:00
|
|
|
ruby2_keywords(:initialize) if respond_to?(:ruby2_keywords, true)
|
2014-08-25 10:34:50 -04:00
|
|
|
|
|
|
|
# Returns a hash with the job data that can safely be passed to the
|
2018-11-18 23:36:44 -05:00
|
|
|
# queuing adapter.
|
2014-08-25 10:34:50 -04:00
|
|
|
def serialize
|
|
|
|
{
|
2016-08-06 12:40:07 -04:00
|
|
|
"job_class" => self.class.name,
|
|
|
|
"job_id" => job_id,
|
2016-09-22 11:17:28 -04:00
|
|
|
"provider_job_id" => provider_job_id,
|
2016-08-06 12:40:07 -04:00
|
|
|
"queue_name" => queue_name,
|
|
|
|
"priority" => priority,
|
2018-05-01 13:33:50 -04:00
|
|
|
"arguments" => serialize_arguments_if_needed(arguments),
|
2016-08-06 12:40:07 -04:00
|
|
|
"executions" => executions,
|
2018-11-23 14:31:14 -05:00
|
|
|
"exception_executions" => exception_executions,
|
2018-02-22 09:14:42 -05:00
|
|
|
"locale" => I18n.locale.to_s,
|
2020-06-04 00:19:22 -04:00
|
|
|
"timezone" => timezone,
|
2019-02-12 12:49:12 -05:00
|
|
|
"enqueued_at" => Time.now.utc.iso8601
|
2014-08-25 10:34:50 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2014-12-30 13:26:39 -05:00
|
|
|
# Attaches the stored job data to the current instance. Receives a hash
|
|
|
|
# returned from +serialize+
|
|
|
|
#
|
|
|
|
# ==== Examples
|
|
|
|
#
|
|
|
|
# class DeliverWebhookJob < ActiveJob::Base
|
2017-12-05 01:25:12 -05:00
|
|
|
# attr_writer :attempt_number
|
|
|
|
#
|
|
|
|
# def attempt_number
|
|
|
|
# @attempt_number ||= 0
|
|
|
|
# end
|
|
|
|
#
|
2014-12-30 13:26:39 -05:00
|
|
|
# def serialize
|
2017-12-05 01:25:12 -05:00
|
|
|
# super.merge('attempt_number' => attempt_number + 1)
|
2014-12-30 13:26:39 -05:00
|
|
|
# end
|
|
|
|
#
|
|
|
|
# def deserialize(job_data)
|
2014-12-31 10:31:25 -05:00
|
|
|
# super
|
2017-12-05 01:25:12 -05:00
|
|
|
# self.attempt_number = job_data['attempt_number']
|
2014-12-30 13:26:39 -05:00
|
|
|
# end
|
|
|
|
#
|
2017-12-05 01:25:12 -05:00
|
|
|
# rescue_from(Timeout::Error) do |exception|
|
|
|
|
# raise exception if attempt_number > 5
|
2014-12-30 13:26:39 -05:00
|
|
|
# retry_job(wait: 10)
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
def deserialize(job_data)
|
2016-08-06 12:40:07 -04:00
|
|
|
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"]
|
2018-11-23 14:31:14 -05:00
|
|
|
self.exception_executions = job_data["exception_executions"]
|
2016-08-06 12:40:07 -04:00
|
|
|
self.locale = job_data["locale"] || I18n.locale.to_s
|
2019-08-01 03:41:26 -04:00
|
|
|
self.timezone = job_data["timezone"] || Time.zone&.name
|
2019-02-12 12:49:12 -05:00
|
|
|
self.enqueued_at = job_data["enqueued_at"]
|
2014-12-30 13:26:39 -05:00
|
|
|
end
|
|
|
|
|
2014-08-25 10:34:50 -04:00
|
|
|
private
|
2018-05-01 13:33:50 -04:00
|
|
|
def serialize_arguments_if_needed(arguments)
|
|
|
|
if arguments_serialized?
|
|
|
|
@serialized_arguments
|
|
|
|
else
|
|
|
|
serialize_arguments(arguments)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-08-25 10:34:50 -04:00
|
|
|
def deserialize_arguments_if_needed
|
2018-05-01 13:33:50 -04:00
|
|
|
if arguments_serialized?
|
2014-08-25 10:34:50 -04:00
|
|
|
@arguments = deserialize_arguments(@serialized_arguments)
|
|
|
|
@serialized_arguments = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-01 13:33:50 -04:00
|
|
|
def serialize_arguments(arguments)
|
|
|
|
Arguments.serialize(arguments)
|
2014-08-25 10:34:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def deserialize_arguments(serialized_args)
|
|
|
|
Arguments.deserialize(serialized_args)
|
|
|
|
end
|
2018-05-01 13:33:50 -04:00
|
|
|
|
|
|
|
def arguments_serialized?
|
|
|
|
defined?(@serialized_arguments) && @serialized_arguments
|
|
|
|
end
|
2014-08-25 10:34:50 -04:00
|
|
|
end
|
|
|
|
end
|