Keeping Rails running at Dreamhost Part 2

Update 2/2/07: Per Thomas’ comment I’ve released the code below as the dreamhost rails plugin.
Update 1/25/07: People have reported difficulties copy and pasting the dispatch.fcgi source code from this blog post so here is a dispatch.fcgi to download. Make sure you make it executable!

My first attempt to keep my Ruby on Rails sites running at Dreamhost involved modifying dispatch.fcgi to implement the frao_handler approach as described on the wiki. This left me with a 3% 500 error rate. Ughh!

My second attempt involved gracefully restarting dispatch.fcgi’s every hour or two to avoid the wrath of the Dreamhost process monitor. This reduced the 500 error rate down to 0.2% which was still too high!

I’ve finally gotten my 500 error rate down to 0 by making some minor changes to the signal handling code of dispatch.fcgi. If the dispatch.fcgi process is in the midst of handling a request I defer letting it be killed until the request is complete. I did this by installing a custom TERM signal handler that protects the dispatch.fcgi process while a request is being processed. This solution works for my Rails 1.1 and Rails 1.2 (previously Edge) based sites. Here’s my complete dispatch.fcgi:


require File.dirname(__FILE__) + "/../config/environment"
require 'fcgi_handler'

class RailsFCGIHandler
   def busy_exit_handler(signal)
     dispatcher_log :info, "busy: asked to terminate during request signal #{signal}, deferring!"
     @when_ready = :exit

   # Dreamhost sends the term signal and if we're handling a request defer it
   def term_process_request(cgi)
   rescue Exception => e  # errors from CGI dispatch
     raise if SignalException === e
     install_signal_handler('TERM', method(:exit_now_handler).to_proc)
   alias_method :process_request, :term_process_request

This entry was posted in Linux, Ruby, Ruby on Rails, Systems Administration, Web. Bookmark the permalink.

10 Responses to Keeping Rails running at Dreamhost Part 2

  1. Todd Huss says:

    Hi folks, I’ve posted a downloadable version at since the formatted source in the blog post doesn’t seem to copy and paste very well. Let me know how it works!

  2. jcalanog says:

    Hi Todd,

    Thanks for the link.

    Not sure what has happened, but it seems that by obtaining a copy of
    this dispatch.fcgi, my site now works (for now).

  3. Hi Todd,

    yes my site is working…. ( First I tried the frao_handler approach. This also works for me (I’m getting then almost none 500 messages anymore but occasionaly, one slips through). I did modify the dispatch.fcgi as explained in your article. Next I killed all running dispatch.fcgi tasks. My problem then is that with the new dispatch.fcgi, my site is no longer starting up. The only solution I found was to set the old dispatch.fcgi (the original or the frao) back and then everything runs again. I also checked the chmod so that’s not the issue. I’m sure I’m doing something wrong but I can’t figure out what…. Can I start the dispatch.fcgi by hand or is there a log where I can see if dispatch.fcgi has a problem starting up? I can’t find any clues in the logs of mywebsite/log/

  4. Pingback: Randquist Rants » Blog Archive » Dreamhost and Ruby on Rails

  5. wayann says:

    Just wanted to thank you! your mod saved my life as a RoR newbie! 😉

  6. Any plans to release this as a plugin?

  7. Koen Eijsvogels says:


    It finaly got your code working. Thanks! It works perfectly.
    I do have a question: how do you measure the percentage of 500 errors? Do you use Dreamhost stats or are you using a different source to calculate this figure?

  8. Pingback: La 57ième boule de cristal » Archives du Blog » le 57ième déploiement capistrano sur Dreamhost

  9. phiras says:

    Great job man, I spend 3 weeks trying to setup my project.
    Thank you indeed 😀

  10. frank says:

    it helped me to get redmine up and running on a sarge-based system with fastcgi. thanks.

Comments are closed.