2017-07-09 13:49:52 -04:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 09:40:03 -04:00
|
|
|
|
2018-07-01 07:22:44 -04:00
|
|
|
require "active_job/arguments"
|
|
|
|
|
2014-05-19 06:06:09 -04:00
|
|
|
module ActiveJob
|
2016-07-29 16:54:55 -04:00
|
|
|
# Provides behavior for enqueuing jobs.
|
2021-01-19 09:57:46 -05:00
|
|
|
|
|
|
|
# Can be raised by adapters if they wish to communicate to the caller a reason
|
|
|
|
# why the adapter was unexpectedly unable to enqueue a job.
|
|
|
|
class EnqueueError < StandardError; end
|
|
|
|
|
2014-05-19 06:06:09 -04:00
|
|
|
module Enqueuing
|
2014-05-22 13:33:23 -04:00
|
|
|
extend ActiveSupport::Concern
|
2014-05-30 19:19:30 -04:00
|
|
|
|
2014-11-03 22:31:31 -05:00
|
|
|
# Includes the +perform_later+ method for job initialization.
|
2014-05-22 13:33:23 -04:00
|
|
|
module ClassMethods
|
2018-11-08 18:04:26 -05:00
|
|
|
# Push a job onto the queue. By default the arguments must be either String,
|
|
|
|
# Integer, Float, NilClass, TrueClass, FalseClass, BigDecimal, Symbol, Date,
|
|
|
|
# Time, DateTime, ActiveSupport::TimeWithZone, ActiveSupport::Duration,
|
2021-05-16 03:44:59 -04:00
|
|
|
# Hash, ActiveSupport::HashWithIndifferentAccess, Array, Range or
|
2018-11-08 18:04:26 -05:00
|
|
|
# GlobalID::Identification instances, although this can be extended by adding
|
|
|
|
# custom serializers.
|
2014-05-22 13:33:23 -04:00
|
|
|
#
|
2014-10-20 15:10:59 -04:00
|
|
|
# Returns an instance of the job class queued with arguments available in
|
2021-01-19 09:57:46 -05:00
|
|
|
# Job#arguments or false if the enqueue did not succeed.
|
|
|
|
#
|
|
|
|
# After the attempted enqueue, the job will be yielded to an optional block.
|
2021-03-19 11:53:06 -04:00
|
|
|
def perform_later(...)
|
|
|
|
job = job_or_instantiate(...)
|
2021-01-19 09:57:46 -05:00
|
|
|
enqueue_result = job.enqueue
|
|
|
|
|
|
|
|
yield job if block_given?
|
|
|
|
|
|
|
|
enqueue_result
|
2014-05-22 13:33:23 -04:00
|
|
|
end
|
|
|
|
|
2016-12-22 05:03:42 -05:00
|
|
|
private
|
2021-04-12 14:49:54 -04:00
|
|
|
def job_or_instantiate(*args) # :doc:
|
2014-08-25 10:34:50 -04:00
|
|
|
args.first.is_a?(self) ? args.first : new(*args)
|
2014-05-22 13:33:23 -04:00
|
|
|
end
|
2021-04-12 14:49:54 -04:00
|
|
|
ruby2_keywords(:job_or_instantiate)
|
2014-05-22 13:33:23 -04:00
|
|
|
end
|
2014-05-30 19:19:30 -04:00
|
|
|
|
2014-09-17 15:46:53 -04:00
|
|
|
# Enqueues the job to be performed by the queue adapter.
|
2014-08-25 10:34:50 -04:00
|
|
|
#
|
|
|
|
# ==== Options
|
2014-09-04 01:08:06 -04:00
|
|
|
# * <tt>:wait</tt> - Enqueues the job with the specified delay
|
|
|
|
# * <tt>:wait_until</tt> - Enqueues the job at the time specified
|
2014-08-25 10:34:50 -04:00
|
|
|
# * <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
|
|
|
|
#
|
|
|
|
# my_job_instance.enqueue
|
2014-09-04 01:08:06 -04:00
|
|
|
# my_job_instance.enqueue wait: 5.minutes
|
2014-08-25 10:34:50 -04:00
|
|
|
# my_job_instance.enqueue queue: :important
|
2014-09-04 01:08:06 -04:00
|
|
|
# my_job_instance.enqueue wait_until: Date.tomorrow.midnight
|
2015-03-18 05:48:26 -04:00
|
|
|
# my_job_instance.enqueue priority: 10
|
2016-10-28 23:05:58 -04:00
|
|
|
def enqueue(options = {})
|
2014-09-04 01:08:06 -04:00
|
|
|
self.scheduled_at = options[:wait].seconds.from_now.to_f if options[:wait]
|
|
|
|
self.scheduled_at = options[:wait_until].to_f if options[:wait_until]
|
2014-08-25 10:34:50 -04:00
|
|
|
self.queue_name = self.class.queue_name_from_part(options[:queue]) if options[:queue]
|
2015-03-18 05:48:26 -04:00
|
|
|
self.priority = options[:priority].to_i if options[:priority]
|
2021-01-19 09:57:46 -05:00
|
|
|
self.successfully_enqueued = false
|
2018-12-05 13:37:48 -05:00
|
|
|
|
2014-08-25 10:34:50 -04:00
|
|
|
run_callbacks :enqueue do
|
2016-08-07 19:05:28 -04:00
|
|
|
if scheduled_at
|
2019-09-23 16:53:05 -04:00
|
|
|
queue_adapter.enqueue_at self, scheduled_at
|
2014-08-25 10:34:50 -04:00
|
|
|
else
|
2019-09-23 16:53:05 -04:00
|
|
|
queue_adapter.enqueue self
|
2014-08-25 10:34:50 -04:00
|
|
|
end
|
2018-12-05 13:37:48 -05:00
|
|
|
|
2021-01-19 09:57:46 -05:00
|
|
|
self.successfully_enqueued = true
|
|
|
|
rescue EnqueueError => e
|
|
|
|
self.enqueue_error = e
|
2018-09-26 12:25:20 -04:00
|
|
|
end
|
2018-12-05 13:37:48 -05:00
|
|
|
|
2021-01-19 09:57:46 -05:00
|
|
|
if successfully_enqueued?
|
2018-09-26 12:25:20 -04:00
|
|
|
self
|
|
|
|
else
|
2020-05-06 20:44:44 -04:00
|
|
|
false
|
2014-08-25 10:34:50 -04:00
|
|
|
end
|
2014-05-22 14:35:02 -04:00
|
|
|
end
|
2014-05-19 06:06:09 -04:00
|
|
|
end
|
2014-05-19 16:13:40 -04:00
|
|
|
end
|