Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fire on_booted after server starts #2431

Merged
merged 1 commit into from
Oct 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions History.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 6 additions & 2 deletions lib/puma/cluster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -395,9 +395,9 @@ def run
stop
end

@launcher.events.fire_on_booted!

begin
booted = false

while @status == :run
begin
if @phased_restart
Expand Down Expand Up @@ -438,6 +438,10 @@ def run
when "p"
w.ping!(result.sub(/^\d+/,'').chomp)
@launcher.events.fire(:ping!, w)
if !booted && @workers.none? {|worker| worker.last_status.empty?}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this conditional kind of hurts my head but I don't have a better suggestion

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extract logic to helper method with clever name?

@launcher.events.fire_on_booted!
booted = true
end
end
else
log "! Out-of-sync worker list, no #{pid} worker"
Expand Down
28 changes: 15 additions & 13 deletions lib/puma/cluster/worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,23 +103,25 @@ def run
return
end

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 restart_server.pop
server_thread = server.run
stat_thread ||= Thread.new(@worker_write) do |io|
Puma.set_thread_name "stat payload"

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
Expand Down
3 changes: 2 additions & 1 deletion lib/puma/single.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@ def run
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
Expand Down
4 changes: 1 addition & 3 deletions test/test_cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,6 @@ def test_control_clustered

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
Expand All @@ -144,6 +142,7 @@ def test_control_clustered
ensure
if UNIX_SKT_EXIST && HAS_FORK
cli.launcher.stop
t.join

done = nil
until done
Expand All @@ -152,7 +151,6 @@ def test_control_clustered
done = log[/ - Goodbye!/]
end

t.join
$debugging_hold = false
end
end
Expand Down