2017-07-09 13:49:52 -04:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 09:40:03 -04:00
|
|
|
|
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
|
2017-05-31 05:16:20 -04:00
|
|
|
mattr_accessor :queue_name_prefix
|
|
|
|
mattr_accessor :default_queue_name, default: "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
|
2019-02-11 17:36:44 -05:00
|
|
|
#
|
|
|
|
# Can be given a block that will evaluate in the context of the job
|
|
|
|
# allowing +self.arguments+ to be accessed so that a dynamic queue name
|
|
|
|
# can be applied:
|
|
|
|
#
|
|
|
|
# class PublishToFeedJob < ApplicationJob
|
|
|
|
# queue_as do
|
|
|
|
# post = self.arguments.first
|
|
|
|
#
|
|
|
|
# if post.paid?
|
|
|
|
# :paid_feeds
|
|
|
|
# else
|
|
|
|
# :feeds
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# def perform(post)
|
|
|
|
# post.to_feed!
|
|
|
|
# end
|
|
|
|
# end
|
2016-10-28 23:05:58 -04:00
|
|
|
def queue_as(part_name = nil, &block)
|
2014-08-25 10:34:50 -04:00
|
|
|
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
|
2018-11-03 22:39:18 -04:00
|
|
|
class_attribute :queue_name, instance_accessor: false, default: -> { self.class.default_queue_name }
|
2017-05-29 12:01:50 -04:00
|
|
|
class_attribute :queue_name_delimiter, instance_accessor: false, default: "_"
|
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
|