Fork and Ruby!

8 10 2008

Utilizing a huge amount of resources…i.e. pwnage on a Dell 2950:

Terminal — ssh — 135×55
Uploaded with plasq’s Skitch!

Check out how all 8 cores are getting used and both network cards are screaming from my 16 ruby processes pulling down a shit-ton of data.





Rescuing Exceptions… err… I mean StandardError in Ruby

7 10 2008

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:

example.rb
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!




Rentmappr.com now has RESTful search!

15 07 2008

Rentmappr.com now has RESTful search.  Here’s how it works:

You can now just fill in the url to search for properties. Every option available in the search form has been put into a RESTful url that are built in the following format:

www.rentmappr.com/search/minimum_price/maximum_price/bedrooms/

You can add “none” for min_price, max_price, or bedrooms, to say you don’t have a preference.

You can search for pets by adding /cats or /dogs, or /cats/dogs for both.

Some examples

/search/1850/none/3br/dogs will search for properties with a monthly rent of at least $1850, with no maximum price, that has three bedrooms and allows dogs.

/search/none/2650/none will search for properties with no minimum price but a monthly rent capped at $2650 and with no preference on bedrooms.

/search/1850/2650/4br/dogs/cats will search for properties between $1850 to $2650 per month that is four bedrooms and allows cats and/or dogs.

Currently you will have to pick a city to search in before these url’s will work.

I am working on a RESTful url search that will let you target specific markets.  Using the same schema from above I would only have to add the city’s name to the url.

/search/boulder/1850/none/3br would search for houses in Boulder with at least a monthly rent of $1850 that has three bedrooms.

This feature will shortly be extended into a “link to these results” generator, making sharing of results much easier, ala “link to this map” in google maps.





Rentmappr.com: more things coming soon…

9 07 2008

So I did some analysis on my rentmappr.com 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

26 06 2008

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.

Pre-req’s:
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 = MemCache.new(:namespace => "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.





Protected: BR BP Questions.

10 12 2007

This post is password protected. To view it please enter your password below:






Gmail with Rails…just so I don’t forget how to do it.

6 12 2007

Smtp settings in config/environment.rb:

ActionMailer::Base.smtp_settings = {
:address => “smtp.gmail.com”,
:port => 587,
:domain => “christm.us”,
:authentication => :plain,
:user_name => “admin@christm.us”,
:password => “*******”
}

Get code from this very helpful post.  Make the folders and files

  • action_mailer_tls
    • lib
      • smtp_tls.rb
    • init.rb

and copy/paste his code into the correct files you just created





LightMate Gedit Theme and now for TextMate too!

5 12 2007

Ok kids, finally got the themes available up on my website: http://ubermajestix.com/uber/themes.

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

4 12 2007

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: http://crepuscular-homunculus.blogspot.com/2007/10/gedit-for-ruby-and-everything-else-on.html

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: http://www.tatanka.com.br/ies4linux/page/Installation:Ubuntu

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

http://my.opera.com/area42/blog/gedit-browser-preview-plugin
http://my.opera.com/area42/blog/gedit-language-reference-plugin
http://my.opera.com/area42/blog/gedit-template-plugin





Get the value of a selected option with Javascript

4 12 2007

Ok, there were no good straight forward references on google. So…here’s how you do it…

I have a select box…

<select>
<option value=1>One</option>
<option value=2>Two</option>
</select>

when the user selects any option I wan to get the value of the option (I’m trying to evaluate the option value and then based on the selection, show the user another part of the form..)

So in your select tag you drop in:

onchange="this.options[this.selectedIndex].value"

  • “onchange=” – this is triggered when the user changes the option in a select box (onclick is not supported in IE)
  • “this.options” – this gets you the array of options in your select tag, here this would be: [1,2]
  • “this.selectedIndex” – this gets you the index of the item selected…so if the user selects option “One” this.selectedIndex=0, for the first element in the array of options.
  • “.value” – this just gets me the value of the option, here that’s either 1 or 2. If you want the text (“One” or “Two”) use “.text”

So lets put it together (i’ll add my conditional showing of another piece of the form too):

<select onchange="if( this.options[this.selectedIndex].value=='1')
{Element.show('another_piece')}
else
{Element.hide('another_piece'}">
<option value=1>One</option>
<option value=2>Two</option>
</select>
;

Check out my rails work in action: http://my.christm.us – helps keep your christmas shopping lists organized and on budget.