Rails and association caching/evaluation

December 10th, 2012

I just experienced this issue with an old Rails 3.0.10 application, where associations wouldn’t work properly. Apparently if config.cache_classes = false is true (which is default in the production environment) Rails is only evaluating associations once.

My issue was this:

Site.rb:
has_many :competitions, :conditions => ["startdate <= ? AND enddate >= ?", Time.now, Time.now]

After 24 hours the new competitions would not show up, unless if we restarted our application servers.

This got me confused for a while, but I was able to replicate it. I set config.cache_classes = true and loaded up the Rails console, and did this:

1.8.7 :001 > Site.find_by_name(‘example.dk’).competitions
SELECT “competition”.* FROM “competition” WHERE (“competition”.site = 54 AND (startdate <= ’2012-12-10 10:33:09 +0100′ AND enddate > ’2012-12-10 10:33:09 +0100′)) ORDER BY startdate DESC

And running it again 2 minutes after:

1.8.7 :002 > Site.find_by_name(‘example.dk’).competitions
SELECT “competition”.* FROM “competition” WHERE (“competition”.site = 54 AND (startdate <= ’2012-12-10 10:33:09 +0100′ AND enddate > ’2012-12-10 10:33:09 +0100′)) ORDER BY startdate DESC

Notice that the dates are exactly the same.

My fix was to use scopes for this behavior and not clauses. Some blogs suggests that it helps to use interpolation, but that is not my experience.

Other blogs about this issue:

http://blog.d27n.com/2009/10/07/rails-has_many-time-now-gotcha/
http://www.spacevatican.org/2008/11/9/do-you-know-when-your-code-runs/

Fixing danish locale errors on Ubuntu

October 21st, 2012

I got tired of seeing LC_ALL and other locale specific errors on my Ubuntu installs on Linode (and other vservers). Apparantly all you need to do is this:

1. Run this: apt-get install language-pack-da-base

2. Add this to /etc/environment:

LANGUAGE=”da_DK”
LC_ALL=”da_DK.UTF-8″
LANG=”da_DK”
LC_TYPE=”da_DK”

(Maybe you need to also run “dpkg-reconfigure locales”)

Easy deploy of Rails app with Capistrano, Varnish and Sidekiq

August 9th, 2012

Whenever I start a new Rails project, I will like to be testing it extensively on another server, as fast as possible.

So there is a couple of steps that I will go through every time I start my project. I need the following base in all projects:

  • Sidekiq as a background worker; to send emails, process data etc.
  • Capistrano for deploying the app.
  • Unicorn, to run the app after deployment.
  • Foreman, for running the setup locally.

This is my config:

[Gemfile]

[config/sidekiq.yml]

Configs that need variables to be set:

[config/unicorn/production.rb]

[config/deploy.rb]

Remember to bundle, and run ‘capify .‘ in the project to generate the Capfile.

Using PSSH for executing parallel SSH commands

June 12th, 2012

I recently needed to check the date across all of our servers, so instead of SSHing into all of them and executing a command manually, I learned about the tool pssh.

Installing pssh on Mac OS X

Installing pssh isn’t available through brew, because it is available through PyPi and it has dependencies on a homebrew built Python. Luckily it is easy to get PyPi running with easy_install.

[~]=> sudo easy_install pip ... (lots of output) [~]=> sudo pip install pssh

You now have installed pssh!

Using pssh!

First I create a file containing a list of servers:

[~]=> cat servers
backend@server1.dk
backend@server2.dk
backend@server3.dk
backend@server4.dk
backend@server5.dk
backend@server6.dk
backend@server7.dk

And for running the command on the servers:

[~]=> pssh -h servers "date"
[1] 13:33:00 [SUCCESS] backend@server1.dk
[2] 13:33:00 [SUCCESS] backend@server2.dk
[3] 13:33:00 [SUCCESS] backend@server3.dk
[4] 13:33:00 [SUCCESS] backend@server4.dk 
[5] 13:33:00 [SUCCESS] backend@server5.dk
[6] 13:33:00 [SUCCESS] backend@server6.dk
[7] 13:33:00 [SUCCESS] backend@server7.dk

But to see the output of the server you need to use the -i argument:

[~]=> pssh -h benjamin-servers -i "date"
[1] 13:34:11 [SUCCESS] backend@server1.dk
Tue Jun 12 13:34:11 CEST 2012
[2] 13:34:11 [SUCCESS] backend@server2.dk
Tue Jun 12 13:34:11 CEST 2012
[3] 13:34:11 [SUCCESS] backend@server3.dk
Tue Jun 12 13:34:11 CEST 2012
[4] 13:34:11 [SUCCESS] backend@server4.dk
Tue Jun 12 13:34:11 CEST 2012
[5] 13:34:11 [SUCCESS] backend@server5.dk
Tue Jun 12 13:34:11 CEST 2012
[6] 13:34:11 [SUCCESS] backend@server6.dk
Tue Jun 12 13:34:11 CEST 2012
[7] 13:34:11 [SUCCESS] backend@server7.dk
Tue Jun 12 13:34:11 CEST 2012

If you are unfortunate enough to run a couple of servers, without having a centralized keystore, or manage the server with puppet, you can add a public key to multiple servers with pssh as well:

[~]=> pssh -h benjamin-servers -i “echo ‘ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDGcYH4Cg+PWnYFMUMFXDOAiC+hoN/N3frBstJC5+lMQoq5gJQ4nEb0zzS+5SyLaqwbD5SqByOAY/KynRYtrYLpyJwzRUKD7VoyL5wkahkeMeJvKs7b5tmom970mDMtRgyEuePUaVj0y4basdphmEbRjLwvuQSgNwQdf3g5pVoLXntk0VY4xtE9nkSftaB5EJkoSmzOJT/PwivGwMDNq31/yP4nkYZIUjOpOe2hF3aeFJCL+3xvpkI3tYVRCzqJZHLssiwQM5EK3HiOi4wWuvmOc1LJA5FATe04hyMCUopkwdZO2dGQVpI38IVrD1yKg6okwiq2C+B4HuKsbZ8seAeG5D+i9wh this@is.a.key’ >> /usr/home/backend/.ssh/authorized_keys”

Great week, now powering up for the next week!

January 22nd, 2012

I had a very fun week, and now I am powering up for the next one! The title of this blogpost was originally called ‘Low-key sunday’ as a tribute to ‘Beskidte Toner’, with a lot of chill tunes. But then I realized that I had too much energy for listening to slow music :)

My suggestions for getting ready for the dreadful monday we all hate:

 
The Glitch Mob – Beyond Monday

Madeon – Pop Culture

Pretty Lights – Looking For Love (But Not So Sure)