Monday, February 18, 2008

A Couple Of Quick Mongrel Tips

timeout mongrel connections

There are two time based arguments for your pleasure:

-o, --timeout X - Time to wait before killing a stalled thread
-t, --throttle X - Time to pause between accepting clients

Both conveniently begin with the letter 't'. Mistaking -t for the timeout can lead to some interesting behavior like for example causing your site to not accept new clients for a bit.

If you're looking to timeout your connections after X amount of time you can use -o X, where X is the time to wait in seconds:

mongrel_rails start -d -e production -o 30 -p 5555 -P mongrel.pid


stopping a mongrel

Mongrels dont always like to stop when you tell them to stop, they will just sit there happily forever not doing anything productive:

% mongrel_rails stop -P mongrel.pid
Sending TERM to Mongrel at PID 25073...Done.
% ps aux | grep '25073'
25073 Feb17 5:51 mongrel_rails [5555/0/8288]: idle
But I said stop! :(

You can use --force to send a kill -9, but this isn't too graceful. You want to allow any queued clients to exit before doing this.

To deal with this you can also provide a wait argument which attempt to do a graceful stop, but if after X amount of time it can't, it will bring out the BFG and kill -9 the process:

mongrel_rails stop --force -w 30 -P mongrel.pid


monitoring a mongrel

If you're planning to restart mongrel processes using God by checking the http status code, make sure you check for more than N consecutive failures as you can(and will) get random timeouts/refused connections.

restart.condition(:http_response_code) do |c|
c.timeout = 15.seconds
c.code_is_not = 200
c.times = [3, 3]
end
This for example, will check for 3 consecutive http response failures before restarting.