aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib
diff options
context:
space:
mode:
authorYehuda Katz <wycats@gmail.com>2012-04-26 21:43:12 -0700
committerYehuda Katz <wycats@gmail.com>2012-04-26 21:43:12 -0700
commit602000be90e9935f4f4ee5acc096725d7b7c33e5 (patch)
tree5c0c7938927be67b56f5c50a08e482a84992ea95 /railties/lib
parentadff4a706a5d7ad18ef05303461e1a0d848bd662 (diff)
downloadrails-602000be90e9935f4f4ee5acc096725d7b7c33e5.tar.gz
rails-602000be90e9935f4f4ee5acc096725d7b7c33e5.tar.bz2
rails-602000be90e9935f4f4ee5acc096725d7b7c33e5.zip
Missing git add :trollface:
Diffstat (limited to 'railties/lib')
-rw-r--r--railties/lib/rails/queueing.rb65
1 files changed, 65 insertions, 0 deletions
diff --git a/railties/lib/rails/queueing.rb b/railties/lib/rails/queueing.rb
new file mode 100644
index 0000000000..b77940f821
--- /dev/null
+++ b/railties/lib/rails/queueing.rb
@@ -0,0 +1,65 @@
+module Rails
+ module Queueing
+ # In test mode, the Rails queue is backed by an Array so that assertions
+ # can be made about its contents. The test queue provides a +contents+
+ # method to make assertions about the queue's contents and a +drain+
+ # method to drain the queue and run the jobs.
+ #
+ # Jobs are run in a separate thread to catch mistakes where code
+ # assumes that the job is run in the same thread.
+ class TestQueue
+ attr_reader :contents
+
+ def initialize
+ @contents = []
+ end
+
+ def drain
+ # run the jobs in a separate thread so assumptions of synchronous
+ # jobs are caught in test mode.
+ t = Thread.new do
+ while job = @contents.pop
+ job.run
+ end
+ end
+ t.join
+ end
+
+ # implement the Queue API
+ def push(object)
+ @contents << object
+ end
+ end
+
+ # The threaded consumer will run jobs in a background thread in
+ # development mode or in a VM where running jobs on a thread in
+ # production mode makes sense.
+ #
+ # When the process exits, the consumer pushes a nil onto the
+ # queue and joins the thread, which will ensure that all jobs
+ # are executed before the process finally dies.
+ class ThreadedConsumer
+ def self.start(queue)
+ new(queue).start
+ end
+
+ def initialize(queue)
+ @queue = queue
+ end
+
+ def start
+ @thread = Thread.new do
+ while job = @queue.pop
+ job.run
+ end
+ end
+ self
+ end
+
+ def shutdown
+ @queue.push nil
+ @thread.join
+ end
+ end
+ end
+end