diff --git a/History.md b/History.md index c4449a81..ea24b467 100644 --- a/History.md +++ b/History.md @@ -5,6 +5,7 @@ * Bugfixes * Cleanup daemonization in rc.d script (#2409) + * Fire `on_booted` after server starts * Refactor * Extract req/resp methods to new request.rb from server.rb (#2419) diff --git a/lib/puma/cluster.rb b/lib/puma/cluster.rb index ff41cf0b..73c4ffe5 100644 --- a/lib/puma/cluster.rb +++ b/lib/puma/cluster.rb @@ -395,9 +395,9 @@ module Puma stop end - @launcher.events.fire_on_booted! - begin + booted = false + while @status == :run begin if @phased_restart @@ -438,6 +438,10 @@ module Puma when "p" w.ping!(result.sub(/^\d+/,'').chomp) @launcher.events.fire(:ping!, w) + if !booted && @workers.none? {|worker| worker.last_status.empty?} + @launcher.events.fire_on_booted! + booted = true + end end else log "! Out-of-sync worker list, no #{pid} worker" diff --git a/lib/puma/cluster/worker.rb b/lib/puma/cluster/worker.rb index c6774ab0..d49b03e3 100644 --- a/lib/puma/cluster/worker.rb +++ b/lib/puma/cluster/worker.rb @@ -103,23 +103,25 @@ module Puma return end - Thread.new(@worker_write) do |io| - Puma.set_thread_name "stat payload" + while restart_server.pop + server_thread = server.run + stat_thread ||= Thread.new(@worker_write) do |io| + Puma.set_thread_name "stat payload" - while true - sleep Const::WORKER_CHECK_INTERVAL - begin - require 'json' - io << "p#{Process.pid}#{server.stats.to_json}\n" - rescue IOError - Thread.current.purge_interrupt_queue if Thread.current.respond_to? :purge_interrupt_queue - break + while true + begin + require 'json' + io << "p#{Process.pid}#{server.stats.to_json}\n" + rescue IOError + Thread.current.purge_interrupt_queue if Thread.current.respond_to? :purge_interrupt_queue + break + end + sleep Const::WORKER_CHECK_INTERVAL end end + server_thread.join end - server.run.join while restart_server.pop - # Invoke any worker shutdown hooks so they can prevent the worker # exiting until any background operations are completed @launcher.config.run_hooks :before_worker_shutdown, index, @launcher.events diff --git a/lib/puma/single.rb b/lib/puma/single.rb index cd4edaf1..150bf543 100644 --- a/lib/puma/single.rb +++ b/lib/puma/single.rb @@ -55,10 +55,11 @@ module Puma log "Use Ctrl-C to stop" redirect_io + server_thread = server.run @launcher.events.fire_on_booted! begin - server.run.join + server_thread.join rescue Interrupt # Swallow it end diff --git a/test/test_cli.rb b/test/test_cli.rb index c5e27967..0d2a3132 100644 --- a/test/test_cli.rb +++ b/test/test_cli.rb @@ -133,8 +133,6 @@ class TestCLI < Minitest::Test assert_equal 2, status["workers"] - # wait until the first status ping has come through - sleep 6 s = UNIXSocket.new @tmp_path s << "GET /stats HTTP/1.0\r\n\r\n" body = s.read @@ -144,6 +142,7 @@ class TestCLI < Minitest::Test ensure if UNIX_SKT_EXIST && HAS_FORK cli.launcher.stop + t.join done = nil until done @@ -152,7 +151,6 @@ class TestCLI < Minitest::Test done = log[/ - Goodbye!/] end - t.join $debugging_hold = false end end