From 5a842b0621516d4a74dadd391b495eaa6ec60744 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Mon, 9 Feb 2009 21:42:07 -0800 Subject: add add_listener method for use in configs 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. --- lib/unicorn.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) 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 -- cgit v1.2.3-24-ge0c7