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

Provides friendlier way to access queue adapters of a job.

- removed predicate method. Used only reader.
This commit is contained in:
Mohit Natoo 2016-05-17 01:43:08 +05:30
parent 3beb5e53ed
commit 673606a962
2 changed files with 22 additions and 4 deletions

View file

@ -7,6 +7,7 @@ module ActiveJob
extend ActiveSupport::Concern extend ActiveSupport::Concern
included do included do
class_attribute :_queue_adapter_name, instance_accessor: false, instance_predicate: false
class_attribute :_queue_adapter, instance_accessor: false, instance_predicate: false class_attribute :_queue_adapter, instance_accessor: false, instance_predicate: false
self.queue_adapter = :async self.queue_adapter = :async
end end
@ -19,11 +20,15 @@ module ActiveJob
_queue_adapter _queue_adapter
end end
def queue_adapter_name
_queue_adapter_name
end
# Specify the backend queue provider. The default queue adapter # Specify the backend queue provider. The default queue adapter
# is the +:async+ queue. See QueueAdapters for more # is the +:async+ queue. See QueueAdapters for more
# information. # information.
def queue_adapter=(name_or_adapter_or_class) def queue_adapter=(name_or_adapter_or_class)
self._queue_adapter = interpret_adapter(name_or_adapter_or_class) interpret_adapter(name_or_adapter_or_class)
end end
private private
@ -31,16 +36,24 @@ module ActiveJob
def interpret_adapter(name_or_adapter_or_class) def interpret_adapter(name_or_adapter_or_class)
case name_or_adapter_or_class case name_or_adapter_or_class
when Symbol, String when Symbol, String
ActiveJob::QueueAdapters.lookup(name_or_adapter_or_class).new assign_adapter(name_or_adapter_or_class.to_s,
ActiveJob::QueueAdapters.lookup(name_or_adapter_or_class).new)
else else
if queue_adapter?(name_or_adapter_or_class) if queue_adapter?(name_or_adapter_or_class)
name_or_adapter_or_class adapter_name = "#{name_or_adapter_or_class.class.name.demodulize.remove('Adapter').underscore}"
assign_adapter(adapter_name,
name_or_adapter_or_class)
else else
raise ArgumentError raise ArgumentError
end end
end end
end end
def assign_adapter(adapter_name, queue_adapter)
self._queue_adapter_name = ActiveSupport::StringInquirer.new(adapter_name)
self._queue_adapter = queue_adapter
end
QUEUE_ADAPTER_METHODS = [:enqueue, :enqueue_at].freeze QUEUE_ADAPTER_METHODS = [:enqueue, :enqueue_at].freeze
def queue_adapter?(object) def queue_adapter?(object)

View file

@ -25,13 +25,18 @@ class QueueAdapterTest < ActiveJob::TestCase
base_queue_adapter = ActiveJob::Base.queue_adapter base_queue_adapter = ActiveJob::Base.queue_adapter
child_job_one = Class.new(ActiveJob::Base) child_job_one = Class.new(ActiveJob::Base)
assert_equal child_job_one.queue_adapter_name, ActiveJob::Base.queue_adapter_name
child_job_one.queue_adapter = :stub_one child_job_one.queue_adapter = :stub_one
assert_not_equal ActiveJob::Base.queue_adapter, child_job_one.queue_adapter assert_equal "stub_one", child_job_one.queue_adapter_name
assert child_job_one.queue_adapter_name.stub_one?
assert_kind_of ActiveJob::QueueAdapters::StubOneAdapter, child_job_one.queue_adapter assert_kind_of ActiveJob::QueueAdapters::StubOneAdapter, child_job_one.queue_adapter
child_job_two = Class.new(ActiveJob::Base) child_job_two = Class.new(ActiveJob::Base)
child_job_two.queue_adapter = :stub_two child_job_two.queue_adapter = :stub_two
assert child_job_two.queue_adapter_name.stub_two?
assert_equal "stub_two", child_job_two.queue_adapter_name
assert_kind_of ActiveJob::QueueAdapters::StubTwoAdapter, child_job_two.queue_adapter assert_kind_of ActiveJob::QueueAdapters::StubTwoAdapter, child_job_two.queue_adapter
assert_kind_of ActiveJob::QueueAdapters::StubOneAdapter, child_job_one.queue_adapter, "child_job_one's queue adapter should remain unchanged" assert_kind_of ActiveJob::QueueAdapters::StubOneAdapter, child_job_one.queue_adapter, "child_job_one's queue adapter should remain unchanged"