2012-04-28 03:26:23 -04:00
|
|
|
require "thread"
|
2012-07-19 15:09:39 -04:00
|
|
|
require 'delegate'
|
2012-04-28 03:26:23 -04:00
|
|
|
|
2012-04-27 00:43:12 -04:00
|
|
|
module Rails
|
|
|
|
module Queueing
|
2012-07-21 12:55:22 -04:00
|
|
|
# A container for multiple queues. This class delegates to a default Queue
|
|
|
|
# so that <tt>Rails.queue.push</tt> and friends will Just Work. To use this class
|
2012-07-19 15:09:39 -04:00
|
|
|
# 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
|
|
|
|
|
2012-04-28 03:26:23 -04:00
|
|
|
# A Queue that simply inherits from STDLIB's Queue. Everytime this
|
|
|
|
# queue is used, Rails automatically sets up a ThreadedConsumer
|
|
|
|
# to consume it.
|
|
|
|
class Queue < ::Queue
|
|
|
|
end
|
|
|
|
|
2012-04-27 00:43:12 -04:00
|
|
|
# In test mode, the Rails queue is backed by an Array so that assertions
|
2012-04-27 18:51:40 -04:00
|
|
|
# can be made about its contents. The test queue provides a +jobs+
|
2012-04-27 00:43:12 -04:00
|
|
|
# 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.
|
2012-04-27 17:26:26 -04:00
|
|
|
class TestQueue < ::Queue
|
2012-05-03 21:26:30 -04:00
|
|
|
# Get a list of the jobs off this queue. This method may not be
|
2012-04-27 17:58:04 -04:00
|
|
|
# available on production queues.
|
|
|
|
def jobs
|
|
|
|
@que.dup
|
|
|
|
end
|
|
|
|
|
2012-07-03 06:55:46 -04:00
|
|
|
# Marshal and unmarshal job before pushing it onto the queue. This will
|
|
|
|
# raise an exception on any attempts in tests to push jobs that can't (or
|
|
|
|
# shouldn't) be marshalled.
|
|
|
|
def push(job)
|
|
|
|
super Marshal.load(Marshal.dump(job))
|
|
|
|
end
|
|
|
|
|
2012-05-03 21:26:30 -04:00
|
|
|
# Drain the queue, running all jobs in a different thread. This method
|
2012-04-27 17:58:04 -04:00
|
|
|
# may not be available on production queues.
|
2012-04-27 00:43:12 -04:00
|
|
|
def drain
|
|
|
|
# run the jobs in a separate thread so assumptions of synchronous
|
|
|
|
# jobs are caught in test mode.
|
2012-04-27 17:26:26 -04:00
|
|
|
Thread.new { pop.run until empty? }.join
|
2012-04-27 00:43:12 -04:00
|
|
|
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
|
2012-04-27 18:00:58 -04:00
|
|
|
begin
|
|
|
|
job.run
|
|
|
|
rescue Exception => e
|
2012-05-02 23:15:50 -04:00
|
|
|
handle_exception e
|
2012-04-27 18:00:58 -04:00
|
|
|
end
|
2012-04-27 00:43:12 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def shutdown
|
|
|
|
@queue.push nil
|
|
|
|
@thread.join
|
|
|
|
end
|
2012-05-02 23:15:50 -04:00
|
|
|
|
|
|
|
def handle_exception(e)
|
|
|
|
Rails.logger.error "Job Error: #{e.message}\n#{e.backtrace.join("\n")}"
|
|
|
|
end
|
2012-04-27 00:43:12 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|