diff --git a/docs/signals.md b/docs/signals.md index 47b057b5..5c2bb411 100644 --- a/docs/signals.md +++ b/docs/signals.md @@ -38,5 +38,6 @@ Puma cluster responds to these signals: - `TERM` send `TERM` to worker. Worker will attempt to finish then exit. - `USR2` restart workers - `USR1` restart workers in phases, a rolling restart. +- `HUP` reopen log files defined in stdout_redirect configuration parameter - `INT` equivalent of sending Ctrl-C to cluster. Will attempt to finish then exit. - `CHLD` diff --git a/lib/puma/cli.rb b/lib/puma/cli.rb index cb3fbda1..ca798f72 100644 --- a/lib/puma/cli.rb +++ b/lib/puma/cli.rb @@ -537,6 +537,14 @@ module Puma log "*** SIGTERM not implemented, signal based gracefully stopping unavailable!" end + begin + Signal.trap "SIGHUP" do + redirect_io + end + rescue Exception + log "*** SIGHUP not implemented, signal based logs reopening unavailable!" + end + if jruby? Signal.trap("INT") do @status = :exit @@ -564,6 +572,10 @@ module Puma true end + def redirect_io + @runner.redirect_io + end + def stats @runner.stats end diff --git a/lib/puma/cluster.rb b/lib/puma/cluster.rb index c4ec1501..526eeecb 100644 --- a/lib/puma/cluster.rb +++ b/lib/puma/cluster.rb @@ -36,6 +36,12 @@ module Puma end end + def redirect_io + super + + @workers.each { |x| x.hup } + end + class Worker def initialize(idx, pid, phase) @index = idx @@ -82,6 +88,11 @@ module Puma Process.kill "KILL", @pid rescue Errno::ESRCH end + + def hup + Process.kill "HUP", @pid + rescue Errno::ESRCH + end end def spawn_workers @@ -104,9 +115,9 @@ module Puma end def next_worker_index - all_positions = 0...@options[:workers] + all_positions = 0...@options[:workers] occupied_positions = @workers.map { |w| w.index } - available_positions = all_positions.to_a - occupied_positions + available_positions = all_positions.to_a - occupied_positions available_positions.first end @@ -172,6 +183,7 @@ module Puma $0 = "puma: cluster worker #{index}: #{master}" Signal.trap "SIGINT", "IGNORE" + @workers = [] @master_read.close @suicide_pipe.close