Torquebox: An All-In-One Java/JBoss Powered Ruby Webapp Platform

torquebox2In the past two years we’ve seen a number of changes in the world of Ruby webapp deployment, but have you heard of Torquebox? Built upon the Red Hat Inc. JBoss middleware, Torquebox is an enterprise-grade application server that provides scale-oriented services to your Ruby webapps, including turn-key clustering. With its latest release, Torquebox supports all Rack-based Ruby frameworks.

Torquebox comes with job scheduling and asynchronous task scheduling out of the box (no extra installs necessary), and while I’ve grown fond of RabbitMQ, the ease of using the built-in JMS (Java Message Service) messaging is appealing, particularly if you’re likely to deploy within a Java dominant environment. If you are having trouble convincing management to let you use Ruby, Torquebox is the most enterprise-oriented platform I’ve seen to date. Not just that, it’s open-source and licensed under the LGPLv3. Their community site boasts that JBoss (upon which Torquebox is built) is “the world’s number one Java application server, and it was created by the community.”

Torquebox 1.0.0.Beta18 was released within a day’s time from the latest JRuby 1.4 release and you can download Torquebox from its download page, or build it yourself from source easily obtained from GitHub. If you are using Mac OS X, the following code sample will get you up and running with a simple cut and paste:

wget http://repository.torquebox.org/maven2/releases/org/torquebox/torquebox-bin/1.0.0.Beta18/torquebox-bin-1.0.0.Beta18.zip
unzip torquebox-bin-1.0.0.Beta18.zip
mv torquebox-1.0.0.Beta18-bin torquebox
export TORQUEBOX_HOME=~/torquebox
export JBOSS_HOME=$TORQUEBOX_HOME/jboss
export JRUBY_HOME=$TORQUEBOX_HOME/jruby
PATH=$JRUBY_HOME/bin:$PATH
sudo gem install jruby-openssl
sudo gem install activerecord-jdbcsqlite3-adapter
cd ~/torquebox/share/rails
rails -m template.rb ~/torquebox_rails_app
cd ~/torquebox_rails_app
rake rails:freeze:gems
emacs config/database.yml

# (now prefix sqlite3 with jdbc to become "jdbcsqlite3")

jruby -S rake gems:install
jruby -S rake db:migrate
jruby -S rake torquebox:rails:deploy
jruby -S rake torquebox:server:run

While waiting for JBoss to unfurl, you can close your eyes and do a mental walk-through of installing Erlang, a RabbitMQ daemon, cron, HAProxy, etc. – because you won’t have to do that with Torquebox – and when you open your eyes approximately two minutes later, you should be able to see the default Rails page at http://localhost:8080. While everything appears the same as if you were running on it on a Mongrel, you’re really experiencing Rails as it’s presented from within a JBoss AS appserver, more about that in a minute.

Handily, Torquebox contains tried-and-true JDBC drivers for a number of databases, including Derby, H2, HSQLDB, MySQL, PostgreSQL, and of course, SQLite3. Additionally, Torquebox is Capistrano friendly – so you probably won’t need to learn any new tricks to deploy – and it also has built-in support for cryptographic key-storage.

With an application appropriately bundled into a WAR file, you can utilize Torquebox’s farming to “deploy your app to one node of the cluster, and to have that node farm it out to all of its peers.” Torquebox uses httpd+mod_cluster out in front, so it’s “intelligently aware” of your JBoss cluster, and thus, if you have a two-node cluster running, and you start two more JBoss instances on the same subdomain, they’ll auto-join the cluster and then the existing nodes will auto-farm the app to them. You’re app now has twice the resources and didn’t need to be restarted, which in and of itself, is pretty huge.

So, where does Torquebox fit in? One can make the argument that it fits “everywhere.” The JVM comes pre-installed on Macs, is easy enough to add to Linux or a Windows desktop, and of course deploys to a number of hosts. Scaling the JVM isn’t new, and finding the people to do so is probably easier than finding dedicated Ruby programmers. I would be remiss not to point out that JBoss is hungry for RAM, and I realized quickly that Torquebox wanted more cowbell than my Linode 720 could afford. Engine Yard Cloud greatly simplifies deployment to EC2 instances (via Chef), and the smallest instance provides a whopping 1.7GB RAM, so scaling JRuby through the company that houses the JRuby core commiters just makes sense to me. If, however, you feel better qualified to deploy Torquebox yourself, you might look at JBoss Cloud.

I recommend taking 10 minutes to watch the pair (1, 2) of screencasts by Bob McWhirter, which demonstrate all of what I’ve highlighted here in well under a half an hour.

caliper-logo.png[ad] Find duplication, code smells, complex code and more in your Ruby code with Caliper! The metrics are free and setup takes just one click. Get started!