Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Improve pidfile reporting and test coverage

 * Output reduced to a single line
 * Integration test added that also suppresses and checks output
  • Loading branch information...
commit edc8b923fa4ea4e3c1ce8778f5ddbc89688bc01b 1 parent b4d7b95
James Tucker authored May 13, 2012
9  lib/rack/server.rb
@@ -325,14 +325,10 @@ def write_pid
325 325
 
326 326
       def check_pid!
327 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."
  328
+        when :running, :not_owned
  329
+          $stderr.puts "A server is already running. Check #{options[:pid]}."
333 330
           exit(1)
334 331
         when :dead
335  
-          STDERR.puts "The process is no longer running. Removing the pid file."
336 332
           ::File.delete(options[:pid])
337 333
         end
338 334
       end
@@ -341,7 +337,6 @@ def pidfile_process_status
341 337
         return :exited unless ::File.exist?(options[:pid])
342 338
 
343 339
         pid = ::File.read(options[:pid]).to_i
344  
-        STDERR.puts "There is already a pid file wrote a by process with pid #{pid}"
345 340
         Process.kill(0, pid)
346 341
         :running
347 342
       rescue Errno::ESRCH
21  test/spec_server.rb
@@ -10,6 +10,13 @@ def app
10 10
     lambda { |env| [200, {'Content-Type' => 'text/plain'}, ['success']] }
11 11
   end
12 12
 
  13
+  def with_stderr
  14
+    old, $stderr = $stderr, StringIO.new
  15
+    yield $stderr
  16
+  ensure
  17
+    $stderr = old
  18
+  end
  19
+
13 20
   it "overrides :config if :app is passed in" do
14 21
     server = Rack::Server.new(:app => "FOO")
15 22
     server.app.should == "FOO"
@@ -97,4 +104,18 @@ def app
97 104
     server.send(:pidfile_process_status).should.eql :not_owned
98 105
   end
99 106
 
  107
+  should "inform the user about existing pidfiles with running processes" do
  108
+    pidfile = Tempfile.open('pidfile') { |f| f.write(1); break f }.path
  109
+    server = Rack::Server.new(:pid => pidfile)
  110
+    with_stderr do |err|
  111
+      should.raise(SystemExit) do
  112
+        server.start
  113
+      end
  114
+      err.rewind
  115
+      output = err.read
  116
+      output.should.match(/already running/)
  117
+      output.should.include? pidfile
  118
+    end
  119
+  end
  120
+
100 121
 end

0 notes on commit edc8b92

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