Mongrel Win32 HOWTO

Mongrel now supports Win32 much better than previous releases thanks to some great people and their hard work. You can now run Mongrel with Ruby on Rails as a windows service, and there are pre-compiled gems available for people to use.

Before reading this document you need to read Getting Started and make sure it works.

Installing Service Support

Mongrel used to have a separate mongrel_rails_service script but this caused problems and has since been unified into just mongrel_rails and a special GemPlugin? that gives you a set of service:: commands.

To install the mongrel_service GemPlugin? you simply install mongrel and then do:

$ gem install mongrel_service

This will give you a set of service commands that you can find out about by just running mongrel_rails and then passing each one the -h option to get help.

Running The Service

After you do the gem install, find a Rails application you want to run and do:

  $ mongrel_rails service::install -N myapp \ 
      -c c:\my\path\to\myapp -p 4000 -e production
  $ mongrel_rails service::start -N myapp

Now hit the port and poof, works (or should).

The application will stop if you use:

$ mongrel_rails service::stop -N myapp

NOTE: Stop reports an exception but does stop the service.

Now the really great thing is that you can just do all this from the Services control panel like your Rails application is a regular Windows service.

Even works in development mode, which is pretty nice. I use win32 at work now and what I have setup is three services: myapp_dev, myapp_stage, myapp_prod. I point dev and stage at the same directory but run dev in development mode and stage in production mode. Then I have myapp_prod in a separate directory and when I'm about to claim I've got something to release I'll go simulate a subversion check-out and run my tests again.

Other Service Commands

There is a full set of service control commands in the mongrel_service plugin. This lets you use either the Services control panel or a command line script to manage your Rails applications. What's also nice is that you can register as many applications as you want, and even the same one with different names.

service::install

If you want to run the same app in different modes then use the -N option to the install command:

 $ mongrel_rails service::install -N myapp_dev \ 
     -c c:\my\path\to\myapp -p 4000 -e development
 $ mongrel_rails service::start -N myapp

You can also use the -D option to give the service a different display name in the Services console.

service::start

Pretty much just takes a service name to start up. It will run and print a message until the service finally starts, which sometimes can take 10-60 seconds.

service::stop

Sort of works right now and also only takes a -N parameter. It has a few errors when it tries to stop a service so we're working on making it cleaner.

NOTE: since mongrel_service 0.3.1, start and stop commands were removed. Use net start / net stop instead.

service::remove

Takes the name (-N) of the service to remove and then removes it from the list. This would be how you'd remove a service so you can change it's start-up options.

CPU Affinity

Mongrel's win32 support actually is able to set the CPU affinity of a running Mongrel service. This is pretty neat since it means if you're running a fancy SMP machine or a dual core that pretends to be SMP, then you can force Mongrel onto one of them and get a nice little boost.

It's pretty easy to use, just pass the -u or --cpu option to the install command and give a CPU of 1-X. That means if you have 4 CPUs and you want Mongrel on #4 then do:

 $ mongrel_rails service::install -N myapp \ 
     -c c:\my\path\to\myapp -p 4000 -e production -u 4

Pretty much the same command, just one more option and you're done.

Making you Service autostart with Windows

By default, the new Mongrel service get installed to be run _manually_, using Mongrel's commands or the Service Manager. You could tweak it to start automatically when Windows start, just use the Service Control commandline tool:

 $ sc config myapp start= auto

Also, you can configure the services that are neede to make it work, like database support:

 $ sc config myapp start= auto dependency= MySql

The space after the equal sign is needed for the command to complete successfully.