Rerun

http://github.com/alexch/rerun

Rerun launches your program, then watches the filesystem. If a relevant file changes, then it restarts your program.

Rerun works for both long-running processes (e.g. apps) and short-running ones (e.g. tests). So it works like shotgun and autotest (and guard and all the rest).

Rerun's advantage is its simple design. Since it uses standard Unix "SIGINT" and "SIGKILL" signals, you're sure the restarted app is really acting just like it was when you ran it from the command line the first time.

By default only *.rb,js,css,scss,sass,erb,html,haml,ru files are watched. Use the --pattern option if you want to change this.

If you're on Mac OS X, and using the built-in ruby, it uses the built-in facilities for monitoring the filesystem, so CPU use is very light.

Rerun does not work on Windows. Sorry, but you can't do much relaunching without "fork".

Installation:

    gem install rerun

"sudo" may be required on older systems.

If you are using RVM you might want to put this in your global gemset so it's available to all your apps. (There really should be a better way to distinguish gems-as-libraries from gems-as-tools.)

    rvm @global do gem install rerun

Usage:

    rerun [options] [--] cmd

For example, if you're running a Sinatra app whose main file is app.rb:

    rerun ruby app.rb

If the first part of the command is a .rb filename, then ruby is optional, so the above can also be accomplished like this:

    rerun app.rb

Or if you're using Thin to run a Rack app that's configured in config.ru but you want it on port 4000 and in debug mode, and only want to watch the app subdirectory:

    rerun --dir app -- thin start --debug --port=4000 -R config.ru

The -- is to separate rerun options from cmd options. You can also use a quoted string for the command, e.g.

    rerun --dir app "thin start --debug --port=4000 -R config.ru"

Rackup can also be used to launch a Rack server, so let's try that:

    rerun -- rackup --port 4000 config.ru

Want to mimic autotest? Try

    rerun -x rake

or

    rerun -cx rspec

How about regenerating your HTML files after every change to your Erector widgets?

    rerun -x erector --to-html my_site.rb

Use Heroku Cedar? rerun is now compatible with foreman. Run all your Procfile processes locally and restart them all when necessary.

    rerun foreman start

Options:

--dir directory to watch (default = ".")

--pattern glob to match inside directory. This uses the Ruby Dir glob style -- see http://www.ruby-doc.org/core/classes/Dir.html#M002322 for details. By default it watches these files: rb,js,css,scss,sass,erb,html,haml,ru.

--clear (or -c) clear the screen before each run

--exit (or -x) expect the program to exit. With this option, rerun checks the return value; without it, rerun checks that the launched process is still running.

Also --version and --help, naturally.

Growl Notifications

If you have growlnotify available on the PATH, it sends notifications to growl in addition to the console.

Here's how to install growlnotify:

The Installer package for growlnotify is in the growlnotify folder in the Extras folder on the Growl disk image. Simply open the Installer package and follow the on-screen instructions.

NOTE: Growl recently moved to the App Store. I upgraded, and it still works for me, but I'd love it if someone can confirm that growlnotify is still available in a clean App Store install and works as advertised.

On-The-Fly Commands

While the app is (re)running, you can make things happen by pressing keys:

  • r restart (as if a file had changed)
  • c clear the screen
  • x exit (just like control-C)

To Do:

  • Cooldown (so if a dozen files appear in a burst, say from 'git pull', it only restarts once)
  • If the last element of the command is a .ru file and there's no other command then use rackup
  • Allow arbitrary sets of directories and file types, possibly with "include" and "exclude" sets
  • ".rerun" file to specify options per project or in $HOME.
  • Test on Linux.
  • Merge with Kicker or Watchr or Guard -- maybe by using it as a library and writing a Rerun recipe
  • On OS X, use a C library using growl's developer API http://growl.info/developer/
  • "Failed" icon
  • Get Rails icon working
  • Figure out an algorithm so "-x" is not needed (if possible)

Other projects that do similar things

Why would I use this instead of Shotgun?

Shotgun does a "fork" after the web framework has loaded but before your application is loaded. It then loads your app, processes a single request in the child process, then exits the child process.

Rerun launches the whole app, then when it's time to restart, uses "kill" to shut it down and starts the whole thing up again from scratch.

So rerun takes somewhat longer than Shotgun to restart the app, but does it much less frequently. And once it's running it behaves more normally and consistently with your production app.

Also, Shotgun reloads the app on every request, even if it doesn't need to. This is fine if you're loading a single file, but my web pages all load other files (CSS, JS, media) and that adds up quickly. The developers of shotgun are probably using caching or a front web server so this doesn't affect them too much.

And hey, does Shotgun reload your Worker processes if you're using Foreman and a Procfile? I'm pretty sure it doesn't.

YMMV!

Why would I use this instead of Rack::Reloader?

Rack::Reloader is certifiably beautiful code, and is a very elegant use of Rack's middleware architecture. But because it relies on the LOADED_FEATURES variable, it only reloads .rb files that were 'require'd, not 'load'ed. That leaves out (non-Erector) template files, and also, the way I was doing it, sub-actions (see this thread).

Rack::Reloader also doesn't reload configuration changes or redo other things that happen during app startup. Rerun takes the attitude that if you want to restart an app, you should just restart the whole app. You know?

Why did you write this?

I've been using Sinatra and loving it. In order to simplify their system, the Rat Pack just removed auto-reloading from Sinatra proper. I approve of this: a web application framework should be focused on serving requests, not on munging Ruby ObjectSpace for dev-time convenience. But I still wanted automatic reloading during development. Shotgun wasn't working for me (see above) so I spliced Rerun together out of code from Rspactor, FileSystemWatcher, and Shotgun -- with a heavy amount of refactoring and rewriting.

Credits

Rerun: Alex Chaffee, [email protected], http://github.com/alexch/

Based upon and/or inspired by:

Patches by:

License

Open Source MIT License. See "LICENSE" file.