Author: Piet

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)

Quiet 2007

Ok, 2007 was a bit quiet on the blogging side. Been squeezing the max out of every minute of spare time and been working/learning as hell. Maybe more about what I’ve been upto later.

The layout started looking a bit stale so I modded a layout I like (credits: see bottom of page) to something I like even more (gustibus and stuff) and gave WP the much needed update. So there, giving a reason to unsubscribe to the 3 people that had forgotten that they were following my feed!

CSSVista: Internet Explorer Live CSS Editing

This tool has been around for quite some time, but it often surprises me how few people (also ‘professional’ ones) seem to have heard about it. Since it’s the only FREE live CSS IE editing tool I know, and since (added bonus!) it actually offers split-view Firefox/Internet Explorer editing, there’s no harm in spreading the word a bit.

Get it at: http://litmusapp.com/labs. DL link is sent to email address provided. However, I’m pretty sure they honour their no-spam policy.

Oh yes… it only runs on windows. But since there’s no escape having to consider IE users browsing your site, it’s almost worth keeping a windows installation for (that, and homebanking).

Tip: If your site requires a login and you have problems logging in using CSSVista: save the page you want to edit using Firefox and open it in CSSVista for editing.

And just in case you’ve been hiding under a rock, for day-to-day live CSS editing use (running fine under Linux!) there’s the webdev swiss army knife: Chris Pederick Web Developer Extension.

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)