Skip to content

Commit

Permalink
Add on_booted event for single mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcin Olichwirowicz committed Sep 22, 2021
1 parent e3acca8 commit 94e7d64
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/puma/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,18 @@ def after_worker_fork(&block)
@options[:after_worker_fork] << block
end

# Code to run after puma is booted in a single mode
#
# @note Single mode only.
# @example
# on_booted do
# puts 'After booting...'
# end
def on_booted(&block)
@options[:on_booted] ||= []
@options[:on_booted] << block
end

alias_method :after_worker_boot, :after_worker_fork

# When `fork_worker` is enabled, code to run in Worker 0
Expand Down
2 changes: 2 additions & 0 deletions lib/puma/single.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ def run
@server = server = start_server
server_thread = server.run

@launcher.config.run_hooks :on_booted, nil, @launcher.events

log "Use Ctrl-C to stop"
redirect_io

Expand Down
4 changes: 4 additions & 0 deletions test/test_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,10 @@ def test_run_hooks_before_fork
assert_run_hooks :before_fork
end

def test_run_hooks_on_booted
assert_run_hooks :on_booted
end

def test_run_hooks_and_exception
conf = Puma::Configuration.new do |c|
c.on_restart do |a|
Expand Down

0 comments on commit 94e7d64

Please sign in to comment.