Skip to content

Instantly share code, notes, and snippets.

@joefiorini
Forked from jonathanpenn/server_restarter
Created February 17, 2009 16:29
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 joefiorini/65820 to your computer and use it in GitHub Desktop.
Save joefiorini/65820 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
#
# usage: script/passenger_restarter
#
# ** Fork of Jonathan Penn's server_restarter to work with Passenger.
# Joe Fiorini (http://www.faithfulgeek.org)
#
# Rails autoloading, while nice in theory, frequently doesn't work. Since Rails 2.3+
# is so fast when completely reloading the server, I wrote this script to listen to the
# given directories, and kill/restart the server when any file is changed.
#
# It's quick, simple, and it reliably reloads your application when changes are made.
#
# To install, just copy it into the scripts directory and set it to be executable.
#
# Jonathan Penn (http://www.wavethenavel.com)
POLL_DIRECTORIES = %w(app config db lib)
POLL_TIME = 0.9 # In Seconds
SERVER_COMMAND = "touch tmp/restart.txt"
# For passenger:
# SERVER_COMMAND = "touch tmp/restart.txt"
Dir.chdir(File.dirname(__FILE__)+"/..")
@states = {}
def check_for_changes
changes = []
Dir[*POLL_DIRECTORIES.map{|i|i+"/**/*"}].each do |file|
unless File.symlink?(file)
new_time = File.stat(file).mtime
if @states[file] != new_time
@states[file] = new_time
changes << file
end
end
end
changes
end
def emphasized(m)
"\e[1;1m\e[41m \e[0m \e[1;1m\e[1m #{m} \e[0m"
end
system SERVER_COMMAND
Signal.trap("INT") { puts "Shutting down server_restarter"; exit }
check_for_changes
$stderr.puts "server_restarter running...watching #{@states.length} files every #{POLL_TIME} seconds."
loop do
sleep POLL_TIME
changes = check_for_changes
unless changes.empty?
$stderr.puts emphasized("Changes found...#{changes.inspect}")
system SERVER_COMMAND
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment