1
0
Fork 0
mirror of https://github.com/puma/puma.git synced 2022-11-09 13:48:40 -05:00
puma--puma/bin/mongrel_rails

199 lines
6.1 KiB
Text
Raw Normal View History

require 'rubygems'
require 'yaml'
require 'mongrel/rails'
class Start < GemPlugin::Plugin "/commands"
include Mongrel::Command::Base
def configure
options [
["-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"],
['-n', '--num-procs INT', "Number of processor threads to use", :@num_procs, 1024],
['-t', '--timeout SECONDS', "Timeout all requests after SECONDS time", :@timeout, 0],
['-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"],
['-B', '--debug', "Enable debugging mode", :@debug, false],
['-C', '--config PATH', "Use a config file", :@config_file, nil],
['-S', '--script PATH', "Load the given file as an extra config script.", :@config_script, nil]
]
end
def validate
@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)
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"
valid_dir? @docroot, "Path to docroot not valid: #@docroot"
valid_exists? @mime_map, "MIME mapping file does not exist: #@mime_map" if @mime_map
valid_exists? @config_file, "Config file not there: #@config_file" if @config_file
return @valid
end
def run
# command line setting override config file settings
settings = { :host => @address, :port => @port, :cwd => @cwd,
:log_file => @log_file, :pid_file => @pid_file, :environment => @environment,
:docroot => @docroot, :mime_map => @mime_map, :daemon => @daemon,
:debug => @debug, :includes => ["mongrel"]
}
if @config_file
STDERR.puts "** Loading settings from #{@config_file} (command line options override)."
conf = YAML.load_file(@config_file)
settings = conf.merge(settings)
end
config = Mongrel::Rails::RailsConfigurator.new(settings) do
log "Starting Mongrel in #{settings[:environment]} mode at #{settings[:host]}:#{settings[:port]}"
if defaults[:daemon]
log "Daemonizing, any open files are closed. Look at #{settings[:pid_file]} and #{settings[:log_file]} for info."
daemonize
end
listener do
mime = {}
if defaults[:mime_map]
log "Loading additional MIME types from #{settings[:mime_map]}"
mime = load_mime_map(defaults[:mime_map], mime)
end
if defaults[:debug]
log "Installing debugging prefixed filters. Look in log/mongrel_debug for the files."
debug "/"
end
log "Starting Rails in environment #{settings[:environment]} ..."
uri "/", :handler => rails
log "Rails loaded."
log "Loading any Rails specific GemPlugins"
load_plugins
if settings[:config_script]
log "Loading #{settings[:config_script]} external config script"
run_config("config/mongrel.rb")
end
setup_rails_signals
end
end
config.run
config.log "Mongrel available at #{settings[:host]}:#{settings[:port]}"
config.join
if config.needs_restart
if RUBY_PLATFORM !~ /mswin/
cmd = "ruby #{__FILE__} start #{original_args.join(' ')}"
config.log "Restarting with arguments: #{cmd}"
exec cmd
else
config.log "Win32 does not support restarts. Exiting."
end
end
end
end
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
class Stop < GemPlugin::Plugin "/commands"
include Mongrel::Command::Base
def configure
options [
['-c', '--chdir PATH', "Change to dir before starting (will be expanded)", :@cwd, Dir.pwd],
['-f', '--force', "Force the shutdown.", :@force, 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 @force
send_signal("KILL", @pid_file)
else
send_signal("TERM", @pid_file)
end
end
end
class Restart < GemPlugin::Plugin "/commands"
include Mongrel::Command::Base
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)
end
end
end
GemPlugin::Manager.instance.load "mongrel" => GemPlugin::INCLUDE, "rails" => GemPlugin::EXCLUDE
if not Mongrel::Command::Registry.instance.run ARGV
exit 1
end