diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2012-07-19 12:09:39 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2012-07-19 12:09:39 -0700 |
commit | 3b3ca133071419a42c0b1f55c96fc604ff73f2ac (patch) | |
tree | ed88fbf05020aa8ee637a9dff0bcb6cddc2fb223 /railties | |
parent | fa3c84fabee01847c3468cd1e8e32003693bc629 (diff) | |
download | rails-3b3ca133071419a42c0b1f55c96fc604ff73f2ac.tar.gz rails-3b3ca133071419a42c0b1f55c96fc604ff73f2ac.tar.bz2 rails-3b3ca133071419a42c0b1f55c96fc604ff73f2ac.zip |
adding Rails::Queueing::Container
This allows us to do:
In your configuration:
Rails.queue[:image_queue] = SomeQueue.new
Rails.queue[:mail_queue] = SomeQueue.new
In your app code:
Rails.queue[:mail_queue].push MailJob.new
Both jobs pushed to the same default queue
Rails.queue.push DefaultJob.new
Rails.queue[:default].push DefaultJob.new
Diffstat (limited to 'railties')
-rw-r--r-- | railties/lib/rails/application.rb | 2 | ||||
-rw-r--r-- | railties/lib/rails/queueing.rb | 27 | ||||
-rw-r--r-- | railties/test/application/queue_test.rb | 10 | ||||
-rw-r--r-- | railties/test/queueing/container_test.rb | 30 |
4 files changed, 63 insertions, 6 deletions
diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index 3afbf0a03e..da5b33c0fb 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -177,7 +177,7 @@ module Rails end def queue #:nodoc: - @queue ||= build_queue + @queue ||= Queueing::Container.new(build_queue) end def build_queue #:nodoc: diff --git a/railties/lib/rails/queueing.rb b/railties/lib/rails/queueing.rb index 8a76914548..892cf8e894 100644 --- a/railties/lib/rails/queueing.rb +++ b/railties/lib/rails/queueing.rb @@ -1,7 +1,34 @@ require "thread" +require 'delegate' module Rails module Queueing + # A container for multiple queues. This class delegates to a default Queue + # so that Rails.queue.push and friends will Just Work. To use this class + # with multiple queues: + # + # # In your configuration: + # Rails.queue[:image_queue] = SomeQueue.new + # Rails.queue[:mail_queue] = SomeQueue.new + # + # # In your app code: + # Rails.queue[:mail_queue].push SomeJob.new + # + class Container < DelegateClass(::Queue) + def initialize(default_queue) + @queues = { :default => default_queue } + super(default_queue) + end + + def [](queue_name) + @queues[queue_name] + end + + def []=(queue_name, queue) + @queues[queue_name] = queue + end + end + # A Queue that simply inherits from STDLIB's Queue. Everytime this # queue is used, Rails automatically sets up a ThreadedConsumer # to consume it. diff --git a/railties/test/application/queue_test.rb b/railties/test/application/queue_test.rb index 22d6c20404..83ca981336 100644 --- a/railties/test/application/queue_test.rb +++ b/railties/test/application/queue_test.rb @@ -20,14 +20,14 @@ module ApplicationTests test "the queue is a TestQueue in test mode" do app("test") - assert_kind_of Rails::Queueing::TestQueue, Rails.application.queue - assert_kind_of Rails::Queueing::TestQueue, Rails.queue + assert_kind_of Rails::Queueing::TestQueue, Rails.application.queue[:default] + assert_kind_of Rails::Queueing::TestQueue, Rails.queue[:default] end test "the queue is a Queue in development mode" do app("development") - assert_kind_of Rails::Queueing::Queue, Rails.application.queue - assert_kind_of Rails::Queueing::Queue, Rails.queue + assert_kind_of Rails::Queueing::Queue, Rails.application.queue[:default] + assert_kind_of Rails::Queueing::Queue, Rails.queue[:default] end class ThreadTrackingJob @@ -144,7 +144,7 @@ module ApplicationTests test "a custom queue implementation can be provided" do setup_custom_queue - assert_kind_of MyQueue, Rails.queue + assert_kind_of MyQueue, Rails.queue[:default] job = Struct.new(:id, :ran) do def run diff --git a/railties/test/queueing/container_test.rb b/railties/test/queueing/container_test.rb new file mode 100644 index 0000000000..69e59a3871 --- /dev/null +++ b/railties/test/queueing/container_test.rb @@ -0,0 +1,30 @@ +require 'abstract_unit' +require 'rails/queueing' + +module Rails + module Queueing + class ContainerTest < ActiveSupport::TestCase + def test_delegates_to_default + q = Queue.new + container = Container.new q + job = Object.new + + container.push job + assert_equal job, q.pop + end + + def test_access_default + q = Queue.new + container = Container.new q + assert_equal q, container[:default] + end + + def test_assign_queue + container = Container.new Object.new + q = Object.new + container[:foo] = q + assert_equal q, container[:foo] + end + end + end +end |