Don't detach or fork for script/server tailing

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2961 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
Nicholas Seckar
2005-11-09 22:57:05 +00:00
parent c7e5c27b51
commit 73a697c38a
2 changed files with 21 additions and 19 deletions

View File

@@ -1,5 +1,7 @@
*SVN*
* Don't detach or fork for script/server tailing [Nicholas Seckar]
* Added automatic browser launching on OS X when starting script/server [DHH]
* Changed all script/* to use #!/usr/bin/env ruby instead of hard-coded Ruby path. public/dispatcher.* still uses the hard-coded path for compatibility with web servers that don't have Ruby in path [DHH]

View File

@@ -8,16 +8,6 @@ unless defined?(FCGI)
exit 1
end
def tail_f(input)
loop do
line = input.gets
yield line if line
if input.eof?
sleep 1
input.seek(input.tell)
end
end
end
config_file = "#{RAILS_ROOT}/config/lighttpd.conf"
@@ -32,6 +22,8 @@ end
port = IO.read(config_file).scan(/^server.port\s*=\s*(\d+)/).first rescue 3000
puts "=> Rails application started on http://0.0.0.0:#{port}"
tail_thread = nil
if ARGV.first == "-d"
puts "=> Configure in config/lighttpd.conf"
detach = true
@@ -40,18 +32,26 @@ else
puts "=> Ctrl-C to shutdown server (see config/lighttpd.conf for options)"
detach = false
Process.detach(fork do
begin
File.open("#{RAILS_ROOT}/log/#{RAILS_ENV}.log", 'r') do |log|
log.seek(0, IO::SEEK_END)
tail_f(log) {|line| puts line}
log_path = "#{RAILS_ROOT}/log/#{RAILS_ENV}.log"
cursor = File.size(log_path)
last_checked = Time.now
tail_thread = Thread.new do
File.open(log_path, 'r') do |f|
loop do
f.seek cursor
if f.mtime > last_checked
last_checked = f.mtime
contents = f.read
cursor += contents.length
print contents
end
sleep 1
end
rescue Exception
end
exit
end)
end
end
trap(:INT) { exit }
Thread.new { sleep 0.5; `open http://0.0.0.0:#{port}` } if RUBY_PLATFORM =~ /darwin/
`lighttpd #{!detach ? "-D " : ""}-f #{config_file}`
`lighttpd #{!detach ? "-D " : ""}-f #{config_file}`
tail_thread.kill if tail_thread