Rescuing Exceptions… err… I mean StandardError in Ruby

Linux, Ruby

I’ve heard from people how or how not to rescue things that might break in ruby.  And I was confused.
Your basic rescue frame of mind is something like this:

15306 2014 GitHub
Uploaded with plasq‘s Skitch!

This won’t give you a very good description of what you rescued so you can rescue specific errors and exceptions like this:

15307 2014 GitHub
Uploaded with plasq‘s Skitch!

But! I want to catch EVERY POSSIBLE ERROR I MIGHT RUN INTO! So why not just rescue the whole Exception class? Yeah that sounds like a great idea.

15307 2014 GitHub
Uploaded with plasq‘s Skitch!

Ok so that catches anything we might run into BUT NO!!! Its not a great idea. Let’s illustrate:

Uploaded with plasq‘s Skitch!

Now let’s say we have a script that is taking FOREVER to do anything like the one above. Let’s send it a kill signal via ctl+c or you could call kill with the process id from the command line. So ready set… kill! Die die die!

Um wait nothing happend…

What did the logger say?

Uh wait what!?!?!

Terminal 2014 bash 2014 142�0
Uploaded with plasq‘s Skitch!

Ah so rescuing Exception rescues Interrupt (ctl+c) and SignalException (kill).  In this case I had to call kill -9 to get the damn thing to die. The kill -9 command is kind of a silver bullet but you can end up with a mess so don’t use it when you don’t have to, like my associate who crashed a whole cluster of servers that ran a large rental video chain’s backend because kill -9 on that weird flavor of linux did a lot more than just kill the one process, it killed everything that didn’t belong to his user (i.e. goodbye oracle db processes)!

What we should do is rescue StandardError unless you really want to rescue things like ScriptErrors (when you’re eval-ing ruby code) or rescue SignalExceptions (like do something before the thing dies).

This should help:

The Ruby Programming Language
Uploaded with plasq‘s Skitch! more things coming soon…

Linux, Rails, Ruby

So I did some analysis on my results.  Looks like I get on average about 84% of postings to successfully geocode and get posted to rentmappr from craigslist.  I do have an address correction algorithm in place but I haven’t analyzed its results for success.  From watching the geocoder do its work though, I estimate I get about 20% of the postings that have a “bad” or non-geocodeable address to successfully geocode.

I will shortly be adding bedroom search to further narrow results.  I did push dog and cat search options to production last week.

I’m also pleased to announce that my roommate successfully found, and rented, an apartment using rentmappr!  Her glowing testimonial to follow.

Memcache Sessions in Rails

Linux, Rails, Ruby


Does rails scale? No! But memcached does!

Ok ok, you aren’t probably going to need memcache for caching huge queries yet.  But it is useful for storing your user’s session data server side and not having to worry about filling up cookies or clearing out files in your tmp directory or your database.

So I’ve been designing a single sign on system for the interanl applications at CI.  And I need apps on all kinds of different servers to talk to the same memcache instance(s) for their sessions.  I did follow this article on err.the_blog to get started but I only got so far and ran into some issues that got hard to debug.

The default memcached session store hits localhost:11211 which is generally fine for most applications but I needed a clustered approach and could not for the life of me figure it out.  Turns out like most things with rails it was really easy.

1. Memcached installed on your machine or whatever machine you want to use for sessions
1a. Top Funky has a really simple shell script that will work on os x and another that works on ubuntu
2. You will need the memcache-client gem, you know what to do.

In environment.rb in my rails app I needed to do a few things:

1. Setup the connection to the memcache server

2. Tell rails to use memcache for sessions

3. Setup rails to drop all its sessions stuff into the memcache server we setup

Alright so 1:
require 'memcache'
CACHE = => "your_app")
CACHE.servers = 'some_ip_address:some_port', 'another_ip_address_if_you_need_it:some_port'

and then 2(pretty easy)
config.action_controller.session_store = :mem_cache_store

and then 3 (this is where you can set the session timeout and then pass in the memcached object CACHE)

config.action_controller.session = {
:session_key => '_your_app_session',
:secret      => 'someotherkindofsecretthatnooneknows',
:cache       => CACHE,
:expires=>900  }

I think if memcache doesn’t find the session on one server it’ll look on the others you put in the CACHE.servers list.  However if your memcache instance goes down your rails app is hosed and starts throwing up 500 errors all over itself.

You can actually throw alot of this config into a gem if you are going to have several apps that are all going to use the same config (setup the CACHE stuff in your gem and then just do step 3 in your apps)  That way if you need to switch or add servers to your memcache setup you don’t have to make changes in 20 places.

Server Meltdown…literally

Linux, Windows

So i was serving off of my acer laptop running ubuntu 7.10.  It was doing some hardcore multi-threaded processing of craigslist data every 30 minutes so I was using a lot of cpu (like 75% on a dual core machine).  So the acer got kinda hot.  And then my roommate in his infinite knowledge of computers (which is limited to let’s build a windows machine to play counter strike on) decided that my laptop was too hot for not “running any programs because i opened it up and there weren’t any windows open so I just shut it off and unplugged it and put it in your room.”  I did stash the thing in his room because he insists he has a hard line to the router/cable modem for his gaming, and I didn’t want to host a website on wireless.

ANYWAY, I had it fired up for a few days in my room on wireless and then for some reason I unplugged it or I don’t know but I wouldn’t turn back on.  My other roommate about the same time was complaining about a power surge b/c her alarm didn’t go off so I thought maybe my power adapter got fried (wouldn’t be the first time).  So I tried the cord in several different outlets and was sure it was fried so i started to splice the connector onto another laptop power supply and then realized that the internal center pin that the ac adapter plugs into was bent.

