2014-05-19 05:04:23 -04:00
|
|
|
module ActiveJob
|
|
|
|
module QueueName
|
2014-06-09 12:49:14 -04:00
|
|
|
extend ActiveSupport::Concern
|
2014-05-19 05:04:23 -04:00
|
|
|
|
2014-11-03 22:26:04 -05:00
|
|
|
# Includes the ability to override the default queue name and prefix.
|
2014-06-09 12:49:14 -04:00
|
|
|
module ClassMethods
|
2014-08-18 04:17:36 -04:00
|
|
|
mattr_accessor(:queue_name_prefix)
|
2014-08-15 16:32:08 -04:00
|
|
|
mattr_accessor(:default_queue_name) { "default" }
|
2014-06-09 12:49:14 -04:00
|
|
|
|
2014-09-26 13:10:06 -04:00
|
|
|
# Specifies the name of the queue to process the job on.
|
|
|
|
#
|
|
|
|
# class PublishToFeedJob < ActiveJob::Base
|
|
|
|
# queue_as :feeds
|
|
|
|
#
|
|
|
|
# def perform(post)
|
|
|
|
# post.to_feed!
|
|
|
|
# end
|
|
|
|
# end
|
2014-08-25 10:34:50 -04:00
|
|
|
def queue_as(part_name=nil, &block)
|
|
|
|
if block_given?
|
|
|
|
self.queue_name = block
|
|
|
|
else
|
|
|
|
self.queue_name = queue_name_from_part(part_name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def queue_name_from_part(part_name) #:nodoc:
|
2014-10-09 13:33:55 -04:00
|
|
|
queue_name = part_name || default_queue_name
|
2014-08-18 04:17:36 -04:00
|
|
|
name_parts = [queue_name_prefix.presence, queue_name]
|
2014-09-23 16:51:44 -04:00
|
|
|
name_parts.compact.join(queue_name_delimiter)
|
2014-06-09 12:49:14 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
included do
|
2014-08-25 10:34:50 -04:00
|
|
|
class_attribute :queue_name, instance_accessor: false
|
2014-09-23 16:51:44 -04:00
|
|
|
class_attribute :queue_name_delimiter, instance_accessor: false
|
|
|
|
|
2014-08-15 16:32:08 -04:00
|
|
|
self.queue_name = default_queue_name
|
2014-09-23 16:51:44 -04:00
|
|
|
self.queue_name_delimiter = '_' # set default delimiter to '_'
|
2014-05-19 05:04:23 -04:00
|
|
|
end
|
2014-08-25 10:34:50 -04:00
|
|
|
|
2015-06-10 00:29:19 -04:00
|
|
|
# Returns the name of the queue the job will be run on.
|
2014-08-25 10:34:50 -04:00
|
|
|
def queue_name
|
|
|
|
if @queue_name.is_a?(Proc)
|
|
|
|
@queue_name = self.class.queue_name_from_part(instance_exec(&@queue_name))
|
|
|
|
end
|
|
|
|
@queue_name
|
|
|
|
end
|
|
|
|
|
2014-05-19 05:04:23 -04:00
|
|
|
end
|
2014-08-15 16:32:08 -04:00
|
|
|
end
|