about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2009-02-09 21:42:07 -0800
committerEric Wong <normalperson@yhbt.net>2009-02-09 21:43:35 -0800
commit5a842b0621516d4a74dadd391b495eaa6ec60744 (patch)
tree527cb8f2b52e4f7db00d25dc85a3c42dba78b138
parent2abb663f6e20f9bc0f14ac00c0edeb57bf3f43e0 (diff)
downloadunicorn-5a842b0621516d4a74dadd391b495eaa6ec60744.tar.gz
This makes it possible to bind per-process listener ports for
easier debugging.  One of my biggest gripes about other prefork
webservers is that strace-ing the correct process for debugging
is difficult.  This makes it possible for each worker to bind to
a unique port or UNIX socket independent of the other workers.
-rw-r--r--lib/unicorn.rb17
1 files changed, 17 insertions, 0 deletions
diff --git a/lib/unicorn.rb b/lib/unicorn.rb
index c0474c6..2e7a1a9 100644
--- a/lib/unicorn.rb
+++ b/lib/unicorn.rb
@@ -39,6 +39,9 @@ module Unicorn
       :nr_workers => 1,
       :after_fork => lambda { |server, worker_nr|
           server.logger.info("worker=#{worker_nr} spawned pid=#{$$}")
+
+          # per-process listener ports for debugging/admin:
+          # server.add_listener("127.0.0.1:#{8081 + worker_nr}")
         },
       :before_fork => lambda { |server, worker_nr|
           server.logger.info("worker=#{worker_nr} spawning...")
@@ -128,6 +131,20 @@ module Unicorn
       self
     end
 
+    # Allows workers to add a private, per-process listener via the
+    # @after_fork hook.  Very useful for debugging and testing.
+    def add_listener(address)
+      if io = bind_listen(address, 1024)
+        @purgatory << io
+        io = server_cast(io)
+        logger.info "adding listener #{io} addr=#{sock_name(io)}"
+        @listeners << io
+      else
+        logger.error "adding listener failed addr=#{address} (in use)"
+        raise Errno::EADDRINUSE, address
+      end
+    end
+
     # monitors children and receives signals forever
     # (or until a termination signal is sent).  This handles signals
     # one-at-a-time time and we'll happily drop signals in case somebody