Skip navigation

I recently had the following error while configuring Nginx with Unicorn on Rails 2.3.8

in my config.ru, I had the following:

(taken from the Ruby on Rack instructions on the ‘official’ guide page, http://guides.rubyonrails.org/rails_on_rack.html )
config.ru
======
require “config/environment”

use Rails::Rack::LogTailer
use ActionDispatch::Static
run ActionController::Dispatcher.new

This was giving the following annoying error when trying to start the unicorn server:

/usr/lib/ruby/gems/1.8/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:443:in `load_missing_constant’: uninitialized constant ActionDispatch (NameError)
from /usr/lib/ruby/gems/1.8/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:80:in `const_missing’
from /usr/lib/ruby/gems/1.8/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:92:in `const_missing’
from config.ru:5
from /usr/lib/ruby/gems/1.8/gems/rack-1.1.2/lib/rack/builder.rb:46:in `instance_eval’
from /usr/lib/ruby/gems/1.8/gems/rack-1.1.2/lib/rack/builder.rb:46:in `initialize’
from config.ru:1:in `new’
from config.ru:1

It turns out that a patch submitted to unicorn_rails gave the hint (http://bogomips.org/unicorn.git/diff/bin/unicorn_rails?h=body-keepalive&id2=98ea5cca50b907e20d6357f425d7789bac1d1a47 )

The fix was to replace the line #use ActionDispatch::Static with:
use Rails::Rack::Static

The new config.ru thus became:
use Rails::Rack::LogTailer
use Rails::Rack::Static
run ActionController::Dispatcher.new

I am guessing this had to be done because ActionDispatch::Static isn’t present in this version of Rails.

Using Ruby on Rails callbacks, you might be tempted at some point to do this

In your model:
after_save :test_for_something

And then
def test_for_something

if (some_test) then self.errors.add(“Problem”); return true; else return false; end

This is not going to work as it is not handled as a validation. The record will be saved in the db even though this will return false

Similarly, if you are inside a transaction, do not rely on after_save for throwing an exception that would rollback the transaction, as it’s not going to happen.
Doing this inside the after_save will not cause an exception to be thrown
raise ActiveRecord::Rollback

The best way for doing that is keeping validations separate by having a separate validate :method_name method.

Recently I’ve been using  Amazon’s Cloud Computing services in the form of EC2 and S3. I’ve set up several scripts that make life easier by automating some of the tasks that need to be done regularly when administering EC2 virtual instances. In the hope that they will be useful to other people, I’ll be posting here some of them. Here is the first in the series.

The script “reallocate_volume” moves an EBS volume from an instance to another. It gets both instance ids as arguments on the command line. It is useful, for example, when your main production machine (first argument) fails for some reason, and you need to give control to the second machine. Below, the SLEEP_TIME is in seconds and MAX_TRIES is a number specifying delays for retries when detaching & attaching the volumes.

reallocate_volume:

#!/bin/bash

#Replaces volume in instance given as argument $1, with the volume taken from instance in argument $2
# {insert your commands that stop your servers here, e.g. apache & mysql}

umount /mnt/vol #make sure no-one else uses this, otherwise this will fail with inability to umount

volume1=`/root/bin/ec2get_volume_id $1`
ec2-detach-volume $volume1 –force #note that it uses force to ensure this gets detached

STATUS=`ec2-describe-volumes $volume1 | tail -1 | awk ‘{print $5}’ 2> /dev/null`
SLEEP_TIME=10
MAX_TRIES=15
while [ “$STATUS” != “available    ” ]
do
CTR=`expr $CTR + 1`
if [ $CTR -eq $MAX_TRIES ]
then
/bin/echo “WARNING: Cannot force detach volume $VOL to $DEV — Giving up after $MAX_TRIES attempts. Please try to use snapshot”
exit 1
fi
/bin/sleep $SLEEP_TIME
STATUS=`ec2-describe-volumes $volume1 | tail -1 | awk ‘{print $5}’ 2> /dev/null`
done

#attaching to the new machine
ec2-attach-volume $volume1 -i $2 -d /dev/sda2

STATUS=`ec2-describe-volumes $volume1 | tail -1 | awk ‘{print $5}’ 2> /dev/null`
SLEEP_TIME=10
MAX_TRIES=15
while [ “$STATUS” != “attached” ]
do
CTR=`expr $CTR + 1`
if [ $CTR -eq $MAX_TRIES ]
then
/bin/echo “WARNING: Cannot attach volume $VOL to $DEV — Giving up after $MAX_TRIES attempts”
exit 1
fi
/bin/sleep $SLEEP_TIME
STATUS=`ec2-describe-volumes $volume1 | tail -1 | awk ‘{print $5}’ 2> /dev/null`
done

mkdir -p /mnt/vol
mount /mnt/vol

# {insert your commands that start your servers here, e.g. apache & mysql}

Now this depends on one other script called ec2get_volume_id which gets the volume that corresponds to the instance id given as argument to it:

ec2get_volume_id

#!/bin/bash

#gets the volume for the instance id specified in the argument $1
ec2-describe-volumes | grep $1 | awk ‘{print $2}’

To remove pulse audio in Karmic:
Code:
$ sudo apt-get purge pulseaudio gstreamer0.10-pulseaudio
Code:
$ sudo apt-get autoremove
Code:
$ sudo apt-get install alsa-base alsa-tools alsa-tools-gui alsa-utils alsa-oss linux-sound-base alsamixergui
Code:
$ sudo apt-get install esound esound-clients esound-common libesd-alsa0 gnome-alsamixer
restart your computer!
Notes:
-run gstreamer-properties in terminal to set defaults to alsa (the old system/preferences/sound in jaunty)
This means:
Code:
$ gstreamer-properties
-remove gstreamer0.10-pulseaudio to get sound in totem.
The above commands should have taken care of that, but it never hurts to be sure when talking about the hydra that is PulseAudio.
-gnome-alsamixer is for changing the volume, not an applet but better that nothing
This means you are left without a panel applet to control volume across the board, thanks to the thoughtful efforts of Canonical to shove PulseAudio down our throats. So you need to have something to control audio with:
Code:
$ sudo apt-get install gnome-alsamixer
And something to keep gnome-alsamixer within easy reach, provided by AllTray:
Code:
$ sudo apt-get install alltray
Now, one last word of advice. Usually the best solution is to use aptitude or synaptic to do these moves, but you’ll run headfirst into their dependency resolution efforts, which command them to put PulseAudio back in your system when reinstalling gnome-alsamixer. So either you use apt-get or you reconfigure aptitude/synaptic dependency resolution to work the way you want.
Reference: http://ubuntuforums.org/showthread.php?t=1313253