Easy code reloading with FSSM

After a several month hiatus (filled with such activities as getting married and honeymooning), I'm finally getting back into the swing of "normal" life.

I've been working on a pet project using Sinatra and Unicorn (doing some long-polling stuff, so I needed to support multiple connections even in dev). Unfortunately, this combination makes code reloading tricky to tackle, as it rules out shotgun entirely, and seems to also bust Sinatra::Reloader.

After a couple hours of HUPping unicorns manually, I decided that I needed a more robust solution. Some digging led me to the FSSM gem by ttilley. This library will watch your file system for events and runs a registered callback when they occur. A few minutes of hacking and my reloading woes are solved!

#!/usr/bin/env ruby -wKU

require 'rubygems'
require 'fssm'

puts "Monitoring #{File.expand_path(File.dirname(__FILE__))} for changes..."
FSSM.monitor(File.dirname(__FILE__), ['**/*.rb', '**/*.ru', '**/*.yml']) do
  update { system "kill -HUP `cat pids/unicorn.pid`" }
end

exit 0
blog comments powered by Disqus