From 1e237b4e44b7de564c7d6b331dd2f2243c4113fd Mon Sep 17 00:00:00 2001 From: Cristian Bica Date: Mon, 25 Aug 2014 17:34:50 +0300 Subject: Active Job refactoring --- activejob/lib/active_job/enqueuing.rb | 98 ++++++++++++++++++----------------- 1 file changed, 51 insertions(+), 47 deletions(-) (limited to 'activejob/lib/active_job/enqueuing.rb') diff --git a/activejob/lib/active_job/enqueuing.rb b/activejob/lib/active_job/enqueuing.rb index 3d00d51867..32453930cc 100644 --- a/activejob/lib/active_job/enqueuing.rb +++ b/activejob/lib/active_job/enqueuing.rb @@ -12,60 +12,64 @@ module ActiveJob # # Returns an instance of the job class queued with args available in # Job#arguments. - def enqueue(*args) - new(args).tap do |job| - job.run_callbacks :enqueue do - queue_adapter.enqueue self, job.job_id, *Arguments.serialize(args) - end - end - end - - # Enqueue a job to be performed at +interval+ from now. - # - # enqueue_in(1.week, "mike") - # - # Returns an instance of the job class queued with args available in - # Job#arguments and the timestamp in Job#enqueue_at. - def enqueue_in(interval, *args) - enqueue_at interval.seconds.from_now, *args + def perform_later(*args) + job_or_instantiate(*args).enqueue end - # Enqueue a job to be performed at an explicit point in time. - # - # enqueue_at(Date.tomorrow.midnight, "mike") - # - # Returns an instance of the job class queued with args available in - # Job#arguments and the timestamp in Job#enqueue_at. - def enqueue_at(timestamp, *args) - new(args).tap do |job| - job.enqueued_at = timestamp - - job.run_callbacks :enqueue do - queue_adapter.enqueue_at self, timestamp.to_f, job.job_id, *Arguments.serialize(args) - end + protected + def job_or_instantiate(*args) + args.first.is_a?(self) ? args.first : new(*args) end - end - end - - included do - attr_accessor :arguments - attr_accessor :enqueued_at - end - - def initialize(arguments = nil) - @arguments = arguments end - def retry_now - self.class.enqueue(*arguments) + # Reschedule the job to be re-executed. This is usefull in combination + # with the +rescue_from+ option. When you rescue an exception from your job + # you can ask Active Job to retry performing your job. + # + # ==== Options + # * :in - Enqueues the job with the specified delay + # * :at - Enqueues the job at the time specified + # * :queue - Enqueues the job on the specified queue + # + # ==== Examples + # + # class SiteScrapperJob < ActiveJob::Base + # rescue_from(ErrorLoadingSite) do + # retry_job queue: :low_priority + # end + # def perform(*args) + # # raise ErrorLoadingSite if cannot scrape + # end + # end + def retry_job(options={}) + enqueue options end - def retry_in(interval) - self.class.enqueue_in interval, *arguments - end - - def retry_at(timestamp) - self.class.enqueue_at timestamp, *arguments + # Equeue the job to be performed by the queue adapter. + # + # ==== Options + # * :in - Enqueues the job with the specified delay + # * :at - Enqueues the job at the time specified + # * :queue - Enqueues the job on the specified queue + # + # ==== Examples + # + # my_job_instance.enqueue + # my_job_instance.enqueue in: 5.minutes + # my_job_instance.enqueue queue: :important + # my_job_instance.enqueue at: Date.tomorrow.midnight + def enqueue(options={}) + self.scheduled_at = options[:in].seconds.from_now.to_f if options[:in] + self.scheduled_at = options[:at].to_f if options[:at] + self.queue_name = self.class.queue_name_from_part(options[:queue]) if options[:queue] + 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 end end -- cgit v1.2.3 From 15ddf60e05f5995b1aaf8d8cecfa2354005cc035 Mon Sep 17 00:00:00 2001 From: Cristian Bica Date: Thu, 4 Sep 2014 08:08:06 +0300 Subject: Rename remaining :in / :at to :wait / :wait_until --- activejob/lib/active_job/enqueuing.rb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'activejob/lib/active_job/enqueuing.rb') diff --git a/activejob/lib/active_job/enqueuing.rb b/activejob/lib/active_job/enqueuing.rb index 32453930cc..e8bc44cbc4 100644 --- a/activejob/lib/active_job/enqueuing.rb +++ b/activejob/lib/active_job/enqueuing.rb @@ -27,8 +27,8 @@ module ActiveJob # you can ask Active Job to retry performing your job. # # ==== Options - # * :in - Enqueues the job with the specified delay - # * :at - Enqueues the job at the time specified + # * :wait - Enqueues the job with the specified delay + # * :wait_until - Enqueues the job at the time specified # * :queue - Enqueues the job on the specified queue # # ==== Examples @@ -48,19 +48,19 @@ module ActiveJob # Equeue the job to be performed by the queue adapter. # # ==== Options - # * :in - Enqueues the job with the specified delay - # * :at - Enqueues the job at the time specified + # * :wait - Enqueues the job with the specified delay + # * :wait_until - Enqueues the job at the time specified # * :queue - Enqueues the job on the specified queue # # ==== Examples # # my_job_instance.enqueue - # my_job_instance.enqueue in: 5.minutes + # my_job_instance.enqueue wait: 5.minutes # my_job_instance.enqueue queue: :important - # my_job_instance.enqueue at: Date.tomorrow.midnight + # my_job_instance.enqueue wait_until: Date.tomorrow.midnight def enqueue(options={}) - self.scheduled_at = options[:in].seconds.from_now.to_f if options[:in] - self.scheduled_at = options[:at].to_f if options[:at] + 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] self.queue_name = self.class.queue_name_from_part(options[:queue]) if options[:queue] run_callbacks :enqueue do if self.scheduled_at -- cgit v1.2.3