Archive for April, 2008

Mongrel, RAILS_ROOT and absolute/relative paths (or Mongrel bites me in the arse again)

Monday, April 28th, 2008

I’ve had this before so I should have learnt my lesson. But I didn’t.

I had a site, inherited from another developer, that was migrated to a new server.

The other developer sent me the images associated with the site and I dutifully copied them over. I took a look – lots of “missing image” place-holders, but as this was a work in progress, in a database of thousands of items, I didn’t know what should be there and shouldn’t.

So I asked the other developer to check things over.

No – there should be much more than that” he said.

But look – they are there – in the correct folder on the server!” I replied.

You’re right, ” he said, “but why aren’t they on the site?

It turns out that the code for deciding whether to show the product image or the place-holder looked something like this (with superfluous guff removed):

if File.exists?("#{RAILS_ROOT}/public#{product_image(img, true)}") 
  "<img src="http://blog.3hv.co.uk/wp-admin/#{product.image_filename}" />"
else
  "<img src="http://blog.3hv.co.uk/images/products/image_missing.jpg" />"
end

I spent a while playing around, using script/console on the server (a truly fantastic tool). I asked it to generate the image tag for a given product – it returned exactly what I was expecting. I looked at the equivalent page in the application – it returned the image missing place-holder. But mongrel was running as the same user as script/console – there couldn’t be a permissions issue could there?

Then I remembered …

  • on my dev box, in console and/or test suites, RAILS_ROOT returns an absolute path
  • on my dev box, in mongrel, RAILS_ROOT returns an absolute path
  • on a typical Ubuntu server, in console and/or test suites, RAILS_ROOT returns an absolute path
  • on a typical Ubuntu server, in mongrel, RAILS_ROOT returns a relative path

That was the issue – the file system was getting confused as mongrel gave it a relative path for RAILS_ROOT (ignoring the fact that it should be able to resolve it, no matter how many double dots there were). And even worse, things appeared to be working fine until it was deployed to the webserver.

The fix is simple – add the following to your application controller:

helper_method :rails_root
def rails_root
  File.expand_path RAILS_ROOT
end 

and then replace all instances of RAILS_ROOT within your controllers, views and helpers with rails_root. Problem solved.

Going on an Information Diet

Wednesday, April 16th, 2008

I recently went on holiday. To Spain. It was good.

Apart from it took me about four days to switch out of work mode. Constantly worrying about checking my mail. Has Basecamp been updated? What’s happening on Twitter? Given that I was only away for seven days, four days in work mode was not the best.

I made up my mind to try and keep things under control from now on. After a day of work, switch off. No reading blog entries, no checking emails, no tweets. Do something else, talk to a person who is physically present, watch the TV (OK, maybe that’s pushing it), play the guitar, get mauled by the cat. Just stop thinking about work!

Then, on my return, I was faced with the prospect of opening my FeedReader.

I remembered how my heart would sink when, opening it on a morning would reveal a red badge with a three digit number in it. “500 unread articles? I’ve only been asleep for six hours. It will take me that long to plough through those articles!” Imagine the unread count after being away for a week

I didn’t want to go back there.

So I made the choice not to open it. Ever.

Two weeks in and I don’t miss it.

I’ve got podcasts for the car through iTunes (yeah yeah, a glorified feed reader, but with six or seven “unread” items, not thousands).

I’ve got Northpack.

I’ve got the Geekup mailing list.

And I’ve got Twitter.

Thank buggery for Twitter.

People are always posting links in their tweets. I don’t have to read them. I use Twitterific, so it pops up on-screen for a couple of seconds. If I miss it, I miss it. If I see it I can choose to look at the link or ignore it. Decision made in less than five seconds.

No stress about the stuff I’ve not yet looked at. No stress about the stuff I’ve missed. In fact, no stress at all.

That’s how I’ve been dealing with the overload. How do you manage it?