From fdc17e1736c1d6128c29f4baba2dd86c6ef224c6 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Fri, 27 Mar 2009 00:34:59 -0700 Subject: style: symbols instead of strings for signal names They're easier for me to type and read and just barely faster when doing comparisons on. --- lib/unicorn.rb | 39 +++++++++++++++++++-------------------- test/exec/test_exec.rb | 44 ++++++++++++++++++++++---------------------- 2 files changed, 41 insertions(+), 42 deletions(-) diff --git a/lib/unicorn.rb b/lib/unicorn.rb index b36ae21..24e5549 100644 --- a/lib/unicorn.rb +++ b/lib/unicorn.rb @@ -162,7 +162,7 @@ module Unicorn respawn = true QUEUE_SIGS.each { |sig| trap_deferred(sig) } - trap('CHLD') { |sig_nr| awaken_master } + trap(:CHLD) { |sig_nr| awaken_master } $0 = "unicorn master" logger.info "master process ready" # test_exec.rb relies on this message begin @@ -173,27 +173,27 @@ module Unicorn murder_lazy_workers spawn_missing_workers if respawn master_sleep - when 'QUIT' # graceful shutdown + when :QUIT # graceful shutdown break - when 'TERM', 'INT' # immediate shutdown + when :TERM, :INT # immediate shutdown stop(false) break - when 'USR1' # rotate logs + when :USR1 # rotate logs logger.info "master rotating logs..." Unicorn::Util.reopen_logs logger.info "master done rotating logs" - kill_each_worker('USR1') - when 'USR2' # exec binary, stay alive in case something went wrong + kill_each_worker(:USR1) + when :USR2 # exec binary, stay alive in case something went wrong reexec - when 'WINCH' + when :WINCH if Process.ppid == 1 || Process.getpgrp != $$ respawn = false logger.info "gracefully stopping all workers" - kill_each_worker('QUIT') + kill_each_worker(:QUIT) else logger.info "SIGWINCH ignored because we're not daemonized" end - when 'HUP' + when :HUP respawn = true if @config.config_file load_config! @@ -221,7 +221,7 @@ module Unicorn # Terminates all workers, but does not exit master process def stop(graceful = true) - kill_each_worker(graceful ? 'QUIT' : 'TERM') + kill_each_worker(graceful ? :QUIT : :TERM) timeleft = @timeout step = 0.2 reap_all_workers @@ -229,7 +229,7 @@ module Unicorn sleep(step) reap_all_workers (timeleft -= step) > 0 and next - kill_each_worker('KILL') + kill_each_worker(:KILL) end ensure self.listeners = [] @@ -238,8 +238,7 @@ module Unicorn private # list of signals we care about and trap in master. - QUEUE_SIGS = - %w(WINCH QUIT INT TERM USR1 USR2 HUP).map { |x| x.freeze }.freeze + QUEUE_SIGS = [ :WINCH, :QUIT, :INT, :TERM, :USR1, :USR2, :HUP ].freeze # defer a signal for later processing in #join (master process) def trap_deferred(signal) @@ -350,7 +349,7 @@ module Unicorn @workers.each_pair do |pid, worker| (now - worker.tempfile.ctime) <= @timeout and next logger.error "worker=#{worker.nr} PID:#{pid} is too old, killing" - kill_worker('KILL', pid) # take no prisoners for @timeout violations + kill_worker(:KILL, pid) # take no prisoners for @timeout violations worker.tempfile.close rescue nil end end @@ -363,7 +362,7 @@ module Unicorn Dir.chdir(@start_ctx[:cwd]) rescue Errno::ENOENT => err logger.fatal "#{err.inspect} (#{@start_ctx[:cwd]})" - @sig_queue << 'QUIT' # forcibly emulate SIGQUIT + @sig_queue << :QUIT # forcibly emulate SIGQUIT return end tempfile = Tempfile.new('') # as short as possible to save dir space @@ -405,7 +404,7 @@ module Unicorn build_app! unless @preload_app @sig_queue.clear QUEUE_SIGS.each { |sig| trap(sig, 'IGNORE') } - trap('CHLD', 'DEFAULT') + trap(:CHLD, 'DEFAULT') $0 = "unicorn worker[#{worker.nr}]" @rd_sig.close if @rd_sig @@ -430,13 +429,13 @@ module Unicorn alive = true ready = @listeners client = nil - %w(TERM INT).each { |sig| trap(sig) { exit(0) } } # instant shutdown - trap('QUIT') do + [:TERM, :INT].each { |sig| trap(sig) { exit(0) } } # instant shutdown + trap(:QUIT) do alive = false @listeners.each { |sock| sock.close rescue nil } # break IO.select end reopen_logs, (rd, wr) = false, IO.pipe - trap(:USR1) { reopen_logs = true; rd.close rescue nil } + trap(:USR1) { reopen_logs = true; rd.close rescue nil } # break IO.select @logger.info "worker=#{worker.nr} ready" while alive && @master_pid == Process.ppid @@ -549,7 +548,7 @@ module Unicorn logger.info "reloading config_file=#{@config.config_file}" @config.reload @config.commit!(self) - kill_each_worker('QUIT') + kill_each_worker(:QUIT) logger.info "done reloading config_file=#{@config.config_file}" rescue Object => e logger.error "error reloading config_file=#{@config.config_file}: " \ diff --git a/test/exec/test_exec.rb b/test/exec/test_exec.rb index 53cebf6..caada3b 100644 --- a/test/exec/test_exec.rb +++ b/test/exec/test_exec.rb @@ -27,7 +27,7 @@ rescue LoadError end class ExecTest < Test::Unit::TestCase - trap('QUIT', 'IGNORE') + trap(:QUIT, 'IGNORE') HI = <<-EOS use Rack::ContentLength @@ -126,14 +126,14 @@ end wait_for_file(pid_file) Process.waitpid(pid) - Process.kill('USR2', File.read(pid_file).to_i) + Process.kill(:USR2, File.read(pid_file).to_i) wait_for_file(old_file) wait_for_file(pid_file) - Process.kill('QUIT', File.read(old_file).to_i) + Process.kill(:QUIT, File.read(old_file).to_i) ucfg.syswrite("timeout %(#{pid_file})\n") # introduce a bug current_pid = File.read(pid_file).to_i - Process.kill('USR2', current_pid) + Process.kill(:USR2, current_pid) # wait for pid_file to restore itself tries = DEFAULT_TRIES @@ -158,7 +158,7 @@ end ucfg.truncate(0) ucfg.syswrite("listeners %w(#{@addr}:#{@port} #{@addr}:#{port2})\n") ucfg.syswrite("pid %(#{pid_file})\n") - Process.kill('USR2', current_pid) + Process.kill(:USR2, current_pid) wait_for_file(old_file) wait_for_file(pid_file) new_pid = File.read(pid_file).to_i @@ -170,8 +170,8 @@ end assert_equal String, results[1].class assert_nothing_raised do - Process.kill('QUIT', current_pid) - Process.kill('QUIT', new_pid) + Process.kill(:QUIT, current_pid) + Process.kill(:QUIT, new_pid) end end @@ -192,14 +192,14 @@ end wait_for_file(pid_file) Process.waitpid(pid) - Process.kill('USR2', File.read(pid_file).to_i) + Process.kill(:USR2, File.read(pid_file).to_i) wait_for_file(old_file) wait_for_file(pid_file) - Process.kill('QUIT', File.read(old_file).to_i) + Process.kill(:QUIT, File.read(old_file).to_i) File.unlink("config.ru") # break reloading current_pid = File.read(pid_file).to_i - Process.kill('USR2', current_pid) + Process.kill(:USR2, current_pid) # wait for pid_file to restore itself tries = DEFAULT_TRIES @@ -220,7 +220,7 @@ end # fix the bug File.open("config.ru", "wb") { |fp| fp.syswrite(HI) } - Process.kill('USR2', current_pid) + Process.kill(:USR2, current_pid) wait_for_file(old_file) wait_for_file(pid_file) new_pid = File.read(pid_file).to_i @@ -230,8 +230,8 @@ end assert_equal String, results[0].class assert_nothing_raised do - Process.kill('QUIT', current_pid) - Process.kill('QUIT', new_pid) + Process.kill(:QUIT, current_pid) + Process.kill(:QUIT, new_pid) end end @@ -289,7 +289,7 @@ end rotate = Tempfile.new('unicorn_rotate') assert_nothing_raised do File.rename(COMMON_TMP.path, rotate.path) - Process.kill('USR1', pid) + Process.kill(:USR1, pid) end wait_for_file(COMMON_TMP.path) assert File.exist?(COMMON_TMP.path), "#{COMMON_TMP.path} exists" @@ -311,7 +311,7 @@ end end assert_equal 4, log.grep(/worker=\d+ done rotating logs/).size assert_equal 0, log.grep(/rotating logs\.\.\./).size - assert_nothing_raised { Process.kill('QUIT', pid) } + assert_nothing_raised { Process.kill(:QUIT, pid) } status = nil assert_nothing_raised { pid, status = Process.waitpid2(pid) } assert status.success?, "exited successfully" @@ -430,7 +430,7 @@ end ucfg.syswrite("pid \"#{pid_file}\"\n") ucfg.syswrite("logger Logger.new('#{new_log.path}')\n") ucfg.close - Process.kill('HUP', pid) + Process.kill(:HUP, pid) end wait_for_file(new_sock_path) @@ -491,7 +491,7 @@ end def assert_shutdown(pid) wait_master_ready("#{@tmpdir}/test_stderr.#{pid}.log") - assert_nothing_raised { Process.kill('QUIT', pid) } + assert_nothing_raised { Process.kill(:QUIT, pid) } status = nil assert_nothing_raised { pid, status = Process.waitpid2(pid) } assert status.success?, "exited successfully" @@ -527,7 +527,7 @@ end def reexec_usr2_quit_test(pid, pid_file) assert File.exist?(pid_file), "pid file OK" assert ! File.exist?("#{pid_file}.oldbin"), "oldbin pid file" - assert_nothing_raised { Process.kill('USR2', pid) } + assert_nothing_raised { Process.kill(:USR2, pid) } assert_nothing_raised { retry_hit(["http://#{@addr}:#{@port}/"]) } wait_for_file("#{pid_file}.oldbin") wait_for_file(pid_file) @@ -535,12 +535,12 @@ end # kill old master process assert_not_equal pid, File.read(pid_file).to_i assert_equal pid, File.read("#{pid_file}.oldbin").to_i - assert_nothing_raised { Process.kill('QUIT', pid) } + assert_nothing_raised { Process.kill(:QUIT, pid) } assert_not_equal pid, File.read(pid_file).to_i assert_nothing_raised { retry_hit(["http://#{@addr}:#{@port}/"]) } wait_for_file(pid_file) assert_nothing_raised { retry_hit(["http://#{@addr}:#{@port}/"]) } - assert_nothing_raised { Process.kill('QUIT', File.read(pid_file).to_i) } + assert_nothing_raised { Process.kill(:QUIT, File.read(pid_file).to_i) } end def reexec_basic_test(pid, pid_file) @@ -555,7 +555,7 @@ end assert_nothing_raised do nr.times do |i| hit(["http://#{@addr}:#{@port}/#{i}"]) - i == kill_point and Process.kill('HUP', pid) + i == kill_point and Process.kill(:HUP, pid) end end wait_master_ready(master_log) @@ -563,7 +563,7 @@ end new_pid = File.read(pid_file).to_i assert_not_equal pid, new_pid assert_nothing_raised { Process.kill(0, new_pid) } - assert_nothing_raised { Process.kill('QUIT', new_pid) } + assert_nothing_raised { Process.kill(:QUIT, new_pid) } end def wait_for_file(path) -- cgit v1.2.3-24-ge0c7