Interactive Memcached Debugging with Rails

Posted on June 20, 2009

The memcached support in Rails is great, but it’s a bit difficult to see what the heck is going on in a production environment. The first problem is, by default Rails doesn’t keep a list of all memcached keys that are available on the system. So, you have to use a plugin like memcache_store_with_delete_matched to have memcache itself store a list of all available keys.

You can install this plugin with the typical script/plugin command.

script/plugin install git://github.com/lacomartincik/memcache-store-with-delete_matched.git

Then, you configure memcached in your environment.rb like this.

mem_cache_options = {
    :c_threshold => 10000,
    :compression => true,
    :debug => false,
    :timeout => false,
    :namespace => 'app',
    :readonly => false,
    :urlencode => false
  }
  config.action_controller.cache_store = :mem_cache_store_with_delete_matched, ['127.0.0.1:11211'], mem_cache_options

The other benefit to using memcache_store_with_delete_matched is that regular expressions within expire_fragment works.

expire_fragment(%r!/index_user_.*!)

So, down to debugging. If you want to see all keys in a script/console, you can do the following.

YAML.load(ActionController::Base.cache_store.fetch("memcached_store_key_list"))

You can select certain keys from the memcached_store_keylist.

YAML.load(ActionController::Base.cache_store.fetch("memcached_store_key_list")) .detect{ |k| k.match(/index/) }

Once you have the list of keys, you can plug them into a fetch command.

ActionController::Base.cache_store.fetch("app/blog/index")

You can also manually expire keys from a console.

c = ApplicationController.new
c.expire_fragment(/index/)

Using Phusion Passenger with HAProxy

Posted on February 25, 2009

Today I decided to turn on HAProxy to load-balance between Apache instances running Phusion Passenger on two separate machines. The results are clear. Doing this nearly doubled our ApacheBench throughput scores.

This config was adaped from the 37Signals blog post here.

# /etc/haproxy/haproxy.cfg
global
        maxconn 4096

defaults
        mode    http
        retries 3
        option redispatch
        maxconn 2000
        contimeout	5000
        clitimeout	50000
        srvtimeout	50000

listen web1 XXX.XXX.XXX.210:80
       mode http
       balance roundrobin
       server web1 XXX.XXX.XXX.210:8080
       server web2 XXX.XXX.XXX.211:8080

Passenger is configured the normal way.

<VirtualHost *:8080>
        ServerName www.example.com
        DocumentRoot /var/www/rails_project/current/public
</VirtualHost>

Chaining Named Scopes

Posted on November 10, 2008

There aren’t many examples of how to combine named_scopes programmatically on the ol’ Blogosphere these days. All I could find were a few scant references to using anonymous scopes like this. It took me quite a while to figure this out which is surprising since this use case seems like such a common thing to do.

If you want to stack conditions on a search form with will_paginate on top of that, this seems to be the spice.

class Shape < ActiveRecord::Base
    named_scope :created_after, lambda { |m| {:conditions => ["created_at > ?", m]} }
    named_scope :with_color, lambda { |color| {:conditions => {:color => color}} }
end

class ShapesController < ApplicationController
  def index
    scope =  Shape.scoped({})
    scope = scope.created_after(params[:months_ago].to_i.months.ago) if params[:months_ago]
    scope = scope.with_color(params[:color]) if params[:color]
    @shapes = scope.paginate(paginate_options({:order => 'id DESC', :page => 1}))
  end
end

I started to use eval to do this, but Ken Turner set me straight today. I view eval as more of a last resort when all else fails. In this case, it’s possible to tap these filters in using a rubber mallet. No sense in busting out the sledgehammer for this one.

Monkeypatching For Robots

Posted on July 16, 2008

Even though Jeff Atwood believes that monkeypatching will lead to the apocalypse, I’ve discovered three cases where it has proven to be most useful.

Case 1: Dynamically Patching a Plug-in

Let’s face it. The Ruby on Rails wiki is littered with a cadre of unmaintained plugins. One example is file_column. Attachment_fu pretty much replaces this, but for whatever reason, the project that I’m working on is using the old warhorse of file upload packages. Recently, I’ve noticed several problems with file_column. The biggest issue is that it was written pre-Leopard So, file_column doesn’t support the new way of calling OS X’s file command. Combining monkeypatching with the Rails alias_method_chain allows us to patch the underlying code with minimal effort. And, keeping the dynamic patch separate, means that updating the plugin will not overwrite the patch.

# Rails lib/project_system.rb
module FileColumn
  def get_content_type_with_leopard_rules(fallback=nil)
    if not RUBY_PLATFORM.eql?('universal-darwin9.0')
      return get_content_type_for_leopard_without_apple_rules
    end
    if options[:file_exec]
      begin
        content_type = `#{@options[:file_exec]} -bI "#{@local_file_path}"`.chomp
        content_type = fallback unless $?.success?
        content_type.gsub!(/;.+$/,"") if content_type
        content_type
      rescue
        fallback
      end
    else
      fallback
    end
  end
  alias_method_chain :get_content_type, :leopard_rules
