Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Check if the PID in pidfile is still running #371

Then abort or remove the pidfile
  • Loading branch information...
commit b4d7b95a057e6d9379848d7695d9ac93d4f48144 1 parent c582fb2
Jean Boussier authored raggi committed
27  lib/rack/server.rb
@@ -324,11 +324,30 @@ def write_pid
324 324
       end
325 325
 
326 326
       def check_pid!
327  
-        return unless ::File.exist?(options[:pid])
328  
-        pid = ::File.read(options[:pid])
  327
+        case pidfile_process_status
  328
+        when :running
  329
+          STDERR.puts "The process is still alive. aborting."
  330
+          exit(1)
  331
+        when :not_owned
  332
+          STDERR.puts "The process is still alive. And owned by another user aborting."
  333
+          exit(1)
  334
+        when :dead
  335
+          STDERR.puts "The process is no longer running. Removing the pid file."
  336
+          ::File.delete(options[:pid])
  337
+        end
  338
+      end
  339
+
  340
+      def pidfile_process_status
  341
+        return :exited unless ::File.exist?(options[:pid])
  342
+
  343
+        pid = ::File.read(options[:pid]).to_i
329 344
         STDERR.puts "There is already a pid file wrote a by process with pid #{pid}"
330  
-        STDERR.puts "If this process is no longer running, you have to remove the pidfile #{options[:pid]}"
331  
-        exit(1)
  345
+        Process.kill(0, pid)
  346
+        :running
  347
+      rescue Errno::ESRCH
  348
+        :dead
  349
+      rescue Errno::EPERM
  350
+        :not_owned
332 351
       end
333 352
 
334 353
   end
26  test/spec_server.rb
@@ -71,4 +71,30 @@ def app
71 71
     open(pidfile) { |f| f.read.should.eql $$.to_s }
72 72
   end
73 73
 
  74
+  should "check pid file presence and running process" do
  75
+    pidfile = Tempfile.open('pidfile') { |f| f.write($$); break f }.path
  76
+    server = Rack::Server.new(:pid => pidfile)
  77
+    server.send(:pidfile_process_status).should.eql :running
  78
+  end
  79
+
  80
+  should "check pid file presence and dead process" do
  81
+    dead_pid = `echo $$`.to_i
  82
+    pidfile = Tempfile.open('pidfile') { |f| f.write(dead_pid); break f }.path
  83
+    server = Rack::Server.new(:pid => pidfile)
  84
+    server.send(:pidfile_process_status).should.eql :dead
  85
+  end
  86
+
  87
+  should "check pid file presence and exited process" do
  88
+    pidfile = Tempfile.open('pidfile') { |f| break f }.path
  89
+    ::File.delete(pidfile)
  90
+    server = Rack::Server.new(:pid => pidfile)
  91
+    server.send(:pidfile_process_status).should.eql :exited
  92
+  end
  93
+
  94
+  should "check pid file presence and not owned process" do
  95
+    pidfile = Tempfile.open('pidfile') { |f| f.write(1); break f }.path
  96
+    server = Rack::Server.new(:pid => pidfile)
  97
+    server.send(:pidfile_process_status).should.eql :not_owned
  98
+  end
  99
+
74 100
 end

0 notes on commit b4d7b95

Please sign in to comment.
Something went wrong with that request. Please try again.