Category: Rails

mod_rails – first impressions

  • Installing was easy as pie. I was missing the Apache development headers and the installer told me which ‘yum’ command I should use to install them. Nice.
  • When you don’t have heaps of memory, remember setting RailsMaxPoolSize to a lower value to avoid swapping. I’d experiment with RailsMaxPoolSize and saturating the number of concurrent requests using ApacheBench in any case. More concurrent threads is only better until a certain point.
  • Apache/HAProxy/Mongrel was a bit slower than Apache/mod_rails, which was a bit slower than HAProxy/mongrel (without Apache). The difference was nothing to loose sleep over. RailsMaxPoolSize was set the same as the nr of mongrels.
  • While ApacheBench testing, touching tmp/restart.txt to force a restart could cause a hang. Only restart of Apache helped.
  • I would get an occasional time-out and ab stopped (heavy testing, large nr of concurrent requests). BUT this also happened with Apache/HAProxy/mongrel (NOT when using HAProxy/mongrel ‘direct’) so I’d guess this is rather an Apache issue.

Mod_rails seems to be a surprising stable and finished product, and this for a first public release! I’m anxious to check out their Ruby Enterprise Edition.

The occasional hang when touching tmp/restart.txt makes me a bit weary, but this only seems to happen under heavy ApacheBench testing with a high number of concurrent threads. The hangs of Apache under heavy load on my test system, even when no mod_rails is involved might indicate this isn’t even a mod_rails problem. I don’t see this being an immediate issue under normal circumstances.

Thank you Phusion! (I don’t like the logo though)

mod_rails

I’ve been planning on checking out nginx for a while. Mainly as an alternative for apache in my currently preferred apache/haproxy/mongrel Rails install.

I guess I’ll have to hold on to apache a little while longer and check this one first: http://www.modrails.com/

From where I’m standing this is the most interesting thing to happen to Ruby on Rails deployment since mongrel.

Update your rails application without downtime

It’s often not possible or wanted to take down your rails application for updates. Even if it is, I still regard downtime as something that should be avoided if possible.

When using the absolutely fantastic HAProxy to load-balance between your mongrels, you can take down a mongrel process without any adverse effects. When a mongrel process is not answering anymore, HAProxy just forwards the request to the next available mongrel and no harm is done.

So all you need is a way to stop and restart each mongrel, one after the other. I cooked up a bash script to do this. It’s not perfect, but I’ve been using it on several projects. The script stops each mongrel, waits until it’s really stopped, then restarts and checks if it’s really running.

Why a bash script ? I wanted to sharpen my (limited) bash scripting skills (so if you have any tips: feel free to comment).

Customize where needed. (properly indented version here)

If you are running monit, remember to ‘unmonitor’ before and ‘monitor’ after.


#!/usr/bin/env bash

#rails project home dir
#!!Change acc to your needs
cd /home/rails/le_test/current

#Ports mongrels are running on
#!!Change acc to your needs
for PORT in 8000 8001 8002
do
#the location of our pid-files
#!!Change acc to your needs
PF=/var/run/mongrel/mongrel.$PORT.pid

#pidfile exists ?
if [ -e $PF ]; then
PR=$(cat $PF)

#process still running ?
if ps $PR > /dev/null 2>&1; then
echo "$PF $PR Running - killing"
kill $PR
while ps $PR > /dev/null 2>&1; do
echo "$PR still running"
sleep 1
done
echo "$PR killed"
else
echo "$PF $PR Not Running - deleting pidfile"
rm -f $PF
fi
else
echo "$PF does not exist"
fi

#check if a command containing $PORT.pid is still running
#in case process is still active but pidfile was not found
if ps ax|grep $PORT.[p]id > /dev/null 2>&1; then
echo "!!! But process containing $PORT.pid running !!!"
echo "!!! NOT auto-restarting, Manual intervention necessary !!!"
else
echo "Restarting port $PORT"
#mongrel start cmd
#!!Change acc to your needs
mongrel_rails start -d -e production -p $PORT -P $PF
sleep 1
#check if process is actually running now (could also be done by pidfile based check)
while ! ps ax|grep $PORT.[p]id > /dev/null 2>&1; do
echo "Not Running"
done
fi

