Monthly Archives: April 2009

How to set up a debugger with mod_rails/Passenger

Anyone who's worked on the web will know easy it is to end up constantly refreshing pages to see if the content delivered from say, a database driven site is the indeed content you want to show.

This is one approach, and while simple to understand, there are often other approaches available to this.

One example is using the ruby debugger, and break points in your code to inspect and control what is happening at each step, to see what variables are available.

This, combined with the more traditional tools for debugging a view using @object.inspect or debug(@object) methods make fixing bugs less of a pain.

As many of us transition from using Mongrel as our server to Passenger, we find ourselves missing this useful tool. This post outlines one way to setup a debugger for Passenger.

Putting a debugger back in Passenger

Before people started using Passenger for handling rails requests, using the ruby debugger was simply a case of adding debugger statements in the code, and then starting the Mongrel or Webrick with script/server --debugger.

When the code hit a debugger statement, the console would drop into an interactive debugger, and all would be well with the world.

Passenger currently doesn't allow an easy way to use the debugger straight out of the box, so we'll have to make a few changes to the code in our own environments, and use a gem called ruby-debug if we want this behaviour again.

Add ruby-debug

We're actually using a gem called ruby debug - this gives us a powerful remote debugger, to inspect code - it does load of other interesting things, but in this case we're only interested in a subset of the features here.

 
`sudo gem install ruby-debug`

Add code to use ruby debug when you hit a debugger statement

Just like how we restart Passenger using the touch tmp/restart.txt file, the nicest way I've found to switch on the debugger comes from the ominously titled duck_punching weblog. It involves adding a rake task to create a debug.txt text file, and adding a snippet of code to your development.rb config file:

 
    if File.exists?(File.join(RAILS_ROOT,'tmp', 'debug.txt'))
      require 'ruby-debug'
      Debugger.wait_connection = true
      Debugger.start_remote
      File.delete(File.join(RAILS_ROOT,'tmp', 'debug.txt'))
    end

This snippet checks for the existence of a debug.txt in the tmp directory of the main Rails directory, and if it's there, it opens a connection to connect to for debugging.

And here's there restart task for rake:

    desc "Restarts passenger if in debug mode"
    task :restart do
      system("touch tmp/restart.txt")
      system("touch tmp/debug.txt") if ENV["DEBUG"] == 'true'
    end

To run in debug mode, you just pass the constant DEBUG in while calling the rake task like so:

    rake restart DEBUG=true

Sprinkle in your debugger statements

We can now add the debugger statements where we want the breakpoints in our code for us to inspect.

Open a rdebug session

Now we can finally open our ruby debug session, using the -c switch to start our client connection.

    rdebug -c

Because we're connecting to the same machine as we're running rails on, it defaults to localhost, but we can connect to other remote machines too.

Gotcha - Remember to reboot!

Once you've finished your command line debugging session, and exited the debugger, you won't be able to start a new sessions without calling the rake:restart command with the DEBUG=true values.

Now that you have this set up, take a look at this Railscast by Ryan Bates or this one by Brian Donovan to get an idea of just how useful the ruby debugger is.

Patrick Lenz has another really helpful article that goes in depth with the commands available to you during a debug session - I found it incredibly useful, and I can't recommend it highly enough.

Letter loungin’

DSC_2187

Yesterday, I put on an event with Claire Medcalf from Glovepuppet, and a few friends, called the letter lounge. As you'd expect from the name, we started it to help people get round to actually writing more letters to distant friends and relatives, and take the time to reflect and generally slow down somewhat, as a way of clawing back some tranquility and respite against the whirl of tweets, emails facebook messages and IM chats that punctuate our lives.

And suffice to say, I'm really happy with how it turned out -turnout was high enough to be encouraging, and low enough to mean Claire and I could take the time to write to people instead of playing host the whole day.

However, without a doubt, the highlight of the day, apart from seeing Claire's entourage waiting outside the hub with a full on toy postbox for us to use, was hearing from one of the visitors, telling me that because of this event, she finally wrote her first physical letter in 15 years - how cool?

