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

70 lines
1.9 KiB
Ruby
Raw Normal View History

2017-07-09 13:49:52 -04:00
# frozen_string_literal: true
2014-05-19 05:04:23 -04:00
module ActiveJob
module QueueName
extend ActiveSupport::Concern
2014-05-19 05:04:23 -04:00
# Includes the ability to override the default queue name and prefix.
module ClassMethods
mattr_accessor :default_queue_name, default: "default"
# 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
#
# 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
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:
queue_name = part_name || default_queue_name
name_parts = [queue_name_prefix.presence, queue_name]
-name_parts.compact.join(queue_name_delimiter)
end
end
included do
class_attribute :queue_name, instance_accessor: false, default: -> { self.class.default_queue_name }
class_attribute :queue_name_delimiter, instance_accessor: false, default: "_"
class_attribute :queue_name_prefix
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
end