I attacked the pin with a screw drive to see if I could bend it into place but it turns out the whole rig just crumbled and fell into the case.  So now it looks like (after trying to get the f-ing case off the acer, and yes I even used an electric jigsaw at one point no dice) that the inner piece that fell off would have to be soldered/epoxy’d back in place to get the damn thing to turn on.

Well there goes 8000 lines of my rentmappr database.

Guess I could pony up for a cheap tower and maybe get a sata external laptop hardrive case to port my old data over oh well guess I shouldn’t be doing hardcore multi-threaded site scraping on a laptop stashed behind my roommates desk.

Ubuntu sucks…get a Mac.

Linux, Windows

Alright alright, I’m not saying Linux sucks – Linux is awesome for what is does and what it does well – 1. be cheap and 2. be fast – which makes it a great choice for a server – not desktop users.

I got real sick real fast of Vista – for all the reasons I don’t need to explain – and being low on funds I tried out some Ubuntu goodness (not greatness) – you can see some of my endless frustration in previous posts about getting wireless working (not only just working, like turning on, but connecting to WEP and WPA encrypted networks), getting ANY hardware to function well has been a pain in the ass – I don’t want to go hacking system files to get my mouse or monitor work the right (I’m not saying I can’t do it but my mom isn’t a l337 hax0r and if Ubuntu is Linux for people this shit has got to change).

Ok so the point of this is get a Mac. Why? Because for the most part, shit works when you plug it in – monitors, mice, mobile broadband cards, printers, networking, vpn, wireless – all of it – and the OS is fast as shit.  Ok its expensive but Apple has provided the first serious Unix environment intended for real stupid people to use.  the company i work for will only buy macs for the development crew – why – because shit works – you don’t dick around all day with hardware or software issues.  So just save up the cash and get one (mac mini…maybe not…mac book pro…yeah definetly.  Oh and get an external hard drive – we’ve had a couple disk failures but TimeMachine or SuperDuper! will restore your machine in about an hour to the last backup point (apps and all).

RealPlayer in Ubuntu!


So I peruse TurnTableLab for the new gems of vinyl out there in the world. But all of their audio samples are in real media (.rm or .ram) format. So of course I tend to only do this in windows.

Well today I figured out (its super easy) how to get RealPlayer running in Ubuntu.

Go to or download the .bin file

open up a terminal window

type: chmod a+x RealPlayer10GOLD.bin

then type ./RealPlayer10GOLD.bin

just hit enter through the prompts and you should be up and running!

LightMate Gedit Theme and now for TextMate too!

Design, Linux, Rails, Ruby

Ok kids, finally got the themes available up on my website:

Check the screenshot below for the gedit theme or hit up my website for screenshots of Textmate.

I really hate working in dark environments…well lights off yes, but a bright computing environment is a must. Thus when customizing gedit to work like TextMate for rails work I really hated the DarkMate theme – and the default lightish theme I didn’t like either.

So I made my own based on some colors from DarkMate. Take a look:

  • rhtml:

rhtml file

  • ruby:

ruby file

UPDATE: Theme is for GTKsourceview-2.0 and later (this is installed with Gutsy Gibbon). This theme will not work with Feisty or earlier releases.

download the theme here!

Upgraded to Ubuntu Gutsy Gibbon

Linux, Rails, Ruby

I went through with it on my dev machine at work b/c I heard there were lots of great new things in gedit and openoffice might stop crashing (gotta love the java apps).

I updated all the components update manager told me to BEFORE I ran the upgrade. I heard there were problems if all Feisty updates were not applied, so I updated. That took about 45 minutes.

Then I ran the upgrade package – I had to delete some custom software repositories in System=>Admin=>SoftwareSources because they were no longer live and the upgrade was trying to load the Gutsy source from them…I just switched the Download From: to Main Server and unchecked the repositories giving me issues under Third Party.

After that the upgrade went smooth…I was still trying to work while doing the upgrade so it took about 2 hours to complete. I lost one text document I tried to save when the installer was doing its thang (it locked the local filesystem) but thats the only other trouble i ran into.

Rebooted and it all looked fine except for gedit. This needed to be re-customized for rails since they change a whole bunch of crap in gedit. I followed the instructions here:

And whala everything in gedit is working…i did sudo apt-get install gedit-plugins

to get some extra fun stuff like a color picker, bracket matcher…I think I like Gemini better b/c I can select text and then wrap it with quotes and brackets…but it does completion of “<” those guys which I hate because I use “<<” all the time to add an element to an array so Gemini makes it into “<><“…anyway, the default color scheme is a little different…actually I don’t like it but I also don’t like Darkmate style which everyone raves about…so I have to find one that works.

I also installed IE6 under Wine…I followed the instructions here:

And this dude has some interesting/useful plugins for gedit also:

Building Pidgin from Source on Ubuntu Fiesty


No Pidgin package pre-made for Ubuntu? Guess I’ll have to build from source!

Step 1: Install a bunch of libraries:
sudo apt-get install libglib2.0-dev
sudo apt-get install libgtk2.0-dev
sudo apt-get install libxml2-de
sudo apt-get install libnss-dev

Step 2: extract source to some directory

Step 3: cd to directory you extracted the source to and type the following commands.

Step 4: ./configure

Step5: wait

Step 6: make

Step 7: wait a lot more (like 5-8 minutes)

Step 8: sudo make install then wait about 15-20 minutes

Step 9:sudo make clean

Step 10: make a launcher – a. right click application panel b. click add to panel c. click “custom application launcher” d. name: Pidgin; command:pidgin %u ; icon: /source/directory/you/made/previously/pidgin/pixmaps/pidgin.ico