about summary refs log tree commit homepage
diff options
context:
space:
mode:
-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