diff options
author | Tamir Duberstein <tamird@squareup.com> | 2014-09-23 20:43:12 -0700 |
---|---|---|
committer | Tamir Duberstein <tamird@gmail.com> | 2015-03-11 22:45:59 -0700 |
commit | 1f8558fa2707e7707dcfef0aba94de9afcd05d3a (patch) | |
tree | 14eac3acdc47d2c667b63b2baedb2ec479e57fe7 /activejob/lib | |
parent | 7884c8f88e6bf64fe228141720183297ec2fce02 (diff) | |
download | rails-1f8558fa2707e7707dcfef0aba94de9afcd05d3a.tar.gz rails-1f8558fa2707e7707dcfef0aba94de9afcd05d3a.tar.bz2 rails-1f8558fa2707e7707dcfef0aba94de9afcd05d3a.zip |
`ActiveJob::Base#queue_adapter` is now a `class_attribute`
This allows different `queue_adapters` to be used in each `ActiveJob`
class heirarchy. Previously, all subclasses used a single global queue
adapter.
Diffstat (limited to 'activejob/lib')
-rw-r--r-- | activejob/lib/active_job/queue_adapter.rb | 30 | ||||
-rw-r--r-- | activejob/lib/active_job/test_helper.rb | 18 |
2 files changed, 36 insertions, 12 deletions
diff --git a/activejob/lib/active_job/queue_adapter.rb b/activejob/lib/active_job/queue_adapter.rb index 23b5f7de75..f3a3d27193 100644 --- a/activejob/lib/active_job/queue_adapter.rb +++ b/activejob/lib/active_job/queue_adapter.rb @@ -1,4 +1,5 @@ require 'active_job/queue_adapters/inline_adapter' +require 'active_support/core_ext/class/attribute' require 'active_support/core_ext/string/inflections' module ActiveJob @@ -7,34 +8,45 @@ module ActiveJob module QueueAdapter #:nodoc: extend ActiveSupport::Concern + included do + class_attribute :_queue_adapter, instance_accessor: false, instance_predicate: false + self.queue_adapter = :inline + end + # Includes the setter method for changing the active queue adapter. module ClassMethods - mattr_reader(:queue_adapter) { ActiveJob::QueueAdapters::InlineAdapter } + def queue_adapter + _queue_adapter + end # Specify the backend queue provider. The default queue adapter # is the :inline queue. See QueueAdapters for more # information. def queue_adapter=(name_or_adapter_or_class) + self._queue_adapter = interpret_adapter(name_or_adapter_or_class) + end + + private + + def interpret_adapter(name_or_adapter_or_class) case name_or_adapter_or_class when Symbol, String - self.queue_adapter = load_adapter(name_or_adapter_or_class) + load_adapter(name_or_adapter_or_class) else - @@queue_adapter = if queue_adapter?(name_or_adapter_or_class) + if queue_adapter?(name_or_adapter_or_class) name_or_adapter_or_class elsif queue_adapter_class?(name_or_adapter_or_class) ActiveSupport::Deprecation.warn "Passing an adapter class is deprecated " \ - "and will be removed in Rails 5.1. Please pass an adapter name " \ - "(.queue_adapter = :#{name_or_adapter_or_class.name.demodulize.remove('Adapter').underscore}) " \ - "or an instance (.queue_adapter = #{name_or_adapter_or_class.name}.new) instead." - name_or_adapter_or_class.new + "and will be removed in Rails 5.1. Please pass an adapter name " \ + "(.queue_adapter = :#{name_or_adapter_or_class.name.demodulize.remove('Adapter').underscore}) " \ + "or an instance (.queue_adapter = #{name_or_adapter_or_class.name}.new) instead." + name_or_adapter_or_class.new else raise ArgumentError end end end - private - QUEUE_ADAPTER_METHODS = [:enqueue, :enqueue_at].freeze def queue_adapter?(object) diff --git a/activejob/lib/active_job/test_helper.rb b/activejob/lib/active_job/test_helper.rb index 5e309c1dca..4efb4b72d2 100644 --- a/activejob/lib/active_job/test_helper.rb +++ b/activejob/lib/active_job/test_helper.rb @@ -1,3 +1,4 @@ +require 'active_support/core_ext/class/subclasses' require 'active_support/core_ext/hash/keys' module ActiveJob @@ -7,8 +8,17 @@ module ActiveJob included do def before_setup - @old_queue_adapter = queue_adapter - ActiveJob::Base.queue_adapter = :test + test_adapter = ActiveJob::QueueAdapters::TestAdapter.new + + @old_queue_adapters = (ActiveJob::Base.subclasses << ActiveJob::Base).select do |klass| + # only override explicitly set adapters, a quirk of `class_attribute` + klass.singleton_class.public_instance_methods(false).include?(:_queue_adapter) + end.map do |klass| + [klass, klass.queue_adapter].tap do + klass.queue_adapter = test_adapter + end + end + clear_enqueued_jobs clear_performed_jobs super @@ -16,7 +26,9 @@ module ActiveJob def after_teardown super - ActiveJob::Base.queue_adapter = @old_queue_adapter + @old_queue_adapters.each do |(klass, adapter)| + klass.queue_adapter = adapter + end end # Asserts that the number of enqueued jobs matches the given number. |