Getting started with Rails and Nanite

by James Harrison on May 1st, 2009

Woo, time for a purely Ruby/Rails post. I’ve not done one in a while, so here goes.

In EVE Metrics 2.0 I’ve been looking to replace the previous delayed_job system with a more robust and fault-tolerant system. A while ago I attended the Ruby Manor conference, and saw an excellent talk on Nanite by George Palmer. It’s a very neat way of doing background processing- it depends on a mapper, agents and actors. Agents have many actors and announce their presence to the mapper regularly along with (by default) system load info, and the mapper then sends jobs to the least loaded agent which supports a given actor. It also works on AMPQ, and by default the RabbitMQ server, a very high performance queue server written in Erlang. It’s fast enough that you can stream video over it quite well, apparently, and it scales well with large queues, so should fit the bill nicely for EVE Metrics.

Getting started, however, can be kinda tricky. So here’s the simple ‘getting started’ that is sadly absent from the Nanite documentation.

First, we need RabbitMQ. So, install Erlang and RabbitMQ- if you’re on Debian/Ubuntu then Erlang is in the default repositories (apt-get install erlang), and while RabbitMQ is also there I recommend grabbing the most recent version in .deb format from their site. Windows users can find a handy bundle with Erlang and RabbitMQ and a nice readme, so go ahead and sort that out. I’ll wait here.

Now that’s all done, it’s a simple matter of installing Nanite and it’s dependencies. Best way to do this is to clone nanite (git clone git://github.com/ezmobius/nanite.git), cd in there and run ‘rake gem’. Then install the gem it creates with ‘gem install pkg/*.gem’. Dependencies (AMPQ and EventMachine) will get autoinstalled. Note that simply running ‘gem install ezmobius-nanite’ fails to install correctly and does not create documentation.

Not much further, I promise!

Now that we’ve got it all installed we need to tie it into Rails. While googling for this, I tripped over the highly useful and recently created nanite-rails plugin. While it’s not perfect it’s a whole lot better than nothing and takes care of some of the nastier bits of Nanite’s workings. Install it as a plugin with script/plugin, and then generate nanite (‘ruby script/generate nanite’). Voila! Now there’s only one last thing to do, and that’s to add the following line to config/environment.rb:

config.gem "nanite"

And we’re done! There’s a rake task to automagically configure RabbitMQ, and so on, but you should be set up with a nanite initializer to kick off the mapper as well as a file structure and agent generator (nanite in RAILS_ROOT). And from there, it’s back to learning. I’m still getting to grips with the actor/agent side of things and working out how to set that all up. I’ll update this post if I find some more useful info, but this lot was fairly scarcely spread over the ‘net so I figure it’ll be some use to people at any rate. If anyone has any guides on how to work with Nanite that I’m hugely overlooking then let me know, but it’s a shame as far as I can see that Nanite has such sparse documentation on actual real-world usage. Ho hum. Still looks like it could work really well if I can get it working.

Related posts:

3 Comments
  1. Rene Mendoza permalink

    i Downloaded the plugin as: script/plugin install git://github.com/dcu/nanite-rails.git

    then i was getting an error running script/nanite start so i added a require ‘yaml’ line to the top of that file

    great article

  2. David permalink

    nice article!

    I am the author of the plugin, if you have suggestions please let me know: http://github.com/dcu/nanite-rails/issues :)

    btw, I’ve updated the README, I hope it’s more useful now

  3. Great to see the project is still being worked on and the documentation is getting better. Sadly not had much time to work seriously on my projects recently but hopefully I’ll get a few days to get stuck in in a week or two… :)

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS