diff options
author | Abdelkader Boudih <terminale@gmail.com> | 2014-08-12 09:17:19 +0000 |
---|---|---|
committer | Abdelkader Boudih <terminale@gmail.com> | 2014-08-12 09:17:19 +0000 |
commit | a75f085941b2d6aed160f5f2f7e64e5fc7e03826 (patch) | |
tree | 23174b264983a15c3108ec912b355829c5f17a80 /activejob/lib/active_job/enqueuing.rb | |
parent | b45b99894a60eda434abec94d133a1cfd8de2dda (diff) | |
parent | 14f74a8331f94150dfee653224de8fc837797709 (diff) | |
download | rails-a75f085941b2d6aed160f5f2f7e64e5fc7e03826.tar.gz rails-a75f085941b2d6aed160f5f2f7e64e5fc7e03826.tar.bz2 rails-a75f085941b2d6aed160f5f2f7e64e5fc7e03826.zip |
Add 'activejob/' from commit '14f74a8331f94150dfee653224de8fc837797709'
git-subtree-dir: activejob
git-subtree-mainline: b45b99894a60eda434abec94d133a1cfd8de2dda
git-subtree-split: 14f74a8331f94150dfee653224de8fc837797709
Diffstat (limited to 'activejob/lib/active_job/enqueuing.rb')
-rw-r--r-- | activejob/lib/active_job/enqueuing.rb | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/activejob/lib/active_job/enqueuing.rb b/activejob/lib/active_job/enqueuing.rb new file mode 100644 index 0000000000..e3ac11ba97 --- /dev/null +++ b/activejob/lib/active_job/enqueuing.rb @@ -0,0 +1,71 @@ +require 'active_job/arguments' + +module ActiveJob + module Enqueuing + extend ActiveSupport::Concern + + 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 + # ActiveModel::GlobalIdentication instances. Arbitrary Ruby objects + # are not supported. + # + # 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 + 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 + 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 + end + + def retry_in(interval) + self.class.enqueue_in interval, *arguments + end + + def retry_at(timestamp) + self.class.enqueue_at timestamp, *arguments + end + end +end |