Created
February 19, 2010 20:02
-
-
Save lstoll/309141 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# Stakeout inspired, Hacked from benschwarz's watch | |
# Will restart node if any .js files in or below this folder change. Will also restart | |
# if node exits (with protection to avoid rapid exit loops) | |
# Assumes your main script is called server.js . | |
def start_node | |
puts "\n** Node Started **\n" | |
@node_pid = Kernel.fork { exec "node server.js" } | |
end | |
def file_list | |
Dir[@path].map {|file| File.mtime(file) } | |
end | |
start_node | |
@path = ::File.join(::File.expand_path(::File.dirname(__FILE__)), '**/*.js') | |
@files = file_list | |
@last_run_exited = false | |
loop do | |
sleep 0.5 | |
if file_list != @files | |
@files = file_list | |
puts "\n** Restarting node **\n" | |
Process.kill("INT", @node_pid) | |
start_node | |
end | |
# Check if node has died, if so restart. | |
begin | |
Process.getpgid(@node_pid) | |
@last_run_exited = false | |
rescue | |
# only auto-restart if the last loop wasn't in error | |
if @last_run_exited | |
puts "\n** Node exited repeatedly, slowing restart **\n" | |
sleep 4.5 | |
else | |
puts "\n** Node exited, restarting **\n" | |
end | |
start_node | |
@last_run_exited = true | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment