Archive for the ‘Nimboo’ Category

More Capistrano 2 goodies: A Radiant recipe library

This is a followup to “A Couple of Capistrano 2 Recipes Libraries”:http://tempe.st/2007/09/a-couple-of-capistrano-2-recipes-libraries

It’s official: I am a Capistranoholist, and I can’t deploy any Rails application without using Capistrano anymore. A few days ago I had to setup a Radiant site for a client and I couldn’t resist writing a small capistrano recipe library (is there an official name for this kind of collections?) with callbacks dedicated to radiant and tasks that help managing radiant installations.

As usual you can get them from the recipes repository (I use an svn:external in all my projects, linking the recipes to lib/recipes).

After you get the recipes load them from Capfile:

load 'deploy' if respond_to?(:namespace) # cap2 differentiator
load 'config/deploy'
load 'lib/recipes/medlar'
load 'lib/recipes/radiant'

Now you will have one more callback and an overridden deploy:cold task:

  after "deploy:migrate", "deploy:radiant:migrate:extensions"
 
  desc "Overridden deploy:cold for Radiant."
  task :cold do
    update
    "radiant:bootstrap"
    start
  end

The overridden task bootstraps radiant during deploy:cold (but assumes you use it only the first time you deploy!), and the callback migrates radiant extensions whenever you migrate your db.

If you don’t need the radiant recipes but you are using the medlar namespace I suggest you update from svn, there have been a lot of fixes to the recipes.

A couple of capistrano 2 recipes libraries

I kept playing with Capistrano 2 after my last article, and I’ve refactored quite a bit my recipes, finally moving them in their own subversion repository. This allows much quicker deployment with my new rails applications. Here’s how I do it:

$ rails my_new_application
$ cd my_new_application
$ capify .

Then I edit Capfile:

load 'deploy' if respond_to?(:namespace) # cap2 differentiator
 
load 'lib/recipes/site5' # This is my site5 recipe
load 'lib/recipes/medlar' # The general use recipes
 
load 'config/deploy'

The site5 and medlar namespaces hold default configuration values, define some callbacks and the following tasks:

cap deploy:medlar:rails:freezer:edge   # Fetch Rails edge and puts it into sh...
cap deploy:medlar:rails:freezer:stable # Fetch Rails stable and puts it into ...
cap deploy:medlar:rails:link           # Links Rails to application/vendor
cap deploy:medlar:rails:update         # Updates the fetched version of rails.
	

cap deploy:site5:kill_dispatch_fcgi # Kills Ruby instances on Site5
cap deploy:site5:link_public_html # Links public_html to current_path/pu...

Last but not least, here’s the simple, clean and elegant deploy.rb:

set :application, "my_new_application"
set :user, "the_username"
 
set :repository,  "repo_address"
set :deploy_to, "/home/#{user}/apps/#{application}"
 
role :app, "server.com"
role :web, "server.com"
role :db,  "server.com", :primary => true

Quite readable, isn’t it? :)

The recipes are available via anonymous subversion: https://svn1.hosted-projects.com/medlar/recipes/

Enjoy and let me know if you found them useful.

The day after TechTalk Italia

On monday I came back from Techtalk Italia and it was such a wonderful experience that I had to wait a day before posting. It was relaxing, inspiring and fun.

We had four sessions where we talked about our projects and the problems Italian enterpreneus encounter during their work, and between these sessions we had great food and great fun.

I met cool people I didn’t know and met with people I now consider friends. Luca Conti, Federico Feroldi, Gioxx and Martin Varsavsky have posted about the event (but Martin didn’t attend).

There is also a flickr set about the talk.

Techtalk Italia

Good friend, fellow nimbooer, wikio engineer and all around cool guy Lorenzo Viscanti organized a meeting that will take place next weekend (the day after tomorrow) in the beatiful Tuscany.

Some of the most brilliant Italian people in the web business will attend to exchange ideas, relax, show each other their work, and to have fun.

Among the others there will be Marco Palazzo (Duespaghi), Davide Lombardi (zooppa), Daniel Ruzzini (domainsbot), Stefano Vitta (bloggers.it and fon), Guido Bellomo (Babelgum), Gioxx (Mozilla.it), Kiaroscuro and Federico Feroldi (nimboo), Lele Dainesi (cisco) and Luca Conti (Pandemia).

If you’re interested in attending the meeting you can contact Lorenzo. There will also be some video posted soon after the event.

Why we need to stop designing and start delivering

