From c1b6071ae3a3fa642a27d8f5c23f9055eab78287 Mon Sep 17 00:00:00 2001 From: Carlos Castellanos Vera Date: Mon, 2 Nov 2020 16:32:37 +0100 Subject: [PATCH] Log puma config if the env variable LOG_CONFIG exists (#2472) --- History.md | 1 + README.md | 3 +++ lib/puma/configuration.rb | 10 ++++++++++ lib/puma/launcher.rb | 11 +++++++++++ test/test_config.rb | 7 +++++++ test/test_launcher.rb | 16 ++++++++++++++++ 6 files changed, 48 insertions(+) diff --git a/History.md b/History.md index 8ac109ba..516e25a8 100644 --- a/History.md +++ b/History.md @@ -2,6 +2,7 @@ * Features * Your feature goes here (#Github Number) + * Prints the loaded configuration if the environment variable `PUMA_LOG_CONFIG` is present ([#2472]) * Integrate with systemd's watchdog and notification features ([#2438]) * Adds max_fast_inline as a configuration option for the Server object ([#2406]) * You can now fork workers from worker 0 using SIGURG w/o fork_worker enabled [#2449] diff --git a/README.md b/README.md index 784ca8db..c8945c8b 100644 --- a/README.md +++ b/README.md @@ -85,6 +85,9 @@ Puma provides numerous options. Consult `puma -h` (or `puma --help`) for a full You can also find several configuration examples as part of the [test](https://github.com/puma/puma/tree/master/test/config) suite. +For debugging purposes, you can set the environment variable `PUMA_LOG_CONFIG` with a value +and the loaded configuration will be printed as part of the boot process. + ### Thread Pool Puma uses a thread pool. You can set the minimum and maximum number of threads that are available in the pool with the `-t` (or `--threads`) flag: diff --git a/lib/puma/configuration.rb b/lib/puma/configuration.rb index 60842bce..7585f524 100644 --- a/lib/puma/configuration.rb +++ b/lib/puma/configuration.rb @@ -92,6 +92,12 @@ module Puma end end end + + def final_options + default_options + .merge(file_options) + .merge(user_options) + end end # The main configuration class of Puma. @@ -290,6 +296,10 @@ module Puma end end + def final_options + @options.final_options + end + def self.temp_path require 'tmpdir' diff --git a/lib/puma/launcher.rb b/lib/puma/launcher.rb index cce0463f..b690dfa8 100644 --- a/lib/puma/launcher.rb +++ b/lib/puma/launcher.rb @@ -87,6 +87,8 @@ module Puma Puma.stats_object = @runner @status = :run + + log_config if ENV['PUMA_LOG_CONFIG'] end attr_reader :binder, :events, :config, :options, :restart_dir @@ -521,5 +523,14 @@ module Puma Bundler.with_unbundled_env { yield } end end + + def log_config + log "Configuration:" + + @config.final_options + .each { |config_key, value| log "- #{config_key}: #{value}" } + + log "\n" + end end end diff --git a/test/test_config.rb b/test/test_config.rb index de0d8db2..97385cb4 100644 --- a/test/test_config.rb +++ b/test/test_config.rb @@ -265,6 +265,13 @@ class TestConfigFile < TestConfigFileBase assert_equal 0, Puma::Configuration.new.options.default_options[:workers] end + def test_final_options_returns_merged_options + conf = Puma::Configuration.new({ min_threads: 1, max_threads: 2 }, { min_threads: 2 }) + + assert_equal 1, conf.final_options[:min_threads] + assert_equal 2, conf.final_options[:max_threads] + end + private def assert_run_hooks(hook_name, options = {}) diff --git a/test/test_launcher.rb b/test/test_launcher.rb index de145d9c..d765b8dc 100644 --- a/test/test_launcher.rb +++ b/test/test_launcher.rb @@ -158,6 +158,22 @@ class TestLauncher < Minitest::Test launcher.run end + def test_log_config_enabled + ENV['PUMA_LOG_CONFIG'] = "1" + + assert_match(/Configuration:/, launcher.events.stdout.string) + + launcher.config.final_options.each do |config_key, _value| + assert_match(/#{config_key}/, launcher.events.stdout.string) + end + + ENV.delete('PUMA_LOG_CONFIG') + end + + def test_log_config_disabled + refute_match /Configuration:/, launcher.events.stdout.string + end + private def events