Thursday, November 15, 2007

Rails on Swiftiply / Mongrel

Hosting Ruby on Rails is complicated, right? Not necessarily. Let's take a look at a fast and flexible way to serve up your Rails application.

Rails on Swiftiply / Mongrel
If you have done any reading or work with Rails you have probably heard of Mongrel. According to the Mongrel site, "Mongrel is a fast HTTP library and server for Ruby".

You may not have heard of Swiftiply yet. But you will. Well, actually I guess you just did. But you will no doubt be hearing more about it. Swiftiply "is a very fast, narrowly targetted clustering proxy". The Swiftiply website says "scaling your web applications should be easy". Sounds good to me. Let's see if it is!

Prerequisites
We will assume that you have already properly installed and configured the following:
Ruby
RubyGems
Rails


Version Info
Here are the most recent versions tested at the time of this article.  Different versions may cause varying results.
  • OS: Ubuntu 7.10
  • Ruby: 1.8.6
  • RubyGems: 1.0.1
  • Swiftiply: 0.6.1.1
  • Mongrel: 1.1.3
  • Rails: 2.0.2

Installation: It's a breeze.
sudo gem install mongrel --include-dependencies
sudo gem install swiftiply --include-dependencies
I told you it was a breeze.

Configuration: Piece of cake.
Let's go ahead and configure Swiftiply first. Swiftiply does not install a configuration file for you, so you are creating a new blank file in the following step:
sudo vim /etc/swiftiply.conf
Here is the configuration that I used:
cluster_address: 123.45.678.9  # Use your IP here
cluster_port: 80
daemonize: true
epoll: true
epoll_descriptors: 20000
map:
- incoming:
- sample.com
- www.sample.com
outgoing: 127.0.0.1:30000 # Where Mongrels will connect
default: true
docroot: /var/www/sample.com/current # Rails root
cache_directory: public
redeployable: true
cache_extensions:
- htm
- html
- txt
Amazingly that is all that is required in the way of configuration. If you wanted to serve up other applications on other domains or sub-domains, simply repeat the "map:" section for each.

Start up Swiftiply with:
swiftiply -c /etc/swiftiply.conf
With Swiftiply running, now all you need to do is start Mongrel(s) from within the directory of the Rails app you wish to serve. We let the system know that we want to use the special Swiftiply version of Mongrel with the "env SWIFT=1" directive. We also tell them to connect to the IP and port that you defined in your swiftiply.conf, like so:
env SWIFT=1 mongrel_rails start -d -a 127.0.0.1 -p 30000

You may also wish to include the directive "-e production", but I ran out of room on the line! ;)

Now if you want multiple mongrels serving up your app, just start another one up exactly the same. Same IP and port.

And now... your done.
And that is it. You have your basic Swiftiply / Mongrel stack setup.

Keep your eyes on Swiftiply as there will be some exciting new features be adding in the coming weeks and months.

Performance
With a blank Rails 1.2.5 application I show the following memory usages in "top":

Ruby (Mongrel): 47M
Swiftiply: 17M

For some statistics on performance of Swiftiply, please see the post "Testing Various Configurations of Rails, Merb, Swiftiply and Nginx" on Webficient.

Stuff To Know
Here are a couple things to remember about Swiftiply as you are setting it up:
  • Each application you wish to serve will be linked to ONE port.
  • All mongrels serving that application will connect to the exact same ONE port.
  • Even if you have (n) mongrels, they all connect to the exact same ONE port per application.
  • Swiftiply can serve multiple applications, just repeat the part from "map:" down in the swiftiply.conf file assigning your new application a single unique port.
Useful?

Monday, November 12, 2007

Book Review: Pro Active Record

Title: Pro Active Record: Databases with Ruby and Rails.
Authors: Kevin Marshall, Chad Pytel, and Jon Yurek

Visuals:
Font size and layout are good. Easy on the eyes. Large and frequent sub-headings make it easier to locate information.

Audience:
The book lists it's intended "User level" at "Intermediate-Advanced".

Practicality:
It really depends on what you are expecting. I've been using Rails and ActiveRecord for about 2 years, so I should fit into the target audience. After reading the book I think it will be a great reference book to have within arm's reach while working with ActiveRecord. To me it will serve as an API to AR. So it will be practical in that sense.

On the other hand, while reading it I never experienced any "aha!" moments where I felt like I learned something new or exciting, which I had hoped for.

If you are a beginner (never having used AR) it will definitely save you time (and eye strain) hunting down tutorials on blogs.

Overall:
It's a good Active Record reference & usage tutorial(s). I would have appreciated this book even more when I was first starting to use the Ruby on Rails framework. So if you are a beginner, don't let the "Intermediate - Advanced" user level scare you off. Active Record is not a terribly complicated subject, and if you are using Rails (as a beginner or not) you will probably be using Active Record too.