Lately I’ve been very unproductive. Lost in business discussions and dreaming about the next big thing, I couldn’t finish two paid projects, and that sucks™.

The invisible shackles that didn’t allow me to finish those jobs have been holding me back so that I wasn’t able to do any real work even on personal projects.

I’ve tried finding the cause for this, blaming my lack of self discipline or the fact that I was not doing GTD as strictly as I was six months ago, but I knew there had to be something else, and the whole situation was getting on my nerves.

Last week I bought a game for the Nintendo Wii, Mario Strikers Charged, and while playing I remembered a pet project I started more than an year ago and never completed.

The project name was ICCFriends, a repository of Nintendo Friend Codes for the usenet it.comp.console community.

This time I had to finish it, so I went and looked through my code: awful. There was as much BDUF in there as in Windows Vista, and while wandering through the models and controllers, suddenly I got it!

I knew why I wasn’t delivering: I was designing things that were not useful. I lost so much time preparing for refactoring, or moving code around or laying the ground for future extractions or features that when the time came to implement the real thing I grew tired or got bored.

I then did the only thing I could possibly do to complete ICCFriends:

$ rm -rf ~/src/iccfriends
$ rails ~/src/iccfriends

I then focused on the simplest and shortest set of features I could imagine and in a couple of hours ICCFriends.net was up and running, and with more interesting features than those I was bduffing.

It looks rushed out because it is, but once again I was able to think and deliver and the rush of energies that came with it is a nice feeling I really missed.

Yesterday I learnt an important lesson, and from now on I will always strip my feature list before even start thinking about the implementation.

Transaction in Rails

In an application I wrote for a client I needed transactions to handle a batch import of records from a legacy table into different models.

I browsed the documentation, googled for info and even asked in #rubyonrails, but wasn’t able to get any help, so I had to resort to good old trial and error.

First I tried nested transactions (the old way):

FirstModel.transaction do 
  SecondModel.transaction do
    ThirdModel.transaction do
      FourthModel.transaction do
        fourth.do_stuff
        third.save
        second.handle_your_stuff
        first.good_old_foo_bar
      end
    end
  end
end

I had a couple of problems with this code:

  • Ugly
  • Deprecated

It also didn’t seem to work for me, so I banged my head against my laptop for half a hour or so and suddenly the rails documentation started to make sense, so I wrote this code instead:

transaction do
  first.save!
  second.save!
  third.save!
  fourth.save!
end

The reason I used save! was that the documentation says that a transaction block catches exceptions. Unfortunately that’s not true and to make it work I had to remove the exclamation marks.

Now I had another problem. I included the transaction code in a class I put in lib/, ImportJob, and I was using it with script/runner:

script/runner 'ImportJob.run' -e ENVIRONMENT

All of a sudden I started getting method missing errors on transaction. Now I could investigate and do the right thing or just hack a solution – I decided to hack a solution :)

class ImportJob < ActiveRecord::Migration
end

Deriving ImportJob from Migration solved my problems. Now if anyone has a cleaner solution I will be happy to implement it but at least I solved my problem with transactions (and I hope this will be helpful for someone else too).

Update: Tim pointed out some flaws in my code and some more testing revealed that I needed to have an exception to trigger a Rollback, so I modified the block:

begin
  transaction do
    first.save!
    second.save!
    third.save!
    fourth.save!
  end
rescue ActiveRecord::RecordInvalid => invalid
  # do whatever you wish to warn the user, or log something
end

This works fine, thanks Tim!

The results of a micro iteration: Planet Nimboo

Following the announcement on bard’s blog, I would like to show you the result of the micro iteration I wrote about a couple of days ago: Planet Nimboo.

We use it to aggregate our blogs, and it’s quite simple, it’s only a ruby script with an erb template (how long since you last saw .rhtml files without rails? :) ), and bard has also cleaned up the code and made it available so you can play with it and make it better:

Code: script, template

Micro Iterations: How to boost your productivity

I have found a way to boost my productivity, and I can say without doubt it works very well :)

Like most of you I have dozens of pet projects that can be done very quickly but require focused time, and time is the scarcest resource of them all, so together with my friend bard, and the other nimboo guys we decided to dedicate a hour, two or three times a week to work together on some project, with the condition that we have to choose something that can be done in a hour, no more.

The results have been staggering and yesterday we met together to work on planet.nimboo.net, an aggregator for the nimbooers’ blogs. In half a hour planet was up and running :)

If you have a couple of friends to do this kind of exercises together please try creating some small product together, you’ll be happy you did.