end



Case 2: Adding a Feature to a Plug-in

I’m a total Unix snob. Upper-case filenames injure what’s left of my dwindling sanity. File_column recklessly allows the user’s filenames to be saved directly to the filesystem. This has the side-effect of dumping a flagon full of mime-type detection FAIL when you view the files that have been uploaded to the server. Think of users uploading images named me.jpeg, me.JPG and me.Jpeg. Inconsistent capitalization is clearly evil and has to be sanitized before it gets to our precious disks. Using a monkeypatch with alias_method_chain, you can override file_column’s sanitize_filename method to convert the filename to lowercase.

# Rails lib/project_system.rb
module FileColumn
  class << self
    def sanitize_filename_with_downcase_rules(filename)
      self.sanitize_filename_without_downcase_rules(filename).downcase.gsub(/\.jpeg$/,'jpg')
    end
    alias_method_chain :sanitize_filename, :downcase_rules
  end
end



Case 3: Overriding a Native Ruby Class to Facilitate Symbol#to_proc Abuses

Ever since reading Reg Braithwaite's (1..100).inject(&:+) post, I've been mystified and rather infatuated with Rails' Symbol#to_proc method (which, incidentally, was only possible through Rails' monkeypatch to the Ruby Symbol class).

Recently, I wrote a method to detect whether any one checkbox or radio button had been selected on a HTML form. This would serve as a form validation, but was complicated due to the fact that radio button values are arbitrary strings and checkbox values are posted as "0" and "1". The radio button values had to be converted to boolean by the controller, but ActiveRecord's value_to_boolean method does not support this. Monkeypatching came to the rescue yet again and allowed me to override Object in an inject-friendly way.

# Rails lib/project_system.rb
class Object
  def selected?
    return true if self and self.is_a?(String) and self.include?('location')
    ActiveRecord::ConnectionAdapters::Column.value_to_boolean(self)
  end
end

Rather than writing a massive case statement or nested if/elsifs, we can just write the following in a model.

# Rails app/models/model_name.rb
def selected_place?
  places.collect(&:selected?).inject(&:|)
  # Logically, the same as places.collect(&:selected?).any?
end

The preceding loop calls our new Object selected? method on each of the location_types column, and the inject ORs all the data together.

I don't really understand why Jeff is writing these slime pieces on monkeypatching. Jeff's logic is equivalent to reasoning that driving should be outlawed because cars are dangerous. However, just because a few people drive drunk doesn't mean that no one should drive! Oddly, I would get behind a law to make drunken monkeypatching a felony.

Malfeasance in 64-bit PowerPC MySQL Gem Compilation

Posted on May 12, 2008

For the three people in the world building the Rails mysql gem on a PowerPC G5-based OS X Server with the 64-bit MySQL installed getting this crazy error:

lazy symbol binding failed: Symbol not found: _mysql_init

The magic ninja gem install command that will cure all your ills goes a little something like this:

sudo env ARCHFLAGS="-arch ppc64" gem install -V mysql -- --with-mysql-include=/usr/local/mysql/include/ --with-mysql-lib=/usr/local/mysql/lib --with-mysql-config=/usr/local/mysql/bin/mysql_config --with-mysql-dir=/usr/local/mysql

Pretty obvious when you think about it. Not sure why it took me a little over an hour to discover the crucial lynchpin for correcting this system-wide thought-tastrophy.

CSS Templating with Sass

Posted on July 09, 2007

I don’t hear many people talking about Sass, but it is indeed fantastic. Basically, Sass is kind of like the Smarty of CSS Templating. Sass files compile to CSS after every change just like Smarty’s TPL files compile directly to PHP. Sass fits well into the Rails don’t-repeat-yourself methodology because you can define color and style constants within CSS rather than explicitly specifying them twenty times. The beauty of Sass is that you get a level of abstraction without any runtime overhead. As a result, Sass performs well and allows quick updates to color schemes and typography without resorting to perl/find or ninja text editor foo. So, why aren’t more people using it?

Ruby CAPTCHA / Ruby-GD StringFT Errors Revisited

Posted on July 09, 2007

