Skip to content

Instantly share code, notes, and snippets.

@telent
Created June 21, 2010 13:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save telent/446833 to your computer and use it in GitHub Desktop.
Save telent/446833 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
# barebones reload-stuff-automatically script for sinatra on linux. In fact
# it doesn't actually restart, because that was causing me problems with socket
# reuse errors. Instead, in the spirit of Unix do-only-one-thing-and-do-it-well,
# it exits and lets the caller restart. You will need to adapt it to start your
# application, because it's currently hardcoded to start mine.
# The name "crossbow" is because it would lose in a fight with a shotgun. But it's
# not as loud.
# I invoke it with "bundle exec ruby bin/sinatra-crossbow.rb"
# Requires inotify support
require 'rb-inotify'
require "rack"
require "sequel"
$:.unshift "lib"
require "rticulate-env"
DB=Sequel.connect(Env['database'])
require "rticulate"
warn "#{$0} (pid=#{Process.pid})"
mongrel=nil
mongrel_thread=
Thread.new do
Rack::Handler::Mongrel.run(Rticulate.rack_app) do |s|
mongrel=s
end
end
notifier=INotify::Notifier.new
notifier.watch(".",:close_write,:modify,:move,
:create,:delete,:recursive) do |e|
unless(/^\.#/.match(e.name)) then
puts "Filesystem event: #{e.name} #{e.flags.join(",")}. Reloading"
# this is probably belt+braces, but this combination works in both
# 1.8 and 1.9
mongrel.stop if mongrel
mongrel_thread.kill
mongrel_thread.join
exit
end
end
# <mongrel> is assigned by another thread, give it
# time to happen before we start the notifier
until mongrel
sleep 0.1
warn mongrel_thread.inspect
end
puts "Ready and waiting"
# notifier.run appears to stop the web server thread from doing anything.
# I hypothesise that it blocks in a way that the ruby 1.8 scheduler
# can't cope with. But, easy workaround
array=[notifier.to_io]
while IO.select(array,array,array)
notifier.process
end
@telent
Copy link
Author

telent commented Jun 21, 2010

The comment on how I start it is a lie. Actually it's more like

$ while bundle exec ruby bin/sinatra-crossbow ; do true ;done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment