2017-07-09 13:49:52 -04:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 09:40:03 -04:00
|
|
|
|
2016-08-06 12:40:07 -04:00
|
|
|
require "active_support/core_ext/string/inflections"
|
2014-05-19 04:55:57 -04:00
|
|
|
|
|
|
|
module ActiveJob
|
2014-12-31 00:57:32 -05:00
|
|
|
# The <tt>ActiveJob::QueueAdapter</tt> module is used to load the
|
2016-02-05 10:05:48 -05:00
|
|
|
# correct adapter. The default queue adapter is the +:async+ queue.
|
2014-09-26 13:10:06 -04:00
|
|
|
module QueueAdapter #:nodoc:
|
2014-08-26 16:08:49 -04:00
|
|
|
extend ActiveSupport::Concern
|
2014-05-19 04:55:57 -04:00
|
|
|
|
2014-09-23 23:43:12 -04:00
|
|
|
included do
|
2016-05-16 16:13:08 -04:00
|
|
|
class_attribute :_queue_adapter_name, instance_accessor: false, instance_predicate: false
|
2014-09-23 23:43:12 -04:00
|
|
|
class_attribute :_queue_adapter, instance_accessor: false, instance_predicate: false
|
2016-02-05 09:35:37 -05:00
|
|
|
self.queue_adapter = :async
|
2014-09-23 23:43:12 -04:00
|
|
|
end
|
|
|
|
|
2014-11-03 22:25:44 -05:00
|
|
|
# Includes the setter method for changing the active queue adapter.
|
2014-08-26 16:08:49 -04:00
|
|
|
module ClassMethods
|
2015-06-01 17:24:04 -04:00
|
|
|
# Returns the backend queue provider. The default queue adapter
|
2016-02-05 09:35:37 -05:00
|
|
|
# is the +:async+ queue. See QueueAdapters for more information.
|
2014-09-23 23:43:12 -04:00
|
|
|
def queue_adapter
|
|
|
|
_queue_adapter
|
|
|
|
end
|
2014-05-19 04:55:57 -04:00
|
|
|
|
2016-05-16 16:13:08 -04:00
|
|
|
def queue_adapter_name
|
|
|
|
_queue_adapter_name
|
|
|
|
end
|
|
|
|
|
2014-09-26 13:10:06 -04:00
|
|
|
# Specify the backend queue provider. The default queue adapter
|
2016-02-05 09:35:37 -05:00
|
|
|
# is the +:async+ queue. See QueueAdapters for more
|
2014-09-26 13:10:06 -04:00
|
|
|
# information.
|
2015-03-11 17:57:13 -04:00
|
|
|
def queue_adapter=(name_or_adapter_or_class)
|
2016-05-16 16:13:08 -04:00
|
|
|
interpret_adapter(name_or_adapter_or_class)
|
2014-09-23 23:43:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def interpret_adapter(name_or_adapter_or_class)
|
|
|
|
case name_or_adapter_or_class
|
|
|
|
when Symbol, String
|
2016-05-16 16:13:08 -04:00
|
|
|
assign_adapter(name_or_adapter_or_class.to_s,
|
|
|
|
ActiveJob::QueueAdapters.lookup(name_or_adapter_or_class).new)
|
2014-12-30 10:53:42 -05:00
|
|
|
else
|
2016-08-06 13:55:02 -04:00
|
|
|
if queue_adapter?(name_or_adapter_or_class)
|
2016-05-16 16:13:08 -04:00
|
|
|
adapter_name = "#{name_or_adapter_or_class.class.name.demodulize.remove('Adapter').underscore}"
|
|
|
|
assign_adapter(adapter_name,
|
|
|
|
name_or_adapter_or_class)
|
2016-08-06 13:55:02 -04:00
|
|
|
else
|
|
|
|
raise ArgumentError
|
|
|
|
end
|
2014-08-26 16:08:49 -04:00
|
|
|
end
|
2015-03-11 17:57:13 -04:00
|
|
|
end
|
2014-08-26 16:08:49 -04:00
|
|
|
|
2017-05-26 08:04:13 -04:00
|
|
|
def assign_adapter(adapter_name, queue_adapter)
|
|
|
|
self._queue_adapter_name = adapter_name
|
|
|
|
self._queue_adapter = queue_adapter
|
|
|
|
end
|
2016-05-16 16:13:08 -04:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
QUEUE_ADAPTER_METHODS = [:enqueue, :enqueue_at].freeze
|
2015-03-11 17:57:13 -04:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def queue_adapter?(object)
|
|
|
|
QUEUE_ADAPTER_METHODS.all? { |meth| object.respond_to?(meth) }
|
|
|
|
end
|
2014-08-26 16:08:49 -04:00
|
|
|
end
|
2014-05-19 04:55:57 -04:00
|
|
|
end
|
2014-09-26 13:10:06 -04:00
|
|
|
end
|