Make script/server work with -d and -e on Mongrel

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5575 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
David Heinemeier Hansson
2006-11-19 21:52:14 +00:00
parent 5999fdd3f3
commit ee8121fc76

View File

@@ -6,57 +6,48 @@ unless defined?(Mongrel)
exit 1
end
require 'initializer'
Rails::Initializer.run(:initialize_logger)
require 'optparse'
detach = false
ip = nil
port = nil
mime = 'config/mime.yml'
OPTIONS = {
:port => 3000,
:ip => "0.0.0.0",
:environment => (ENV['RAILS_ENV'] || "development").dup,
:detach => false
}
ARGV.clone.options do |opt|
opt.on("-p", "--port=port", Integer,
"Runs Rails on the specified port.",
"Default: 3000") { |p| port = p }
opt.on("-a", "--address=ip", String,
"Binds Rails to the specified ip.",
"Default: 0.0.0.0") { |i| ip = i }
opt.on("-m", "--mime=path", String,
"Path to custom mime file.",
"Default: config/mime.yml (if it exists)") { |m| mime = m }
opt.on('-h', '--help', 'Show this message.') { puts opt; exit 0 }
opt.on('-d', '-d', 'Call with -d to detach') { detach = true }
opt.parse!
ARGV.clone.options do |opts|
opts.on("-p", "--port=port", Integer, "Runs Rails on the specified port.", "Default: 3000") { |v| OPTIONS[:port] = v }
opts.on("-b", "--binding=ip", String, "Binds Rails to the specified ip.", "Default: 0.0.0.0") { |v| OPTIONS[:ip] = v }
opts.on("-d", "--daemon", "Make server run as a Daemon.") { OPTIONS[:detach] = true }
opts.on("-e", "--environment=name", String,
"Specifies the environment to run this server under (test/development/production).",
"Default: development") { |v| OPTIONS[:environment] = v }
opts.separator ""
opts.on("-h", "--help", "Show this help message.") { puts opts; exit }
opts.parse!
end
default_port, default_ip = 3000, '0.0.0.0'
puts "=> Rails application started on http://#{ip || default_ip}:#{port || default_port}"
puts "=> Rails application starting on http://#{OPTIONS[:ip]}:#{OPTIONS[:port]}"
log_file = Pathname.new("#{RAILS_ROOT}/log/#{RAILS_ENV}.log").cleanpath
if OPTIONS[:detach]
`mongrel_rails start -d -p #{OPTIONS[:port]} -a #{OPTIONS[:ip]} -e #{OPTIONS[:environment]}`
else
require 'initializer'
Rails::Initializer.run(:initialize_logger)
tail_thread = nil
if !detach
puts "=> Call with -d to detach"
puts "=> Ctrl-C to shutdown server"
detach = false
tail_thread = tail(log_file)
end
tail_thread = tail(Pathname.new("#{RAILS_ROOT}/log/#{RAILS_ENV}.log").cleanpath)
trap(:INT) { exit }
trap(:INT) { exit }
if File.exist?(File.join(RAILS_ROOT, mime)) && !ARGV.any? { |a| a =~ /^--?m/ }
ARGV << "--mime=#{mime}"
end
begin
ARGV.unshift("start")
load 'mongrel_rails'
ensure
unless detach
begin
`mongrel_rails start -p #{OPTIONS[:port]} -a #{OPTIONS[:ip]} -e #{OPTIONS[:environment]}`
ensure
tail_thread.kill if tail_thread
puts 'Exiting'
end
end
end