Well, I finally solved the “undefined method `stringFT' for GD::Image:Class” error under Mac OS X. The rb-gd darwinport seems to have fallen off the face of the earth. Fortunately, I’ve learned that you can download the source from ruby-lang.org. So, I just rolled my own GD.bundle.

wget http://raa.ruby-lang.org/cache/ruby-gd/ruby-GD-0.7.4-1.tar.gz
cd ruby-GD-0.7.4
ruby extconf.rb --with-freetype --with-xpm --with-ttf
make
sudo make install
sudo mv /usr/local/lib/ruby/site_ruby/1.8/i686-darwin8.8.2/GD.bundle /usr/local/lib/ruby/1.8/i686-darwin8.8.2/

This only works if you have an up-to-date ruby installed under /usr/local (via the Hivelogic instructions). If you’ve installed from ports or are using the older ruby shipped with Leopard, you’ll need to switch your prefix to /opt/local or /usr respectively.

Problems with Ruby’s CAPTCHA gem

Posted on June 11, 2007

Even though gd, gd-devel and the ruby GD and CAPTCHA gems were installed on my Red Hat Enterprise Linux 4 server, Ruby’s CAPTCHA gem seemed to blow-up with the error:

undefined method `StringFT'

Hmmm. I googled around and found that you need a library to bind the GD calls to ruby. This makes perfect sense, but documentation for the issue is scarce.

Eventually, I found the following rpm and installed it.

wget http://ftp.at.gnucash.org/opsys/linux/redhat.com/contrib/libc5/i686/ruby-GD-1.0_97111
rpm -ivh ruby-GD-0.7.4-0vl1.i386.rpm

Unfortunately, the RPM assumed that ruby would be installed under /local, but on RHEL everything is under /usr. So, I found where the RPM stored the library and relocated it to the proper directory.

$ rpm -qpl ruby-GD-0.7.4-0vl1.i386.rpm
/local/lib/site_ruby/1.8/i386-linux/GD.so
sudo mv /local/lib/site_ruby/1.8/i386-linux/GD.so /usr/lib/site_ruby/1.8/i386-linux/

Lo and behold, Ruby’s CAPTCHA gem worked like the clappers after this was sorted. Unfortunately, it took me over an hour to sort out the problem. It would seem that something as mundane as CAPTCHA would have just worked under Rails. The major problem seems to be that the ruby-gd maintainer’s site http://tam.0xfa.com is out of commission.

Under Mac OS X, it’s a bit easier because you can use Darwin Ports to install rb-gd. However, that install appeared to bomb for me because I’m using the latest ruby compiled via the Hivelogic instructions. I attempted to symbolically link the ruby binaries to /opt/local/bin, but that didn’t resolve the issue. I’m still looking for a workaround to this.

Iterating Through The Last 30 Days with Ruby on Rails

Posted on June 10, 2007

Recently, I attempted to create a loop in Ruby on Rails that iterated through the last month worth of dates. Stupidly, I just typed in the following code, and tried to run it… (you need to require ActiveSupport for month.ago to work.

# This code does not work
1.month.ago.step(Time.now, 1.day) { |d|
  print d.day,"\\n"
}

Unfortunately, that doesn’t work, because ActiveSupport returns a Time class value for month.ago. Ruby’s Time class doesn’t define a step method. So, being lazy, I just casted the time to an integer with to_i, and constructed the following loop which does work.

1.month.ago.to_i.step(Time.now.to_i,1.day.to_i) { |d|
   print Time.at(d).day,"\\n"
}

Later on, I learned that Date’s have a step class. So, I simplified the loop.

1.month.ago.to_date.step(Time.now.to_date, 1.day) { |d|
    print d.day,"\\n"
}

It seems like there should be an easier way to do this though. Does anyone have a better idea?

Randoword.sh

Posted on October 30, 2006

I’ve had variations of this script kicking around in my .bash_history for quite some time now. The first version of it merely printed a random word from my dictionary file at a rate of one per second. This served as a source of inspiration/entropy for irc conversations and Flickr titles. Today, I decided to extend this one-liner to generate eight-words-at-a-time at a variable rate and jigger the output into an irssi process to amuse the good netizens. Here is the implementation for randoword.sh.


#!/bin/sh
i=0
while true; do
  let i=$i+1
  dictionary="/usr/share/dict/linux.words"
  dl=($(wc -l $dictionary))
  echo -n "randowords #$i: "
  for j in $( seq 1 8 ); do
    head -n$(($(head -c4 /dev/urandom | od -An -tu4) % $dl))\
    $dictionary| tail -n1
  done | xargs
  sleep $((RANDOM % 3600))
done

Update: I never really understood how to use random numbers within a bash script without invoking perl (or equivalent). In light of _fool’s comment, I rewrote my script using only bash and POSIX commands. I had to break-out /dev/urandom and od because bash’s $RANDOM variable only produces random values up to 32767. Technically, it’d be a whole lot faster for a C program to use a pointer to jump around randomly in the dictionary, but that seems too much like work.

Update 2: The seq command probably isn’t POSIX.

Update 3: In Ruby: ruby -e ' $w=STDIN.readlines; 8.times{print "#{$w[rand(483523)].chomp} "}' < /usr/share/dict/linux.words