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
|
|
|
|
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
|
|
|
|
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)
|
2014-09-23 23:43:12 -04:00
|
|
|
self._queue_adapter = interpret_adapter(name_or_adapter_or_class)
|
|
|
|
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
|
|
|
|
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)
|
|
|
|
name_or_adapter_or_class
|
|
|
|
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
|
|
|
|
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
|