Coding Tidbits

Cube Root is an exclusive Ruby on Rails Consultancy. Find our programming ramblings here.

Using Que gem with Phusion Passenger

Que is a background jobs gem for Ruby and PostgreSQL, an excellent alternative to DelayedJob if you are using Postgres.

Our Tweetd project is deployed on a linode slice with nginx and Phusion Passenger.

We can run Que workers (threads) within the web process. Que documentation has usage information about using unicorn and puma servers, but not passenger.

After a bit of googling, came across passenger docs which says passenger provides PhusionPassenger.on_event(:starting_worker_process) hook to specify our own custom code, when a new process is forked.

Finally, stuffed application.rb with following and it worked flawlessly!

if defined?(PhusionPassenger)
  PhusionPassenger.on_event(:starting_worker_process) do |forked|
    if forked
      # We're in smart spawning mode.
      ActiveRecord::Base.establish_connection
      Que.mode = :async
    end
  end
end