I was kind of surprised when I read the Introduction to the book that it starts off with:

"Is there really enough to talk about in Active Record to fill a whole book?"

"Our answer, then and now, is, "Yes and no""

As an "Intermediate - Advanced" user, that's kind of how I felt at the end of this "Pro" book.

Wednesday, November 7, 2007

Merb & Datamapper: Getting Rolling

Today we want to take a look at Merb and DataMapper.

NOTE: I'm doing this run through on OS X. If you are using a different OS, some of the instructions may need modifications (specifically in regards to path locations). This post also assumes that you are using MySQL, which you should already have installed (including the MySQL gem).

Merb: A Web Framework.
Yesterday marked the 0.4 release of Merb. At this point it should be fairly stable. Stable enough to make it, at the very least, worth a run through. A few months have passed since the original tutorial, and since then Merb has released versions 0.4.1, 0.4.2, 0.5.0, 0.5.1, 0.5.2 and 0.5.3. Since 0.5.3 there has been a large amount of work done in putting together the 0.9.x release. At the time of this update, 0.9 is a "developer" release. I will be updating these instructions soon.

Merb, like Ruby on Rails, is an MVC web framework. The Merb project has goals of being performance oriented and providing flexibility through plugins. This should allow a developer to better mix and match available tool sets to the specific needs of any given project.

One of the areas of flexibility that Merb provides is in the use of an Object Relational Mapper (ORM). You may have heard of, or be familiar with, ActiveRecord which is the ORM provided by default within the Ruby on Rails framework. You can use ActiveRecord with Merb, but you also have other ORM options such as DataMapper and Sequel.

So let's get started.

Installation (outdated - still is 0.4 based)
First off you need to get Merb & DataMapper installed on your machine. Assuming that you already have Ruby and Gems installed on your system, installation is as simple as:

sudo gem install merb --include-dependencies
sudo gem install datamapper
sudo gem install ruby2ruby --include-dependencies
sudo gem install merb_datamapper ??
UPDATE: Merb does depend on the json gem (json_pure if you want to use it on jruby). This "should" be installed when you install "merb --include-dependencies", but some readers have reported that it wasn't. So just to be safe, run:
sudo gem install json
Lastly, we need to make sure DataObjects.rb drivers are installed. You should now be able to install as a gem:
sudo gem install do_mysql
UPDATE (12/11/07): benr75 reports: "The do_mysql gem is currently broken and refers to an absolute path for MySQL that may not exist on users systems. Fix instructions here."

Other possibly useful resources are: post on Blog::Heimidal and the DataMapper Getting Started page.

Assuming everything went smoothly for you, we are now ready to roll.

Generate & Configure Your New Merb Project (updated to 0.9)
Next we will be generating the default framework files.

Change to the directory where you wish to create your Merb project. For me that was:
cd /Users/justin/programming/projects/
Similar to Rails, to generate the framework code we simply run:
merb-gen project_name
cd project_name
As mentioned before we need to configure our Merb application to let it know that we want to use the DataMapper ORM, and setup our database. Pretty simple stuff. Just a few text edits in 2 files.

First, open config/init.rb and find and uncomment the following line as below:
### Uncomment for DataMapper ORM
use_orm :datamapper
The only other file we need to setup is our database configuration file. When you initially generated your project code Merb did not create this file. Why? Because you hadn't yet told it what ORM you wanted to use.

Since we have edited the dependencies.rb file to indicate we will be using the DataMapper ORM, all we now need to do is load the Merb framework and it will auto-generate a sample database configuration file for us. This sample file will be based on the specific requirements of the ORM we have chosen. So from the command line run:
merb
Easy. Merb saw that you wanted to use DataMapper, but no database has been configured. Automatically the database.sample.yml file was then generated. Now let's finish up our configuration of Merb. Open config/database.sample.yml and edit it according to your specific setup. For example mine looks like:
---
# This is a sample database file for the DataMapper ORM
:development: &defaults
:adapter: mysql
:database: merbtest_development
:username: root
:password:
:host: localhost

:test:
<<: *defaults :database: merbtest_test :production: <<: *defaults :database: merbtest_production
Don't forget to copy or rename the file to database.yml.

Also, the databases that you just configured in database.yml need to actually exist, so if you haven't done so already please take a moment to create them now.

That's It!
With those two simple configuration steps, our bare bones application should now be ready to roll. To start up, from within your application directory type:
merb
By default Merb is setup to run on port 4000 of host 0.0.0.0. So open up your browser and point it to:
http://0.0.0.0:4000/
You should now see the Merb default page before you.

If you wish to adjust the IP address or port number that Merb runs on, that is configurable from the config/merb.yml config/init.rb file.

That's it for this post. In the next post we will actually make our Merb app do something.

Until then you can get some basic ideas of how to wire things together in this post on PaulBarry.com