2006-02-09 21:38:18 -05:00
|
|
|
require 'rubygems'
|
2006-03-05 02:58:18 -05:00
|
|
|
require 'yaml'
|
2006-03-08 22:05:11 -05:00
|
|
|
require 'mongrel'
|
2006-02-09 20:06:55 -05:00
|
|
|
|
2006-03-06 00:31:39 -05:00
|
|
|
|
|
|
|
class Start < GemPlugin::Plugin "/commands"
|
2006-03-01 22:54:32 -05:00
|
|
|
include Mongrel::Command::Base
|
2006-02-09 20:06:55 -05:00
|
|
|
|
2006-02-11 20:30:33 -05:00
|
|
|
def configure
|
|
|
|
options [
|
2006-03-01 22:54:32 -05:00
|
|
|
["-e", "--environment ENV", "Rails environment to run as", :@environment, ENV['RAILS_ENV'] || "development"],
|
|
|
|
["-d", "--daemonize", "Whether to run in the background or not", :@daemon, false],
|
|
|
|
['-p', '--port PORT', "Which port to bind to", :@port, 3000],
|
|
|
|
['-a', '--address ADDR', "Address to bind to", :@address, "0.0.0.0"],
|
|
|
|
['-l', '--log FILE', "Where to write log messages", :@log_file, "log/mongrel.log"],
|
|
|
|
['-P', '--pid FILE', "Where to write the PID", :@pid_file, "log/mongrel.pid"],
|
2006-03-21 22:31:30 -05:00
|
|
|
['-n', '--num-procs INT', "Number of processor threads to use", :@num_procs, 1024],
|
|
|
|
['-t', '--timeout SECONDS', "Timeout all requests after SECONDS time", :@timeout, 0],
|
2006-03-01 22:54:32 -05:00
|
|
|
['-m', '--mime PATH', "A YAML file that lists additional MIME types", :@mime_map, nil],
|
|
|
|
['-c', '--chdir PATH', "Change to dir before starting (will be expanded)", :@cwd, Dir.pwd],
|
|
|
|
['-r', '--root PATH', "Set the document root (default 'public')", :@docroot, "public"],
|
2006-02-11 20:30:33 -05:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate
|
2006-02-14 07:19:02 -05:00
|
|
|
@cwd = File.expand_path(@cwd)
|
|
|
|
valid_dir? @cwd, "Invalid path to change to during daemon mode: #@cwd"
|
|
|
|
|
|
|
|
# change there to start, then we'll have to come back after daemonize
|
|
|
|
Dir.chdir(@cwd)
|
|
|
|
|
2006-02-11 20:30:33 -05:00
|
|
|
valid_dir? File.dirname(@log_file), "Path to log file not valid: #@log_file"
|
|
|
|
valid_dir? File.dirname(@pid_file), "Path to pid file not valid: #@pid_file"
|
2006-02-14 07:19:02 -05:00
|
|
|
valid_dir? @docroot, "Path to docroot not valid: #@docroot"
|
|
|
|
valid_exists? @mime_map, "MIME mapping file does not exist: #@mime_map" if @mime_map
|
|
|
|
|
2006-02-11 20:30:33 -05:00
|
|
|
return @valid
|
|
|
|
end
|
2006-02-10 01:45:50 -05:00
|
|
|
|
2006-03-01 22:54:32 -05:00
|
|
|
|
2006-02-14 07:19:02 -05:00
|
|
|
def daemonize
|
|
|
|
# save this for later since daemonize will hose it
|
2006-02-20 14:00:49 -05:00
|
|
|
if @daemon and RUBY_PLATFORM !~ /mswin/
|
|
|
|
require 'daemons/daemonize'
|
|
|
|
|
2006-02-14 07:19:02 -05:00
|
|
|
puts "Started Mongrel server in #@environment mode at #@address:#@port"
|
|
|
|
Daemonize.daemonize(log_file=File.join(@cwd, @log_file))
|
|
|
|
|
2006-02-11 20:30:33 -05:00
|
|
|
# change back to the original starting directory
|
2006-02-14 07:19:02 -05:00
|
|
|
Dir.chdir(@cwd)
|
|
|
|
|
|
|
|
open(@pid_file,"w") {|f| f.write(Process.pid) }
|
2006-02-11 20:30:33 -05:00
|
|
|
else
|
2006-02-14 07:19:02 -05:00
|
|
|
puts "Running Mongrel server in #@environment mode at #@address:#@port"
|
2006-02-11 20:30:33 -05:00
|
|
|
end
|
2006-02-14 07:19:02 -05:00
|
|
|
end
|
2006-02-11 20:30:33 -05:00
|
|
|
|
2006-02-14 07:19:02 -05:00
|
|
|
def load_mime_map
|
|
|
|
mime = {}
|
|
|
|
|
|
|
|
# configure any requested mime map
|
|
|
|
if @mime_map
|
|
|
|
puts "Loading additional MIME types from #@mime_map"
|
|
|
|
mime.merge!(YAML.load_file(@mime_map))
|
2006-02-11 20:30:33 -05:00
|
|
|
|
2006-02-14 07:19:02 -05:00
|
|
|
# check all the mime types to make sure they are the right format
|
|
|
|
mime.each {|k,v| puts "WARNING: MIME type #{k} must start with '.'" if k.index(".") != 0 }
|
|
|
|
end
|
|
|
|
|
|
|
|
return mime
|
|
|
|
end
|
|
|
|
|
|
|
|
def configure_rails
|
2006-03-07 19:52:20 -05:00
|
|
|
# need this later for safe reloading
|
2006-03-12 02:30:25 -05:00
|
|
|
$orig_dollar_quote = $".clone
|
2006-03-07 19:52:20 -05:00
|
|
|
|
2006-02-14 07:19:02 -05:00
|
|
|
ENV['RAILS_ENV'] = @environment
|
2006-03-15 01:21:44 -05:00
|
|
|
require 'config/environment'
|
2006-03-07 19:52:20 -05:00
|
|
|
require 'dispatcher'
|
2006-03-08 22:05:11 -05:00
|
|
|
require 'mongrel/rails'
|
2006-02-14 07:19:02 -05:00
|
|
|
|
|
|
|
# configure the rails handler
|
|
|
|
rails = RailsHandler.new(@docroot, load_mime_map)
|
|
|
|
|
|
|
|
return rails
|
|
|
|
end
|
|
|
|
|
|
|
|
def start_mongrel(rails)
|
2006-02-26 16:39:40 -05:00
|
|
|
@restart = false
|
|
|
|
|
2006-02-14 07:19:02 -05:00
|
|
|
server = Mongrel::HttpServer.new(@address, @port, @num_procs.to_i, @timeout.to_i)
|
|
|
|
server.register("/", rails)
|
2006-03-05 02:58:18 -05:00
|
|
|
|
2006-02-26 19:56:59 -05:00
|
|
|
# signal trapping just applies to posix systems
|
|
|
|
# TERM is a valid signal, but still doesn't gracefuly shutdown on win32.
|
|
|
|
if RUBY_PLATFORM !~ /mswin/
|
|
|
|
# graceful shutdown
|
|
|
|
trap("TERM") {
|
|
|
|
server.stop
|
2006-03-12 02:30:25 -05:00
|
|
|
File.unlink @pid_file if File.exist?(@pid_file)
|
2006-02-26 19:56:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
# rails reload
|
|
|
|
trap("HUP") {
|
2006-03-07 19:52:20 -05:00
|
|
|
STDERR.puts "Reloading rails..."
|
|
|
|
rails.reload!
|
|
|
|
STDERR.puts "Done reloading rails."
|
2006-02-26 19:56:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
# restart
|
|
|
|
trap("USR2") {
|
|
|
|
server.stop
|
2006-03-12 02:30:25 -05:00
|
|
|
File.unlink @pid_file if File.exist?(@pid_file)
|
2006-02-26 19:56:59 -05:00
|
|
|
@restart = true
|
|
|
|
}
|
2006-03-19 18:31:30 -05:00
|
|
|
|
|
|
|
trap("INT") {
|
|
|
|
server.stop
|
|
|
|
File.unlink @pid_file if File.exist?(@pid_file)
|
|
|
|
@restart = false
|
|
|
|
}
|
2006-02-26 19:56:59 -05:00
|
|
|
end
|
2006-03-05 02:58:18 -05:00
|
|
|
|
|
|
|
# hook up any rails specific plugins
|
2006-03-06 00:31:39 -05:00
|
|
|
GemPlugin::Manager.instance.load "mongrel" => GemPlugin::INCLUDE
|
2006-03-05 02:58:18 -05:00
|
|
|
|
2006-02-11 20:30:33 -05:00
|
|
|
begin
|
2006-03-07 19:52:20 -05:00
|
|
|
# start mongrel processing thread
|
|
|
|
server.run
|
2006-03-14 00:48:37 -05:00
|
|
|
|
|
|
|
if RUBY_PLATFORM !~ /mswin/
|
|
|
|
puts "Server Ready. Use CTRL-C to quit."
|
|
|
|
else
|
|
|
|
puts "Server Ready. Use CTRL-Pause/Break to quit."
|
|
|
|
end
|
|
|
|
|
2006-02-14 07:19:02 -05:00
|
|
|
server.acceptor.join
|
2006-02-11 20:30:33 -05:00
|
|
|
rescue Interrupt
|
2006-02-26 16:39:40 -05:00
|
|
|
STDERR.puts "Interrupted."
|
2006-02-19 10:03:32 -05:00
|
|
|
raise
|
2006-02-11 22:37:38 -05:00
|
|
|
end
|
2006-02-26 16:39:40 -05:00
|
|
|
|
|
|
|
# daemonize makes restart easy
|
|
|
|
run if @restart
|
2006-02-11 22:37:38 -05:00
|
|
|
end
|
2006-02-14 07:19:02 -05:00
|
|
|
|
|
|
|
def run
|
|
|
|
daemonize
|
|
|
|
rails = configure_rails
|
|
|
|
start_mongrel(rails)
|
|
|
|
end
|
2006-02-11 22:37:38 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2006-02-26 16:39:40 -05:00
|
|
|
def send_signal(signal, pid_file)
|
|
|
|
pid = open(pid_file).read.to_i
|
|
|
|
print "Sending #{signal} to Mongrel at PID #{pid}..."
|
|
|
|
begin
|
|
|
|
Process.kill(signal, pid)
|
|
|
|
rescue Errno::ESRCH
|
|
|
|
puts "Process does not exist. Not running."
|
|
|
|
end
|
|
|
|
|
|
|
|
puts "Done."
|
|
|
|
end
|
2006-02-11 22:37:38 -05:00
|
|
|
|
2006-02-28 02:04:41 -05:00
|
|
|
|
2006-03-06 00:31:39 -05:00
|
|
|
class Stop < GemPlugin::Plugin "/commands"
|
2006-03-01 22:54:32 -05:00
|
|
|
include Mongrel::Command::Base
|
2006-02-11 22:37:38 -05:00
|
|
|
|
|
|
|
def configure
|
2006-02-26 16:39:40 -05:00
|
|
|
options [
|
2006-02-14 07:19:02 -05:00
|
|
|
['-c', '--chdir PATH', "Change to dir before starting (will be expanded)", :@cwd, Dir.pwd],
|
2006-02-26 16:39:40 -05:00
|
|
|
['-f', '--force', "Force the shutdown.", :@force, false],
|
2006-02-14 07:19:02 -05:00
|
|
|
['-P', '--pid FILE', "Where to write the PID", :@pid_file, "log/mongrel.pid"]
|
2006-02-11 22:37:38 -05:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate
|
2006-02-14 07:19:02 -05:00
|
|
|
@cwd = File.expand_path(@cwd)
|
|
|
|
valid_dir? @cwd, "Invalid path to change to during daemon mode: #@cwd"
|
|
|
|
|
|
|
|
@pid_file = File.join(@cwd,@pid_file)
|
2006-02-11 22:37:38 -05:00
|
|
|
valid_exists? @pid_file, "PID file #@pid_file does not exist. Not running?"
|
2006-02-14 07:19:02 -05:00
|
|
|
|
2006-02-11 22:37:38 -05:00
|
|
|
return @valid
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def run
|
2006-02-26 16:39:40 -05:00
|
|
|
if @force
|
|
|
|
send_signal("KILL", @pid_file)
|
|
|
|
else
|
|
|
|
send_signal("TERM", @pid_file)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-03-06 00:31:39 -05:00
|
|
|
class Restart < GemPlugin::Plugin "/commands"
|
2006-03-01 22:54:32 -05:00
|
|
|
include Mongrel::Command::Base
|
2006-02-26 16:39:40 -05:00
|
|
|
|
|
|
|
def configure
|
|
|
|
options [
|
|
|
|
['-c', '--chdir PATH', "Change to dir before starting (will be expanded)", :@cwd, Dir.pwd],
|
|
|
|
['-s', '--soft', "Do a soft restart rather than a process exit restart", :@soft, false],
|
|
|
|
['-P', '--pid FILE', "Where to write the PID", :@pid_file, "log/mongrel.pid"]
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate
|
|
|
|
@cwd = File.expand_path(@cwd)
|
|
|
|
valid_dir? @cwd, "Invalid path to change to during daemon mode: #@cwd"
|
|
|
|
|
|
|
|
@pid_file = File.join(@cwd,@pid_file)
|
|
|
|
valid_exists? @pid_file, "PID file #@pid_file does not exist. Not running?"
|
|
|
|
|
|
|
|
return @valid
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def run
|
|
|
|
if @soft
|
|
|
|
send_signal("HUP", @pid_file)
|
|
|
|
else
|
|
|
|
send_signal("USR2", @pid_file)
|
2006-02-11 20:30:33 -05:00
|
|
|
end
|
|
|
|
end
|
2006-02-09 21:38:18 -05:00
|
|
|
end
|
2006-02-11 20:30:33 -05:00
|
|
|
|
2006-03-06 00:31:39 -05:00
|
|
|
GemPlugin::Manager.instance.load "mongrel" => GemPlugin::INCLUDE, "rails" => GemPlugin::EXCLUDE
|
|
|
|
|
2006-02-11 20:30:33 -05:00
|
|
|
Mongrel::Command::Registry.instance.run ARGV
|