Sunday, April 20, 2008

mod_rails (Passenger) and pre-loading of Ruby classes

I spent a good (well, I didn't think it was good) hour today fighting a deployment to my Slicehost VPS today using mod_rails. I used mod_rails because it's so easy to drop a rails app up an create a vhost in Apache. Well there's a little-known fact about mod_rails that I guarantee will bite you eventually. mod_rails preloads all .rb files in your deployment directory. Even the ones named __unused_thing.rb or even worse, environment.rb inside /apps/controllers. Someone had taken a copy of the /config/environment.rb file and put it in /app/controllers. mod_rails gleefully loaded up that rogue environment.rb file and threw up with an ugly error about not being able to find /app/controllers/boot.rb

It took me an hour to actually get the real error out of the error message. To be fair, the error message listed the file /app/controllers/environment.rb but in my wisdom, I thought that it was mod_rails guessing my app's root wrong somehow. Or I thought I didn't set the docroot in Apache right. If I had read the error a little better, I would have solved this one faster. Since I didn't write the app that I was deploying, I was pretty sure nobody would ever put an environment.rb file in the controllers folder. Hmpf. But who would have guessed that mod_rails loads all Ruby files? That's certainly not the way that old dog Mongrel did it.

Thank god for testing. You DO have good tests don't you?

2 comments:

xmlblog said...

Why do you have unused code in your app? That's a problem waiting to happen, mod_rails or not. We have a strict policy against __remind_me_to_delete_this_later.rb or # commenting out this code for now. Our rule is simple. Delete it and use a good version control system.

Brian said...

This wasn't my app. It was emailed to me by a customer who needed Rails hosting for a small application. I agree, all bad practices.