From e9be891dc851015eaf592d3c64caff77398f1246 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Thu, 19 Mar 2009 18:50:39 -0700 Subject: Trap WINCH to QUIT children without respawning This will only be enabled if we're daemonized and "real" WINCH signals cannot be generated by resizing the terminal. This is to avoid confusing developers who run in the foreground of a terminal. Additionally document procedures for reexecuting a running binary. --- lib/unicorn.rb | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/unicorn.rb b/lib/unicorn.rb index 7b7c4bb..9245f53 100644 --- a/lib/unicorn.rb +++ b/lib/unicorn.rb @@ -161,6 +161,7 @@ module Unicorn @rd_sig, @wr_sig = IO.pipe unless (@rd_sig && @wr_sig) @rd_sig.nonblock = @wr_sig.nonblock = true ready = mode = nil + respawn = true QUEUE_SIGS.each { |sig| trap_deferred(sig) } trap('CHLD') { |sig_nr| awaken_master } @@ -172,7 +173,7 @@ module Unicorn case (mode = @sig_queue.shift) when nil murder_lazy_workers - spawn_missing_workers + spawn_missing_workers if respawn when 'QUIT' # graceful shutdown break when 'TERM', 'INT' # immediate shutdown @@ -183,7 +184,16 @@ module Unicorn Unicorn::Util.reopen_logs when 'USR2' # exec binary, stay alive in case something went wrong reexec + when 'WINCH' + if ppid == 1 || getpgrp != $$ + respawn = false + logger.info "gracefully stopping all workers" + kill_each_worker('QUIT') + else + logger.info "SIGWINCH ignored because we're not daemonized" + end when 'HUP' + respawn = true if @config.config_file load_config! redo # immediate reaping since we may have QUIT workers @@ -239,7 +249,8 @@ module Unicorn private # list of signals we care about and trap in master. - QUEUE_SIGS = %w(QUIT INT TERM USR1 USR2 HUP).map { |x| x.freeze }.freeze + QUEUE_SIGS = + %w(WINCH QUIT INT TERM USR1 USR2 HUP).map { |x| x.freeze }.freeze # defer a signal for later processing in #join (master process) def trap_deferred(signal) @@ -271,6 +282,7 @@ module Unicorn logger.error "reaped exec()-ed PID:#{pid} status=#{$?.exitstatus}" @reexec_pid = 0 self.pid = @pid.chomp('.oldbin') if @pid + $0 = "unicorn master" else worker = @workers.delete(pid) worker.tempfile.close rescue nil @@ -324,6 +336,7 @@ module Unicorn @before_exec.call(self) if @before_exec exec(*cmd) end + $0 = "unicorn master (old)" end # forcibly terminate all workers that haven't checked in in @timeout -- cgit v1.2.3-24-ge0c7