One thing that’s pretty obvious from the server logs for this blog is that using AMQP with Phusion Passenger should be easier. When the topic came up yet again on the amqp mailing list, I decided there needed to be an easier way. So I wrote a small library that takes care of getting AMQP running on any ruby app server, named it Qusion, and put the code on github.
Qusion!
Qusion is pretty small and simple, with only two real features. First, it uses a monkey patch to add an AMQP.start_web_dispatcher method. This method takes the same options as AMQP.start, and it takes care of setting up whatever threads and event callbacks are necessary to run EventMachine and AMQP on whatever webserver it’s running on. To use it in rails, just add something like this to your config/environment.rb:
require "#{RAILS_ROOT}/vendor/gems/qusion/lib/qusion" # or whatever
AMQP.start_web_dispatcher(:host => "localhost")
Taking Care of Your Rabbit
The other feature that Qusion provides is a pool of AMQP channels. AMQP channels are created every time you call MQ.new and they’re handled by individual Erlang processes inside RabbitMQ. The danger here is that if you create a new one for every request you serve, you can eventually cause big problems for RabbitMQ. To solve this problem, Qusion creates a pool of AMQP channels when your app starts. To use them you just need to call Qusion.channel instead of MQ.new. To illustrate:
# instead of this:
MQ.new.queue("my-work-queue")
# Do this:
Qusion.channel.queue("my-work-queue")
Harder Better Faster Stronger—Maybe
Qusion isn’t intended to do very much: it lives only to make it easy to use AMQP for deferring the time intensive tasks that can make your app feel slow. With that said, there might be some other pain points that would be a good fit for Qusion to address. If you come across any, feel free to git fork and hack or contact me if you’ve got a bug you can’t pin down.
Photo from the Fusion Festival 2009 by GuyInkognito CC BY-NC-SA
