class Monstar

June 5, 2009 by pedro mota

I recently had to move from using FastCGI on Camping Ruby framework, to sweet Mongrel because of getting too much 500 HTTP errors under some load tests. Yes irc::zimbatm, me too.

When on FastCGI behing Lighttpd, upon each request, the code is loaded so after changing its source, it gets updated.

Now, under development, Mongrel(s), behind the same Lighttpd frontend, need to be restarted upon source code changes in the models, views, controllers or helpers (yet not CSS files, since they are taken care by lighty). Instead of moving to the xterm, kill the process and restart it i decided to automate it. Since i was disconnected from the internet, I’ve done this script to detect file changes from a pre-specified array of files, monitored under a certain interval in seconds, with an action to start, kill, and restart an app (in my case, a Mongreled Camping web app).

  • Problem: Threads
  • Solution: Process::fork

The app load (in this case I load/execute ruby scripts) is done via Process::fork. A new process starts up each time this method is fired up upon a, 'r').ctime change:

def load_app
  @pid = Process.fork { load(@app) }

Before loading up a new updated instance of the app, the previous one must be terminated. Threads behaviour was problematic because of the thread tree termination in time. This is well done, in a safe mode, with Process::kill(signal, pid) and Process::wait(pid) that waits the pid process termination, essential in the case of Mongrel server.

def kill_app
  Process.kill(\"KILL\", @pid)

As one can see in this screenshot the main app process holds on while the child, in this case our Mongrel’ed Camping web app, is terminated and created a new one. Verbose mode to assert its behaviour. Suits this specific need.

This is very usefull to make tests (Test::Unit:TestCase) run automatically upon source changes (will get to an Autotest clone).

Changing from




one can get any command be executed. ftp, cp, etc…

The script is here do download under a Ruby License.


 $ monstar [options]\n-a, --app SCRIPT.RB,PARAMS,...\n-i, --interval VAL\n-f, --files FILE,FILE1,...\n-h, --help

© 2018 | Follow on Twitter | pedro mg & Hugo