about summary refs log tree commit homepage
path: root/bin
diff options
context:
space:
mode:
authorzedshaw <zedshaw@19e92222-5c0b-0410-8929-a290d50e31e9>2006-02-26 21:39:40 +0000
committerzedshaw <zedshaw@19e92222-5c0b-0410-8929-a290d50e31e9>2006-02-26 21:39:40 +0000
commite8d6936a049ae876ad161988a6eeb8a001bcb459 (patch)
treea4b6ab7ea7bb3d89424438efcc63ff15a6c32af2 /bin
parentd8da35551287f33acf93f48fb9ab6bdb14e11d65 (diff)
downloadunicorn-e8d6936a049ae876ad161988a6eeb8a001bcb459.tar.gz
git-svn-id: svn+ssh://rubyforge.org/var/svn/mongrel/trunk@61 19e92222-5c0b-0410-8929-a290d50e31e9
Diffstat (limited to 'bin')
-rw-r--r--bin/mongrel_rails86
1 files changed, 75 insertions, 11 deletions
diff --git a/bin/mongrel_rails b/bin/mongrel_rails
index 8c3ccc5..9b9ea60 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