From f34618e64b254f7792b476acf2e7eb426aa551a2 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Mon, 13 Apr 2009 11:46:14 -0700 Subject: Expose worker to {before,after}_fork hooks Instead of just worker.nr. This is a configuration file/API change and will break existing configurations. This allows worker.tempfile to be exposed to the hooks so ownership changes can still happen on it. On the other hand, I don't know of many people actually using this feature (or Unicorn). --- lib/unicorn.rb | 4 ++-- lib/unicorn/configurator.rb | 18 +++++++++--------- test/exec/test_exec.rb | 6 +++--- test/unit/test_signals.rb | 2 +- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/lib/unicorn.rb b/lib/unicorn.rb index c1e8fcd..4465109 100644 --- a/lib/unicorn.rb +++ b/lib/unicorn.rb @@ -389,7 +389,7 @@ module Unicorn tempfile = Tempfile.new('') # as short as possible to save dir space tempfile.unlink # don't allow other processes to find or see it worker = Worker.new(worker_nr, tempfile) - @before_fork.call(self, worker.nr) + @before_fork.call(self, worker) pid = fork { worker_loop(worker) } @workers[pid] = worker end @@ -438,7 +438,7 @@ module Unicorn @start_ctx = @workers = @rd_sig = @wr_sig = nil @listeners.each { |sock| sock.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC) } worker.tempfile.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC) - @after_fork.call(self, worker.nr) if @after_fork # can drop perms + @after_fork.call(self, worker) if @after_fork # can drop perms @request = HttpRequest.new(logger) build_app! unless @preload_app end diff --git a/lib/unicorn/configurator.rb b/lib/unicorn/configurator.rb index 3fcb892..548c239 100644 --- a/lib/unicorn/configurator.rb +++ b/lib/unicorn/configurator.rb @@ -12,8 +12,8 @@ module Unicorn # listen '0.0.0.0:9292' # timeout 10 # pid "/tmp/my_app.pid" - # after_fork do |server,worker_nr| - # server.listen("127.0.0.1:#{9293 + worker_nr}") rescue nil + # after_fork do |server,worker| + # server.listen("127.0.0.1:#{9293 + worker.nr}") rescue nil # end class Configurator # The default logger writes its output to $stderr @@ -25,18 +25,18 @@ module Unicorn :listeners => [], :logger => DEFAULT_LOGGER, :worker_processes => 1, - :after_fork => lambda { |server, worker_nr| - server.logger.info("worker=#{worker_nr} spawned pid=#{$$}") + :after_fork => lambda { |server, worker| + server.logger.info("worker=#{worker.nr} spawned pid=#{$$}") # per-process listener ports for debugging/admin: # "rescue nil" statement is needed because USR2 will # cause the master process to reexecute itself and the # per-worker ports can be taken, necessitating another # HUP after QUIT-ing the original master: - # server.listen("127.0.0.1:#{8081 + worker_nr}") rescue nil + # server.listen("127.0.0.1:#{8081 + worker.nr}") rescue nil }, - :before_fork => lambda { |server, worker_nr| - server.logger.info("worker=#{worker_nr} spawning...") + :before_fork => lambda { |server, worker| + server.logger.info("worker=#{worker.nr} spawning...") }, :before_exec => lambda { |server| server.logger.info("forked child re-executing...") @@ -97,13 +97,13 @@ module Unicorn # the worker after forking. The following is an example hook which adds # a per-process listener to every worker: # - # after_fork do |server,worker_nr| + # after_fork do |server,worker| # # per-process listener ports for debugging/admin: # # "rescue nil" statement is needed because USR2 will # # cause the master process to reexecute itself and the # # per-worker ports can be taken, necessitating another # # HUP after QUIT-ing the original master: - # server.listen("127.0.0.1:#{9293 + worker_nr}") rescue nil + # server.listen("127.0.0.1:#{9293 + worker.nr}") rescue nil # end def after_fork(*args, &block) set_hook(:after_fork, block_given? ? block : args[0]) diff --git a/test/exec/test_exec.rb b/test/exec/test_exec.rb index 67a189c..596ab88 100644 --- a/test/exec/test_exec.rb +++ b/test/exec/test_exec.rb @@ -39,8 +39,8 @@ end worker_processes 4 timeout 30 logger Logger.new('#{COMMON_TMP.path}') -before_fork do |server, worker_nr| - server.logger.info "before_fork: worker=\#{worker_nr}" +before_fork do |server, worker| + server.logger.info "before_fork: worker=\#{worker.nr}" end EOS @@ -286,7 +286,7 @@ end File.unlink(tmp.path) ucfg = Tempfile.new('unicorn_test_config') ucfg.syswrite("listen '#@addr:#@port'\n") - ucfg.syswrite("before_fork { |s,nr|\n") + ucfg.syswrite("before_fork { |s,w|\n") ucfg.syswrite(" s.listen('#{tmp.path}', :backlog => 5, :sndbuf => 8192)\n") ucfg.syswrite(" s.listen('#@addr:#{port2}', :rcvbuf => 8192)\n") ucfg.syswrite("\n}\n") diff --git a/test/unit/test_signals.rb b/test/unit/test_signals.rb index 45de263..bedce01 100644 --- a/test/unit/test_signals.rb +++ b/test/unit/test_signals.rb @@ -30,7 +30,7 @@ class SignalsTest < Test::Unit::TestCase tmp.chmod(0) @server_opts = { :listeners => [ "127.0.0.1:#@port", @tmp.path ], - :after_fork => lambda { |server,worker_nr| + :after_fork => lambda { |server,worker| trap(:HUP) { tmp.chmod(n += 1) } }, } -- cgit v1.2.3-24-ge0c7