Start and Stop All Your Mongrel_Cluster Processes

This has been updated. You can find the update here

So, Its possible to use mongrel cluster in a shared environment. I've shown that. But what about stopping and starting all these processes? No one wants to cap restart in every source directory after a server gets rebooted. And what if I need to shut them all down at once!? I can't be ghetto and sudo killall mongrel_rails, I've got self respect.

So to solve my little problem, I took the kick in the butt from Peter Cooper and finally created an init script for all the mongrel_clusters on a shared box.

The beauty of this is that when I add a new app to the server, I don't have to tell the script. Using conventions, it knows what rails apps I have installed.

Basically, when you call start, it iterates through the APP_DIR directory. When it finds a rails app with mongrel_cluster.conf in its config directory, it starts it, but not before it verifies that there isn't a pid under /log/. If it finds a pid, it just reports back to you that the app has already been started.

Calling stop does the inverse, with the same protections. Restart just does a stop then a start. If you've got a stuck process or some other wackyness going on, force_restart will skip past the checks and force restart your rails apps.

Huzzah! Another virtue of using conventions.

Grab the script here: svn export http://code.jtoe.net/rails/scripts/mongrel_processes.rb

Put it anywhere, chmod it, and symlink it to init.d

svn export http://code.jtoe.net/rails/scripts/mongrel_processes.rb
chmod 755 mongrel_processes.rb
sudo ln -s /home/dev/bin/mongrel_processes.rb /etc/init.d/mongrel_processes

If you're on (ubuntu|debian), add it to your start up processes like this:

sudo update-rc.d -f mongrel_processes defaults

And you're off...err automatically on...just reboot and try it out.

Popularity: 28% [?]

You can skip to the end and leave a response. Pinging is currently not allowed.

21 Thoughts

  1. Nice one mate - simple and clean :) Will make sure to link this when we get around to documenting our process with Flow and future apps.

    Kee

  2. Thanks - very handy. Linked in our forums.

  3. And what is the user mongrel runs in ? I hope it's not root...

  4. On (at least) RedHat-base distributions, the definition of the APP_DIR variable belongs to a file in /etc/sysconfig (/etc/sysconfig/mongrel_apps for example). Of course, this file has to be source in the script...

    Eric-Olivier Lamey -
  5. If Mongrel doesn't get a chance to shut itself down properly (say, if your server gets physically power cycled), will the script start Mongrel on boot? Or will it see a stale PID and assume the mongrel process is running when its not?

  6. You can use the force_restart command on it. It skips the pid checks and does a stop & start.

    But that WOULD have to happen manually, you're right. I'll have to look into that.

  7. Since mongrel_cluster already gracefully handles the already running / not running conditions, I simply removed that functionality from the script - this way cluster::start is always run on startup.

  8. For me the script still won't start up all mongrel clusters, if the pid files exist right after boot up. This happens i.e. after a restore of my backup.

    Why not change the start method to something like this and let mongrel_rails take care of the existing pid file:

    def start(app)
    if is_started?(app) == true
    puts "pid file exists. #{app} could be running, trying to start anyway:"
    else
    puts "starting #{app}"
    end
    `mongrel_rails cluster::start -c #{File.join(APP_DIR, app, "current", "config", "mongrel_cluster.yml")}`
    end

  9. Is the server down?
    svn: could not connect to server (http://code.jtoe.net)

  10. You will have problems running a non-Bourne init script on a Solaris system.

    Regardless of the #! line. Solaris will use the /sbin/sh
    as the interpreter...

    see /sbin/rc3:
    if [ -d /etc/rc3.d ]; then
    for f in /etc/rc3.d/S*; do
    if [ -s $f ]; then
    case $f in
    *.sh) . $f ;;
    *) /sbin/sh $f start ;;
    esac
    fi
    done
    fi

  11. Make sure you understand the boot process before you install this. I installed the script with update-rc.d mongrel_processes defaults, and one of my rails apps failed to start on boot because mysql wasn't up yet when it started.

    "sudo update-rc.d mongrel_processes start 30 2 3 4 5 . stop 70 0 1 6 ." should do the trick.

  12. for force_restart(app)
    I add "stop app" before wipe_cluster(app) takes action.
    for start(app)
    I add wipe_cluster(app) at the beginning to deal with pid files problem on my server.

  13. You're mixing Singular and Plural namings - and sometimes the .rb ending seems to be missing - you might want to fix it in your write up - in case I'm right ...

    Thanx, great work of yours!

  14. hello nice work friend ,

    i deadly needed motivation in automising my apps, & i got here

    chetanM

  15. High definition porno movies
    here
    http://premonsw545.worddatingguide.info

  16. High definition porno movies
    here
    http://webtechnologiesnew.blogspot.com

  17. Oh, cool, I found this, cause now you can keep mongrel config in you app devel tree (svn-ed and all that). Very nice.

    • Is there a recipe you use that takes care of cleverly restarting only the newly deployed
      app's cluster?
      or maybe palmtree (for cap 2.X) does exactly that...
    • is there a way to somehow nicely factor out this APP_DIR thing?
      thanks
  18. [...] well it never had a version number before, but I thought I'd add one [...]

  19. hi...

    Agree...

  20. Using the above script will run mongrel as root which is NOT GOOD. You have to add:

    "su #{USER} -c mongrel_rails cluster::start etc"

Share Your Thoughts?

Please excuse my captcha. But the internets require it these days. Comment triage isn’t billable.