Mr. Sparkle - The Magical Reloading Rack Server
This gem contains a script to start a Unicorn-based server for your Rack application that reloads your application code automatically when its files are changed, but doesn't incur the penalty of reloading all the gem dependencies. It's based on Jonathan D. Stott's blog post "Magical Reloading Sparkles"--hence the name. (The name is also a Simpsons reference. CAN YOU SEE THAT I AM SERIOUS?)
The main purpose of this gem is to take Jonathan's idea and package it into something can "just work" without having to be customized inside each project's code. Besides the gem packaging, this code differs from the original watcher script in the following ways:
- It uses listen instead of directory_watcher, which provides native event-driven file change detection, instead of polling.
- It assumes you're using Bundler for dependencies, which means that instead of needing a hardcoded list of gems in the
before_forkhook, like the blog post had, this plugin just does
Bundler.require(:default, ENV['RACK_ENV'])to get all the modules mentioned in the Gemfile loaded before forking. (Unicorn will default the RACK_ENV variable to development if it isn't set already, so by default you'll get all the gems in the
- If you change your Gemfile, the preloads are no longer valid, so this script treats that change as a special case: when the Gemfile changes, we kill the whole server and restart it, thus reloading absolutely everything.
The script comes with a default set of file extensions it will watch for changes. I've tried to be liberal about it--no harm reloading a few extra times when developing. You can run
mr-sparkle --help to see the default set as a regexp, and you can change that regexp with the
$ gem install mr-sparkle
If you've got a Rack app that uses Bundler for its dependencies, then ordinarily all you have to do is execute
in your project's root directory. You'll get a server listening on port 8080 (bound to all addresses, so external machines WILL be able to connect to it by default) and your code will be reloaded if any relevant files change.
You can use command-line options to change the behavior a bit as follows:
$ mr-sparkle [--pattern regex] [--full-reload-pattern regex] [--force-polling] [-- [unicorn options]]
--pattern to replace the default regex that files must match to trigger a reload. I've tried to make the default fairly liberal--it includes all extensions registered with tilt, for instance--so for most apps it will probably work fine.
--full-reload-pattern to trigger a full reload for a different set of files. By default it only does this for
--force-polling to use polling, rather than using file system events. This is useful if you have mounted the files over NFS, in which case events don't work. By default this is turned off.
Any arguments after the
-- will be passed on to unicorn. This is how you would change the default port, make it not bind to external ip addresses, use a rackup file with a name other than
config.ru, etc. See the unicorn documentation for exactly what you can pass here. Do not pass the
-c option to unicorn--
mr-sparkle comes with its own unicorn config file that it will use automatically.
This script requires Ruby 1.9.3 or greater. Since it's a wrapper around Unicorn, it will only work on "unix or unix-like" systems where unicorn is supported.
- Fork it
- Create your feature branch (
git checkout -b my-new-feature)
- Commit your changes (
git commit -am 'Add some feature')
- Push to the branch (
git push origin my-new-feature)
- Create new Pull Request