aboutsummaryrefslogtreecommitdiffstats
path: root/lib/active_job/enqueuing.rb
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2014-05-22 19:33:23 +0200
committerDavid Heinemeier Hansson <david@loudthinking.com>2014-05-22 19:33:23 +0200
commitc073ba339a6820625718f7320989cfa527534563 (patch)
treebcc70b5fcb6103f285659762b0670beabb598e19 /lib/active_job/enqueuing.rb
parentf82bc7e5515d439b5125d7596b56563249b2d83e (diff)
downloadrails-c073ba339a6820625718f7320989cfa527534563.tar.gz
rails-c073ba339a6820625718f7320989cfa527534563.tar.bz2
rails-c073ba339a6820625718f7320989cfa527534563.zip
Add callbacks, implement instrumentation as callbacks, and have the enqueue methods return a job instance
Diffstat (limited to 'lib/active_job/enqueuing.rb')
-rw-r--r--lib/active_job/enqueuing.rb82
1 files changed, 49 insertions, 33 deletions
diff --git a/lib/active_job/enqueuing.rb b/lib/active_job/enqueuing.rb
index e8f3272782..8b1f29ce77 100644
--- a/lib/active_job/enqueuing.rb
+++ b/lib/active_job/enqueuing.rb
@@ -2,42 +2,58 @@ require 'active_job/parameters'
module ActiveJob
module Enqueuing
- # 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.
- #
- # The return value is adapter-specific and may change in a future
- # ActiveJob release.
- def enqueue(*args)
- serialized_args = Parameters.serialize(args)
- instrument_enqueuing :enqueue, args: serialized_args
- queue_adapter.enqueue self, *serialized_args
- end
+ 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, *Parameters.serialize(args)
+ end
+ end
+ end
- # Enqueue a job to be performed at +interval+ from now.
- #
- # enqueue_in(1.week, "mike")
- #
- # Returns truthy if a job was scheduled.
- def enqueue_in(interval, *args)
- enqueue_at(interval.seconds.from_now, *args)
- 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
- # Enqueue a job to be performed at an explicit point in time.
- #
- # enqueue_at(Date.tomorrow.midnight, "mike")
- #
- # Returns truthy if a job was scheduled.
- def enqueue_at(timestamp, *args)
- serialized_args = Parameters.serialize(args)
- instrument_enqueuing :enqueue_at, args: serialized_args, timestamp: timestamp
- queue_adapter.enqueue_at self, timestamp.to_f, *serialized_args
+ job.run_callbacks :enqueue do
+ queue_adapter.enqueue_at self, timestamp.to_f, *Parameters.serialize(args)
+ end
+ end
+ end
end
- private
- def instrument_enqueuing(method_name, options = {})
- ActiveSupport::Notifications.instrument "#{method_name}.active_job", options.merge(adapter: queue_adapter, job: self)
- end
+ included do
+ attr_accessor :arguments
+ attr_accessor :enqueued_at
+ end
+
+ def initialize(arguments = nil)
+ @arguments = arguments
+ end
end
end