Permalink
Browse files

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
  • Loading branch information...
1 parent c7e5c27 commit 73a697c38a842dfa725185b73af3b04a49ba3d3f @seckar seckar committed Nov 9, 2005
Showing with 21 additions and 19 deletions.
  1. +2 −0 railties/CHANGELOG
  2. +19 −19 railties/lib/commands/servers/lighttpd.rb
View
@@ -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]
@@ -8,16 +8,6 @@
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 @@ def tail_f(input)
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 @@ def tail_f(input)
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

0 comments on commit 73a697c

Please sign in to comment.