Skip to content

Commit

Permalink
Add on_booted event
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcin Olichwirowicz authored and Marcin Olichwirowicz committed Jan 26, 2023
1 parent b67e6d5 commit 1b7c021
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 1 deletion.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,15 @@ before_fork do
end
```

You can also specify a block to be run after puma is booted using `on_booted`:

```ruby
# config/puma.rb
on_booted do
# configuration here
end
```

### Error handling

If puma encounters an error outside of the context of your application, it will respond with a 500 and a simple
Expand Down
2 changes: 1 addition & 1 deletion lib/puma/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def configure_control_url(command_line_arg)
#

def setup_options
@conf = Configuration.new do |user_config, file_config|
@conf = Configuration.new({}, {events: @events}) do |user_config, file_config|
@parser = OptionParser.new do |o|
o.on "-b", "--bind URI", "URI to bind to (tcp://, unix://, ssl://)" do |arg|
user_config.bind arg
Expand Down
10 changes: 10 additions & 0 deletions lib/puma/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,16 @@ def after_worker_fork(&block)

alias_method :after_worker_boot, :after_worker_fork

# Code to run after puma is booted (works for both: single and clustered)
#
# @example
# on_booted do
# puts 'After booting...'
# end
def on_booted(&block)
@config.options[:events].on_booted(&block)
end

# When `fork_worker` is enabled, code to run in Worker 0
# before all other workers are re-forked from this process,
# after the server has temporarily stopped serving requests
Expand Down
3 changes: 3 additions & 0 deletions test/config/event_on_booted.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
on_booted do
puts "on_booted called"
end
5 changes: 5 additions & 0 deletions test/config/event_on_booted_exit.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
on_booted do
pid = Process.pid
Process.kill :TERM, pid
Process.wait pid
end
10 changes: 10 additions & 0 deletions test/test_integration_cluster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,16 @@ def test_term_suppress
assert_equal 0, status
end

def test_on_booted
cli_server "-w #{workers} -C test/config/event_on_booted.rb -C test/config/event_on_booted_exit.rb test/rackup/hello.ru", no_wait: true

output = []

output << $_ while @server.gets

assert output.any? { |msg| msg == "on_booted called\n" } != nil
end

def test_term_worker_clean_exit
cli_server "-w #{workers} test/rackup/hello.ru"

Expand Down
10 changes: 10 additions & 0 deletions test/test_integration_single.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ def test_term_exit_code
assert_equal 15, status
end

def test_on_booted
cli_server "-C test/config/event_on_booted.rb -C test/config/event_on_booted_exit.rb test/rackup/hello.ru", no_wait: true

output = []

output << $_ while @server.gets

assert output.any? { |msg| msg == "on_booted called\n" } != nil
end

def test_term_suppress
skip_unless_signal_exist? :TERM

Expand Down

0 comments on commit 1b7c021

Please sign in to comment.