done

This should work with other load-balancers too, but since HAProxy had all features I expected to find and is very resource friendly and has a tiny footprint (and irons a shirt in 2 mins), I didn’t look any further (after looking at Pound, which only advantage is that it’s easy to install/configure and cisco css switches which are just a tiny bit expensive for my needs)

Getting Real by 37signals

I recently read ‘Getting Real‘, a book (only available as pdf) by 37 signals. 37 signals are the people behind the much hyped Ruby on Rails and a bunch of webapps based on this RoR (=Ruby on Rails) framework.

Why I wanted to read this book
I use RoR with success for an ongoing project at my daytime job and also used it for an ‘after-hours’ sideproject of which at least the development was a success thanks to Rails. I’ve also used Ruby (it works without Rails too) for more little projects and ‘glue’ than I can count.
Seeing that Getting Real is almost as hyped as RoR itself I figured I’d shell out the $19 and check what Getting Real is all about. After all, Rails has turned out to be a very productive and really quite fun web development framework. (but then, after years of doing VC++/MFC development even a lobotomy would seem ‘fun’… or even cobol… nah… just kidding about the cobol)
Continue reading

Fixing a rake schema.rb oracle dump

El Problemo
When issuing a ‘rake db:schema:dump’ (or a simple ‘rake test’ since it uses db:schema:dump) on the legacy oracle db mentioned in the previous post the schema.rb contained some weird entries like:

t.column "timestamp", :datetime, :default => #<date : 4903089/2,0,2299161>, :null => false

The culprits were basically field defaults (like the above, using ‘sysdate’ as default) that aren’t handled properly (not even sure if they should). Since this is a legacy db accessed by tons of mainly C/C++ code on which I’m tacking a web ui, changing this to something that suits Rails is usually not possible.

Any other rake tasks using the schema.rb stumbled over these entries so I was basically unable to do a simple ‘rake test’.

Unfortunately I only found the occasional mention of this issue but not a real solution (yet).

El (hack’n’slash) Solution
Basically all I needed db:schema:dump to work correctly for was being able to run tests. For a variety of accidental reasons I choose to run these tests on a mysql db on a w2k box. (at work I’m using xwin under cygwin on a wxp installation to connect to a CentOS server for my rails development… so this might not seem a logical decision… but explaining the reason behind choosing mysql to run the tests is really totally out of scope of this post)

Easiest solution was to filter the badies (renegades and outlaws) out of schema.rb before it was used for something else.

I made an extra db:schema:clean task to read/filter/write schema.rb, called it from db:schema:dump and put these in a file called ‘databases.rake’ in my Rails lib/tasks folder.

databases.rake:

