Ruby on Rails Shared Hosting Mongrel Issues

April 1, 2010 • Damien White

I have had just about nothing but issues getting my Rails application running on my shared host. I’m not going to mention the company since they have been somewhat helpful, however I’m not thrilled with them. I don’t want to give them any good or bad press without giving them a fair chance. I’ve been with them for around a month now.

Starting back in the beginning of March, I developed a Rails application I wanted to start hosting to play around with the different environments, Capistrano, etc. The more I use Rails, the more I love it. I don’t have the opportunity to use Rails for my day job, but that hasn’t stopped me from doing cool stuff on my own time. I eat this stuff up; I’m a true geek through and through. Anyway, I deployed my app (after some gem snafus) and I was so proud of myself. Little did I know I’d encounter more issues. Bring on the 503 Errors…

Constant 503 Errors

Shortly after I deployed my site, I went back and found it wasn’t running… HTTP 503 error. I went into cPanel to look at my Rails application only to find out it wasn’t running. Off digging in the logs and I turned up nothing. Everything looked to be fine, yet over and over, I would start up the app, wait a while, and it would fail. The issue was something was killing the Mongrel instance. Over the course of emailing support about 10 times a day for a few days, I finally figured out a pattern. I would start up the app (start up the Mongrel process), let the app sit for 1 hour without using it, and bam… FAIL. Long story short, it turns out there was something on the shared server at the firewall level killing off Mongrel. It was like if it saw a process running for more than 1 hour, it would just kill it. This was resolved roughly a week after my initial query. I guess it’s partly my fault for going with cheap hosting, but this was just to get my feet wet. Little did I know it was going to cause headaches, but I guess I’m getting a little more than my feet wet while being thrown into some “real life experience” situations, thereby furthering my education with Rails and Linux. Yes, I’m trying to justify my frustration with education :)

More 503 Agony

This is almost unbelievable, but about a day later I noticed that when I periodically tested the site, I got the dreaded 503 error again. It was the same issue: Mongrel wasn’t running. Back to support with the back and forth emails. This time it wasn’t failing 24 times a day (once an hour) but more like once a day. The pattern here was a bit harder to find. From what it seems (support never came right out and said this) their Apache instance gets restarted about once a day. I noticed that at night my site would be running pretty slow. I would go in an check the server status and I would find the CPUs were pegged. Sometime afterwards, my site would be down again. Again, to be fair I don’t know all the ins and outs of Apache, but it seems from what I’ve read that Apache may need a restart to clear logs and such, but I’m not 100% on that. Remember, I’m a .NET / Windows guy diving head into Rails / *nix now.

My Solution

So I devised a solution that seems to work, albeit somewhat of a hack. I created a cron job that runs every minute to check if my Mongrel instance is up and running. I took some of the code from this blog post, which was very helpful. I changed it around to use my specific settings, changed the logic from restarting a cluster to an instance, and finally dropped the email notification (and loop) since this runs as a cron job that automatically emails me when something happens.

#!/bin/sh

RAILS_DIR=/path/to/your/rails/app

function restart_mongrel() {
    mongrel_rails stop
    sleep 5
    rm -f $RAILS_DIR/log/mongrel.pid
    mongrel_rails start -p 12000 -d -e production -P log/mongrel.pid
    sleep 5
    echo "At `date` the Mongrel instance was restarted."
    NEW_PID=`cat $RAILS_DIR/log/mongrel.pid`
    echo "***** The new pid is $NEW_PID ******"
}

cd $RAILS_DIR

ls $RAILS_DIR/log/mongrel.pid > /dev/null 2>&1
ANY_PID_FILES=$?

if [ $ANY_PID_FILES -ne 0 ]; then
    echo "`date`: oops, found no pid files at all in $RAILS_DIR/log, going for a restart"
    restart_mongrel
fi

# if there are pidfiles, check that their processes are running
for pidfile in $RAILS_DIR/log/mongrel.pid ; do
    # check that pidfile is still here (as in, we remove them all deeper in this for-loop, if mongrel is down)
    if [ ! -f $pidfile ]; then
        echo "skipping missing pid file: $pidfile (can happen after a restart)"
        continue
    fi
    PID=`cat $pidfile`
    ps -p$PID > /dev/null
    PID_CHECK_RESULT=$?
    if [ $PID_CHECK_RESULT -ne 0 ]; then
        echo "`date`: Oops, did not find process for pid $PID in pid file $pidfile, will restart mongrel"
        restart_mongrel
    fi
done

Is this the way it’s supposed to be?

Being this is really my first experience with all this stuff, I’m not sure if I would have better luck with another hosting provider or not. I’d be interested to hear your opinions on the topic. The things I like about my current host are:

  • SSH access
  • Ability to install my own gems
  • Unlimited bandwidth
  • Unlimited domains
  • Price

I think next time I would look for the following:

  • Phusion Passenger / mod_rails  – I believe this works better for shared hosting from what I’ve read. I have been running my own Linux box with Passenger and Apache and everything’s been running great, but my box is in total isolation with just me using it.
  • Git support (maybe even git repository hosting as part of the plan, I’ve seen this on some hosts).
  • More space, I only have 5GB now, which for what I paid isn’t bad, but more is always better, right?
  • MongoDB support, although I believe this is only available on a VPS and not shared hosting.

Posted in ruby on rails and tagged with Rails, Ruby

Damien White

I am a software architect with over 16 years of experience. I simply love coding! I have a driving passion for computers and software development, and a thirst for knowledge that just cannot be quenched. I'm happy to share what I know in my quest to learn as much as possible. I focus most of my time on web development using Ruby on Rails, Ember.js, and ASP.NET MVC.

comments powered by Disqus