1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activejob/lib/active_job/enqueuing.rb

83 lines
2.8 KiB
Ruby
Raw Normal View History

require 'active_job/arguments'
2014-05-19 06:06:09 -04:00
module ActiveJob
# Provides behavior for enqueuing and retrying jobs.
2014-05-19 06:06:09 -04:00
module Enqueuing
extend ActiveSupport::Concern
2014-05-30 19:19:30 -04:00
# Includes the +perform_later+ method for job initialization.
module ClassMethods
# Push a job onto the queue. The arguments must be legal JSON types
# (string, int, float, nil, true, false, hash or array) or
# GlobalID::Identification instances. Arbitrary Ruby objects
# are not supported.
#
# Returns an instance of the job class queued with arguments available in
# Job#arguments.
2014-08-25 10:34:50 -04:00
def perform_later(*args)
job_or_instantiate(*args).enqueue
end
2014-08-25 10:34:50 -04:00
protected
def job_or_instantiate(*args)
args.first.is_a?(self) ? args.first : new(*args)
end
end
2014-05-30 19:19:30 -04:00
# Reschedules the job to be re-executed. This is useful in combination
2014-08-25 10:34:50 -04:00
# with the +rescue_from+ option. When you rescue an exception from your job
# you can ask Active Job to retry performing your job.
#
# ==== Options
# * <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
#
# class SiteScrapperJob < ActiveJob::Base
# rescue_from(ErrorLoadingSite) do
# retry_job queue: :low_priority
# end
#
2014-08-25 10:34:50 -04:00
# def perform(*args)
# # raise ErrorLoadingSite if cannot scrape
# end
# end
def retry_job(options={})
enqueue options
end
2014-05-30 19:19:30 -04:00
# Enqueues the job to be performed by the queue adapter.
2014-08-25 10:34:50 -04:00
#
# ==== Options
# * <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
# my_job_instance.enqueue wait: 5.minutes
2014-08-25 10:34:50 -04:00
# my_job_instance.enqueue queue: :important
# my_job_instance.enqueue wait_until: Date.tomorrow.midnight
2015-03-18 05:48:26 -04:00
# my_job_instance.enqueue priority: 10
2014-08-25 10:34:50 -04:00
def enqueue(options={})
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]
2014-08-25 10:34:50 -04:00
run_callbacks :enqueue do
if self.scheduled_at
self.class.queue_adapter.enqueue_at self, self.scheduled_at
else
self.class.queue_adapter.enqueue self
end
end
self
end
2014-05-19 06:06:09 -04:00
end
2014-05-19 16:13:40 -04:00
end