提交 11ab04b1 编写于 作者: T Terry Meacham

Added queue_name_delimiter attribute.

- Added ActiveJob::Base#queue_name_delimiter to allow for
  developers using ActiveJob to change the delimiter from the default
  ('_') to whatever else they may be using (e.g., '.', '-', ...).

- Updated source guide to include a blurb about the delimiter.
上级 85faea4b
...@@ -26,13 +26,16 @@ def queue_as(part_name=nil, &block) ...@@ -26,13 +26,16 @@ def queue_as(part_name=nil, &block)
def queue_name_from_part(part_name) #:nodoc: def queue_name_from_part(part_name) #:nodoc:
queue_name = part_name || default_queue_name queue_name = part_name || default_queue_name
name_parts = [queue_name_prefix.presence, queue_name] name_parts = [queue_name_prefix.presence, queue_name]
name_parts.compact.join('_') name_parts.compact.join(queue_name_delimiter)
end end
end end
included do included do
class_attribute :queue_name, instance_accessor: false class_attribute :queue_name, instance_accessor: false
class_attribute :queue_name_delimiter, instance_accessor: false
self.queue_name = default_queue_name self.queue_name = default_queue_name
self.queue_name_delimiter = '_' # set default delimiter to '_'
end end
# Returns the name of the queue the job will be run on # Returns the name of the queue the job will be run on
......
...@@ -64,7 +64,7 @@ class QueueNamingTest < ActiveSupport::TestCase ...@@ -64,7 +64,7 @@ class QueueNamingTest < ActiveSupport::TestCase
end end
end end
test 'queu_name_prefix prepended to the queue name' do test 'queue_name_prefix prepended to the queue name with default delimiter' do
original_queue_name_prefix = ActiveJob::Base.queue_name_prefix original_queue_name_prefix = ActiveJob::Base.queue_name_prefix
original_queue_name = HelloJob.queue_name original_queue_name = HelloJob.queue_name
...@@ -78,6 +78,23 @@ class QueueNamingTest < ActiveSupport::TestCase ...@@ -78,6 +78,23 @@ class QueueNamingTest < ActiveSupport::TestCase
end end
end end
test 'queue_name_prefix prepended to the queue name with custom delimiter' do
original_queue_name_prefix = ActiveJob::Base.queue_name_prefix
original_queue_name_delimiter = ActiveJob::Base.queue_name_delimiter
original_queue_name = HelloJob.queue_name
begin
ActiveJob::Base.queue_name_delimiter = '.'
ActiveJob::Base.queue_name_prefix = 'aj'
HelloJob.queue_as :low
assert_equal 'aj.low', HelloJob.queue_name
ensure
ActiveJob::Base.queue_name_prefix = original_queue_name_prefix
ActiveJob::Base.queue_name_delimiter = original_queue_name_delimiter
HelloJob.queue_name = original_queue_name
end
end
test 'uses queue passed to #set' do test 'uses queue passed to #set' do
job = HelloJob.set(queue: :some_queue).perform_later job = HelloJob.set(queue: :some_queue).perform_later
assert_equal "some_queue", job.queue_name assert_equal "some_queue", job.queue_name
......
...@@ -149,6 +149,29 @@ end ...@@ -149,6 +149,29 @@ end
# environment # environment
``` ```
The default queue name prefix delimiter is '_'. This can be changed by setting
`config.active_job.queue_name_delimiter` in `application.rb`:
```ruby
# config/application.rb
module YourApp
class Application < Rails::Application
config.active_job.queue_name_prefix = Rails.env
config.active_job.queue_name_delimiter = '.'
end
end
# app/jobs/guests_cleanup.rb
class GuestsCleanupJob < ActiveJob::Base
queue_as :low_priority
#....
end
# Now your job will run on queue production.low_priority on your
# production environment and on staging.low_priority on your staging
# environment
```
If you want more control on what queue a job will be run you can pass a :queue If you want more control on what queue a job will be run you can pass a :queue
option to #set: option to #set:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册