From e8d6936a049ae876ad161988a6eeb8a001bcb459 Mon Sep 17 00:00:00 2001 From: zedshaw Date: Sun, 26 Feb 2006 21:39:40 +0000 Subject: [PATCH] Implements an improved SwitchTower friendly mongrel_rails. git-svn-id: svn+ssh://rubyforge.org/var/svn/mongrel/trunk@61 19e92222-5c0b-0410-8929-a290d50e31e9 --- Rakefile | 2 +- bin/mongrel_rails | 86 ++++++++++++++++++++++++++++++++++++------ lib/mongrel.rb | 2 +- lib/mongrel/command.rb | 4 +- 4 files changed, 79 insertions(+), 15 deletions(-) diff --git a/Rakefile b/Rakefile index c023c6be..9e56614a 100644 --- a/Rakefile +++ b/Rakefile @@ -30,7 +30,7 @@ end setup_extension("http11", "http11") -version="0.3.6" +version="0.3.7" summary = "A small fast HTTP library and server that runs Rails, Camping, and Nitro apps." test_file = "test/test_ws.rb" author="Zed A. Shaw" diff --git a/bin/mongrel_rails b/bin/mongrel_rails index 8c3ccc5e..9b9ea60c 100644 --- a/bin/mongrel_rails +++ b/bin/mongrel_rails @@ -117,19 +117,40 @@ class StartCommand < Mongrel::Command::Command end def start_mongrel(rails) + @restart = false + # start up mongrel with the right configurations server = Mongrel::HttpServer.new(@address, @port, @num_procs.to_i, @timeout.to_i) server.register("/", rails) server.run - trap("INT") { server.stop } + + # graceful shutdown + trap("TERM") { + server.stop + } + # rails reload + trap("HUP") { + server.stop + @restart = true + } + + # restart + trap("USR2") { + server.stop + @restart = true + } + begin - puts "Server ready." + STDERR.puts "Server ready." server.acceptor.join rescue Interrupt - puts "Interrupted." + STDERR.puts "Interrupted." raise end + + # daemonize makes restart easy + run if @restart end def run @@ -140,12 +161,24 @@ class StartCommand < Mongrel::Command::Command 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 StopCommand < Mongrel::Command::Command def configure - options [ + 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 @@ -162,16 +195,47 @@ class StopCommand < Mongrel::Command::Command def run - pid = open(@pid_file).read.to_i - print "Stopping Mongrel at PID #{pid}..." - begin - Process.kill("INT", pid) - rescue Errno::ESRCH - puts "Process does not exist. Not running." + if @force + send_signal("KILL", @pid_file) + else + send_signal("TERM", @pid_file) + end + + File.unlink(@pid_file) + end +end + + + +class RestartCommand < Mongrel::Command::Command + + 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 File.unlink(@pid_file) - puts "Done." end end diff --git a/lib/mongrel.rb b/lib/mongrel.rb index d4d39419..f7ae112d 100644 --- a/lib/mongrel.rb +++ b/lib/mongrel.rb @@ -118,7 +118,7 @@ module Mongrel SERVER_SOFTWARE='SERVER_SOFTWARE' # Current Mongrel version (used for SERVER_SOFTWARE and other response headers). - MONGREL_VERSION='Mongrel 0.3.6' + MONGREL_VERSION='Mongrel 0.3.7' # The standard empty 404 response for bad requests. Use Error4040Handler for custom stuff. ERROR_404_RESPONSE="HTTP/1.1 404 Not Found\r\nConnection: close\r\nServer: #{MONGREL_VERSION}\r\n\r\nNOT FOUND" diff --git a/lib/mongrel/command.rb b/lib/mongrel/command.rb index 410f84f9..3625438e 100644 --- a/lib/mongrel/command.rb +++ b/lib/mongrel/command.rb @@ -44,6 +44,7 @@ module Mongrel @valid = true # this is retarded, but it has to be done this way because -h and -v exit @done_validating = false + @original_args = argv.dup configure @@ -154,9 +155,8 @@ module Mongrel # Runs the args against the first argument as the command name. # If it has any errors it returns a false, otherwise it return true. def run(args) - # find the command and change the program's name to reflect it + # find the command cmd_name = args.shift - $0 = "#{cmd_name}" if !cmd_name or cmd_name == "?" or cmd_name == "help" print_command_list