Passenger 4.0.1 Is Out!



Written By : Kevin Lawver


May 06, 2013

Passenger 4.0.1 is out and that means we need to add support to Moonshine for it! If you update Moonshine, you’ll have everything you need to deploy Passenger 4.0.1 - I highly recommend it. Why? Out of band work (garbage collection)! The Phusion guys blogged about it during one of the betas, but their instructions for setting it up don’t work very well with the final release. I think this is my favorite new feature in Passenger and could end up being extremely useful for folks running larger apps that struggle with garbage collection. I got it working this morning on one of our internal apps and decided I would save you the trouble of figuring it out yourself.

First, you need to add passenger to your Gemfile. I don’t normally put it in mine since it lives outside of the Rails stack, but you’ll need it for this:

gem 'passenger', '4.0.1'

Now we need to add some cool stuff to your config/application.rb. Near the top, after you require rails, add this:

if ['staging','production'].include?(Rails.env)
  require 'phusion_passenger/rack/out_of_band_gc'
  require 'phusion_passenger/public_api'
end

And inside the module where you have all your other configuration, this:

if ['staging','production'].include?(Rails.env)
  config.middleware.use PhusionPassenger::Rack::OutOfBandGc, 5

  PhusionPassenger.on_event(:oob_work) do
    # Phusion Passenger has told us that we're ready to perform OOB work.
    t0 = Time.now
    GC.start
    Rails.logger.info "Out-Of-Bound GC finished in #{Time.now - t0} sec"
  end

end

Now, after you deploy that (to staging first, I hope), you should be able to tail the log and see the app running GC every 5 requests. If your app is super busy, you may want to change the setting to more requests, but 5 is probably a good place to start.

I can foresee a lot of other uses for out of band work, almost anything that the user shouldn’t have to wait for but that needs to done while you have access to the request (assuming that’s something out of band work can do - I haven’t checked yet).

Update: I originally had a before_filter defined here to add the “X-Passenger-Request-OOB-Work” header. It turns out that ends up running garbage collection after every request. The Rack Middleware does the right thing and only adds the header after every fifth request (or whatever you configure it to do). It’s probably not horrible to run GC every request, but it defeats the purpose of using the middleware.