Made my day, and pretty much sealed the deal for us to running another Letter Lounge as soon as we have a chance.

If you want to hear more about the letter lounge... yes it's on twitter - @letterlounge, and now we've run one of these events and have an idea of how to do them, we're going to make it much easier to find out about future ones so we start building some momentum with this idea.

But first, sleep...

Tea, Arduino and Dynamic Demand

It’s the next HomeCamp tomorrow. And in a spectacular feat of bad calendaring, I’ve managed to organise the Letter Lounge to take place on the day I’ve been looking forward to for the last few months are coming away from the initial HomeCamp, utterly inspired by what I saw there.

So to make up for not being able to attend, the least I can do is finally get round to writing about the Tealight, a fun, Homecampy little project James Gardner and I built one evening in the Hub recently.

We did it partly to learn about the Arduino platform, and partly to explore how to make something as complex as demand response intelligible to people who don’t think about this for a living, and well here it is, in all it’s janky, low budget hackish glory:

This green orb constantly polls the national power grid to see how it’s keeping up with demand from everyone watching The Apprentice, and subsequently whether your next cuppa will be a particularly carbon intensive one.

If there’s spare capacity on the grid, the tealight will glows green, it’s basically saying:

‘Go ahead! Make some tea! Knock yourself out!’

If there isn’t, the colour shifts to red, saying:

“Now’s not the best time for that cuppa, give it a little while.”

The main idea here is that you can glance at the globe from across an office or co-working space, to get an idea about whether making that cup of tea is a good idea right now, without having to think too hard about it.

Think of it as national grid load balancing, using people, and hot beverages.

How we made this

This is thing is almost embarrassingly simple.

On the software side, we’re basically polling the api for caniturniton.com a website which scrapes the national grid for power usage data every minute or so, and depending on if the figure returned is higher or lower than the baseline of 50hz, we call a function to tell the BlinkM to change the colour of it’s LED accordingly.

Here’s the shopping list of hardware we used:

The fact that we could even make this, with me generally not being big on writing in any languages that feel like C, and with James being almost completely new to the platform is a testament to how lego-like the Arduino ecosystem is getting now.

When we take the shade off the lamp, it’s quite a bit easier to see how it all works:

Tealight working without the globe

The orange cable is a ethernet cable, and the grey one is the usb power, running off a spare iPod I had lying around.

The code has largely been a messy cut and paste job spliced from number of sources, but mostly from this thread in the arduino forums, and the string parsing code example is from a post by Jeff Tanner here on the daniweb forums.

This isn’t really big enough to be considered a project on github, so I’m just chucking a gist up for now:

Isn’t this massively oversimplifying and trivialising the whole concept of demand smoothing?

You could argue so, yes.

But placing it in a relatively high traffic co-working space, full of people working in totally unrelated fields is a great opportunity to speak to them about the ideas inspiring this little toy, and get lots of interesting feedback, and see how best to communicate on issues related to climate change and how massively energy intensive our life styles are.

But why tea?

A number of reasons:

  • they provide a recognisable power spike for comparing against national averages, and also for graphing against national load averages too. It makes it easier to see if behaviour is changing.

  • there’s a pattern to making hot beverages at work, but it’s pretty easy to timeshift it, without getting into emotive issues like calling people utter carbon bastards for flying to Spain on holiday.

  • it’s a usefully mundane , which makes explaining why changing behaviour here could work to smooth out demand on the national grid, meaning the oldest, dirtiest power stations don’t need to be powered up just so everyone can have a cup of tea at the same time.

  • because a good pun is its own reword.

This builds on ideas and hacks developed by Nick O'Leary at IBM, Tom Dyson at Torchbox Joe Short from Demand Logic, and presented at the last Homecamp. I really wish I was going, but I have a day of letter writing ahead of me instead for Saturday.

Which after this weekend, will be the subject of another post.