Thursday, 5 July 2007

Deploy Rails on Windows: Part 3

This is the last part of a series of posts on deploying Rails on a windows server using Oracle, Mongrel & Pen. My thinking: get it working with a simple app first, to minimise headaches later. The other parts can be found here:
  1. Installing Rails
  2. My-first-app with Oracle
  3. Pen of pesky Mongrels
This part heavily references a talk given by Brian Hogan at RailsConf

Part 3: Pen of pesky Mongrels
Before you go any further, make sure your app works in production mode!

Install Mongrel

The easiest way to get started with Mongrel is to install it via RubyGems:

gem install mongrel

Select the highest stable version number marked (mswin32).

Run Mongrel in the background with:

cd myrailsapp
mongrel_rails start -d

and stop it with:

mongrel_rails stop

There’s quite a few options you can set for the start command. Use the mongrel_rails start -h to see them all.

Mongrel as a Service

Install the gem (pick the most recent one):

gem install win32-service
gem install mongrel_service

Install your app as a service:

mongrel_rails service::install –N comics_4001 -c c:\my\path\to\myapp –p 4001 –e production

Your app will then appear under ‘services’ in Computer Management (accessed via Control Panel or by right clicking on My Computer)

You can then set the service to start automagically. Right click on ‘comics_4001’, choose ‘properties’ and change the startup type to ‘automatic’.

This is really all you need for a small application – perhaps an internal company app or something.

  • You don’t get page caching
  • Doesn’t scale
  • Rails is single threaded so user requests get queued up.
While rails is serving an action to one user, all the other users need to wait. This is bad if some of your actions take a while, which is why we need Pen...

Load Balancing Mongrels with Pen

Pen requires cygwin1.dll, so make sure it’s in the same directory as pen.exe. Also, make you get the one with the ‘a’ suffix – the other version doesn’t work for some reason.

To run pen as a service you need the Windows Server Resource Kit Tools. Install it somewhere. To keep things simple I'll use c:\reskit

Start a couple of mongrels:

mongrel_rails service::install –N comics_4001 -c c:\my\path\to\myapp –p 4001 –e production
mongrel_rails service::install –N comics_4002 -c c:\my\path\to\myapp –p 4002 –e production

You could write a batch file to start and stop these multiple servers.

The following command will balance your mongrels just fine:

pen 4000 localhost:4001 localhost:4002

But to do things properly, keep reading.

Pen as a Service

Install pen as a service using srvany.exe from the reskit:

c:\reskit\instsrv Pen c:\reskit\srvany.exe
>> The service was successfuly added!

Set some parameters with regedit. Go to 'HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Pen' and add a new key called 'Parameters'. Add the following three String values to the parameters key:
  • Application = pen.exe
  • AppParameters = -f 4000 localhost:4001 localhost:4002
  • AppDirectory = c:\pen
Start the pen service from ‘Services’ or with the command:

net start pen

Remove with:

sc delete pen

Pen will then chug along happily and is suitable for most apps with a few users. You still don’t get page caching, for that you need Apache, but this solution is free, simple and performs moderately well.


Taras Matyashovsky said...

Thanks Barry for the post.
I found it very useful especially concerning pen configuration.

Barry said...

No problem Taras. Thanks for the feedback!

Term Papers said...

Thanks for posting this, was exactly what I was looking for, thanks.

Term Papers said...

I am hoping the same best work from you in the future as well.