Skip to content

Commit

Permalink
Log puma config if the env variable LOG_CONFIG exists
Browse files Browse the repository at this point in the history
  • Loading branch information
karloscodes committed Nov 1, 2020
1 parent 61c6213 commit bb774d5
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions History.md
Expand Up @@ -2,6 +2,7 @@

* Features
* Your feature goes here <Most recent on the top, like GitHub> (#Github Number)
* Prints the loaded configuration if the environment variable `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]
Expand Down
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -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 `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:
Expand Down
10 changes: 10 additions & 0 deletions lib/puma/configuration.rb
Expand Up @@ -92,6 +92,12 @@ def finalize_values
end
end
end

def final_options
default_options
.merge(file_options)
.merge(user_options)
end
end

# The main configuration class of Puma.
Expand Down Expand Up @@ -290,6 +296,10 @@ def run_hooks(key, arg, events)
end
end

def final_options
@options.final_options
end

def self.temp_path
require 'tmpdir'

Expand Down
11 changes: 11 additions & 0 deletions lib/puma/launcher.rb
Expand Up @@ -87,6 +87,8 @@ def initialize(conf, launcher_args={})
Puma.stats_object = @runner

@status = :run

log_config if ENV['LOG_CONFIG']
end

attr_reader :binder, :events, :config, :options, :restart_dir
Expand Down Expand Up @@ -521,5 +523,14 @@ def with_unbundled_env
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
7 changes: 7 additions & 0 deletions test/test_config.rb
Expand Up @@ -265,6 +265,13 @@ def test_config_does_not_load_workers_by_default
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 = {})
Expand Down
16 changes: 16 additions & 0 deletions test/test_launcher.rb
Expand Up @@ -158,6 +158,22 @@ def test_puma_stats_clustered
launcher.run
end

def test_log_config_enabled
ENV['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('LOG_CONFIG')
end

def test_log_config_disabled
refute_match /Configuration:/, launcher.events.stdout.string
end

private

def events
Expand Down

0 comments on commit bb774d5

Please sign in to comment.