namespace :db do
namespace :schema do
desc "Create a db/schema.rb file that can be portably used against any DB supported by AR"
task :dump => :environment do
require 'active_record/schema_dumper'
File.open(ENV['SCHEMA'] || "db/schema.rb", "w") do |file|
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
end
Rake::Task["db:schema:clean"].invoke
end
desc "Fix the sysdate timestamps from an oracle schema.rb"
task :clean => :environment do
File.open(ENV['SCHEMA'] || "db/schema.rb", 'r+') do |f|
lines = f.readlines
lines.each do |it|
it.gsub!(/:default => #<date : 4903089\/2,0,2299161>,/, '')
it.gsub!(/:default => "EMPTY_BLOB\(\)",/, '')
end
f.pos = 0
f.print lines
f.truncate(f.pos)
end
end
end
end

(blame WordPress for losing indentation… at least I do!)

Ok… probably really basic stuff for the harcore Railsers out there, but I hope this will help some ‘nuby’ Railsers save some time hunting for where/what/how with this great framework.

(2nd post today, I’ve got another one up my sleeve but will stop now before someone asks me to take a ‘doping test’)

Rails models with attributes that shouldn’t be updated

Situation
A Ruby on Rails project I’m working on during my day-time job (as if I have a night-time one) has a couple of tables with fields that are changed by external processes. (note: it’s a legacy oracle database that’s had some slight mods where possible to make it more Rails-y). The bulk of fields in these tables however contain data that can be changed using a Rails interface I’ve build. These not to be updated fields need to be initialized when writing a new record and should be read (to be displayed) as normal fields.

So what’s the problem then?
Normally you’d just issue an update to only modify the needed fields, but at the moment ActiveRecord can only alter records by doing a read followed by an update of all fields (please correct me if I’m wrong).

This poses the problem that some fields might be changed by an external process after the read and before the write, resulting in ActiveRecord writing the old data back to the table. (resulting in a whole bunch of unwanted, very evil things of which I really don’t even want to think about fixing)

Solution
I found a workaround by overriding update_attributes in my model as
follows:


def update_attributes(attributes)
@attributes.delete('fieldthatshouldnotbechanged1')
@attributes.delete('fieldthatshouldnotbechanged2')
@attributes.delete('fieldthatshouldnotbechanged3')
super(attributes)
end

This effectively deletes the fields from the hash that’s used to construct the update statement. You’ll see in your development.log that the issued update statement doesn’t contain the deleted fields anymore.

I only tried it with update_attributes since that’s what I used to update the fields.

I hope this doesn’t cause any unwanted issues, but if it does I’ll post about it here.

Switch between Rails controller and view in Komodo

Yesterday I tried radrails, which is quite nice for a free tool. Being a Komodo fan however (thanks to my employer for paying the license) the only obvious advantage for me would be being able to switch between controller and corresponding view.

Because I was getting a bit jealous of this feature in Textmate/RadRails/… I cooked up a way to do the same in Komodo.

How can this work in Komodo ?
When calling external commands (via the Toolbox) in Komodo you can give path + linenumber of current file being edited (amongst others) as parameters. You can start the Komodo binary passing a filename + linenumber, and Komodo is nice enough not to start itself twice or load the same file twice when doing this.
The ‘figuring out what controller/action we’re in and calling the corresponding view‘-part (and vice versa) is handled by a little ruby script. (which could probably be much tighter, but I’m still a ‘ruby nuby’ and if I have to clean it up before publishing it’ll probably never leave my personal Toolbox… so be thankfull I have enough guts and ego to dare publish my dirty code 😉

How to install this script in the Komodo toolbox ?

  • Save the script below (let’s call it ‘open_ctrl_view.rb’) wherever you like to save such things.
    Remember to chmod +x it to make it executable!
  • In open_ctrl_view.rb : Edit the path to your komodo installation, or just put ‘komodo’ if komodo is in your search path.
    Edit the shebang to point to the correct ruby version if necessary.
  • In komodo: add new command in toolbox. As command, enter ‘open_ctrl_view.rb %F %L’ (include full path to whre you saved open_ctrl_view.rb if necessary)
    %F will insert the full path and %L the current line of the file being edited
  • Run in: ‘Command Output Tab’ (default option) and I marked ‘Do not open output pane’ below. If necessary you can always switch to the output pane manually if something went wrong.

Add key binding if necessary (warning: this is a bit buggy in Komodo 3.5.2 but should be fixed in 3.5.3)

Windows users: Probably the same as above, except no need for chmod’ing, and you’ll probably need ‘ruby open_ctrl_view.rb %F %L’ instead as command.
Mac users: Aren’t you using Textmate ?

The script (open_ctrl_view.rb)
No guarantees are made. Your head might explode, ladadi ladada, etc.
I was going to include it as text in this post, but even enclosed in the proper tags it looks all funny, so download it here.

Closing comments…
If you make nice enhancements: let me know! (what about a similar script that loads the model your cursor is positioned on ? Komodo can also send the word your cursor is on as a parameter)

Conditional partials in Ruby on Rails

Have you ever wanted to include a partial in Ruby on Rails only if the partial file actually exists ? This can be handy e.g. if you want to be able to include a special menu (as a partial) for some controller actions based on the action name (controller.action_name) but don’t want to make a dummy partial for every action (which is normally needed since Rails will throw an error if it can’t find a partial).

The following example will render a partial contained in the file _.rhtml only if this file exists:

def render_menupartial
render_partial(controller.action_name) if FileTest.exist?(File.join(RAILS_ROOT, 'app', 'views',controller.controller_name,'_'+controller.action_name+'.rhtml'))
end

remember to call this from your view (or probably layout) like < %= render_menupartial %> instead of < % render_menupartial %>.

I know: basic stuff, but since I already used this basic functionality quite often I’m sure someone out there will find this useful.

Ruby/OCI8, Oracle and ORA-12705

After installing the Ruby/OCI8 module on a Windows XP installation where sqlplus is working fine I got the following error when testing the connection in irb:


irb(main):012:0> cn = OCI8.new("username","password","database")
OCIError: ORA-12705: invalid or unknown NLS parameter value specified
from c:/ruby/lib/ruby/site_ruby/1.8/oci8.rb:158:in `begin'
from c:/ruby/lib/ruby/site_ruby/1.8/oci8.rb:158:in `initialize'
from c:/ruby/lib/ruby/site_ruby/1.8/oci8.rb:158:in `do_ocicall'
from c:/ruby/lib/ruby/site_ruby/1.8/oci8.rb:158:in `initialize'
from (irb):12:in `new'
from (irb):12
from :0

This one really took me several hours of google-hunting, finding 0 direct solutions. I was almost throwing my hat in the ring when I finally found the solution.

The Problem
In this case the ‘NLS parameter’ I was having a problem with turned out to be NLS_LANG.

This parameter is supposedly set during installation (I can’t remember choosing this, but this must be the only Oracle client installation I’ve ever done on MS Windows). The ORA-12705 error was cause by my client Ruby/OCI8 sending an incompatible NLS_LANG language parameter when connecting with this specific Oracle db.
Sqlplus, enterprise manager,… were all working fine.

Where is NLS_LANG stored on the client ?
The value was in my case stored in 3 different locations in the windows registry, always in a key ‘NLS_LANG’, once at HKEY_LOCAL_MACHINE\SOFTWARE\ORACLE, and 2 more times in sub-keys of this location. Just do a search in regedit for ‘NLS_LANG’ and you’ll find the culprits. 1 of these NLS_LANG had the value ‘NA’, the other 2 were ‘DUTCH_BELGIUM.WE8MSWIN1252’.

If you want to test if this is the source of your issue, then apparently you can override this value using an ‘NS_LANG’ environment variable. To test this, just do ‘set NLS_LANG = ‘ in your dosbox before starting irb.

How do I know what language the Oracle db expects ?
Open a connection to the db with sqlplus (or whatever it is you’re using) and issue the following:

SQL> select USERENV('LANGUAGE') FROM DUAL;

Result:

USERENV('LANGUAGE')
----------------------------------------------------
AMERICAN_AMERICA.WE8ISO8859P1

To view all NLS parameters, issue:

select * from NLS_DATABASE_PARAMETERS;

When doing ‘set NLS_LANG=AMERICAN_AMERICA.WE8ISO8859P1’ before starting irb, connecting to the db went fine.

Will this work with rails ?
I tried ‘set’ting NLS_LANG before starting webrick, but to no avail. I really had to change registry values for rails to work when running onder webrick.
I guess (hope) it will be the same when running under a more production-level server too.

What if I need to connect to several different db’s which expect different language settings ?
God knows.
I’d start by trying to set all NLS_LANG registry entries to ‘NA’. If that doesn’t work, I’d try to find out if it’s possible to set this as a parameter when connecting. But how you would do this in Rails: ???

Please let me know if you find out!