For the attention of last week

archive of tokumine.com

Month: August, 2009

On Rails, Postgres, Passenger & Capistrano…

We’re sorry, but something went wrong.

You’re using the above combination, and it’s your first time deploying this app via capistrano. You get this:

wrong wrong wrong

A one way ticket to baldness

Not so bad you think. Then you go to the logs where you find precisely nothing. No logs. Zip.

The production.log files are empty, as are the apache logs. You’ve got nothing to go on, and it still works fine locally…

Regain

Like your future self, you could spend the next 4 hrs of your life working out what the heck is going on with no log feedback, or you could simply make sure that you have your environments.rb file ownership set to be the same as your Postgres user. Make sure you’ve got this somewhere in your deploy.rb. I put it in the restart task I wrote:

sudo "chown postgres:postgres  #{current_path}/config/environment.rb"

p157_02

Advertisements

Fine grain caching in rails using Memcache

speed up with memcache

Speed is pretty important, especially with searching. Fortunately it’s a cinch to use ultra fine grained caching in Rails (objects, search results, json data etc), as long as you’ve have installed the Memcache server, set it up, and started it. In your environments.rb:

Shake

require "memcache"
CACHE = MemCache.new('127.0.0.1')

In your application_controlller.rb:

private
def cache(key)
    begin
      unless output = CACHE.get(key)
        output = yield
        CACHE.set(key, output)
      end
      return output
    rescue MemCache::MemCacheError
      yield
    end
  end

Bake

With these simple methods, you’ll be able to cache anything you want simply by using:

output = cache(your_key) { YourClass.do_a_massive_query() }

If it’s not cached, the query will be run and stored in the cache for next time. If it is, you get your results back ultra fast. Another cool thing is that if you don’t have memcache installed (eg on your dev box), it will just default to doing the code in your block.