From 6d8e889f1836e58478f5d32f2d695869a7756fb3 Mon Sep 17 00:00:00 2001 From: schneems Date: Fri, 8 May 2020 13:32:50 -0500 Subject: [PATCH] Revert api change from #2086 introduce Puma.stats_hash api The change in #2086 is not backwards compatible with existing gems that parse the output of Puma.stats such as barnes. Releasing a version of puma with this change would break anyone using the Barnes app and only in production. I'm proposing to keep the existing interface and instead add a new API. This buys us all the features of #2086 without causing any production facing downtime by customers due to API incompatibilities. Unfortunately it requires that we serialize and the de-serialize the values. One prior benefit of returning json in a string was that it allowed an end user to de-serialize using a faster json algorithm such as `oj` via the "multi json" gem. But the performance penalty will be better than a stability break. --- History.md | 2 +- lib/puma.rb | 4 ++++ test/test_cli.rb | 2 ++ test/test_launcher.rb | 4 ++-- 4 files changed, 9 insertions(+), 3 deletions(-) diff --git a/History.md b/History.md index d319d221b3..7df580dddd 100644 --- a/History.md +++ b/History.md @@ -11,9 +11,9 @@ * Force shutdown responses can be overridden by using the `lowlevel_error_handler` config (#2203) * Faster phased restart and worker timeout (#2121) * New configuration option to set state file permissions (#2238) + * `Puma.stats_hash` returns a stats in Hash instead of a JSON string (#2086, #2253) * Deprecations, Removals and Breaking API Changes - * `Puma.stats` now returns a Hash instead of a JSON string (#2086) * `--control` has been removed. Use `--control-url` (#1487) * `worker_directory` has been removed. Use `directory`. * min_threads now set by environment variables PUMA_MIN_THREADS and MIN_THREADS. (#2143) diff --git a/lib/puma.rb b/lib/puma.rb index cb46f8ca35..bd667116f1 100644 --- a/lib/puma.rb +++ b/lib/puma.rb @@ -20,6 +20,10 @@ def self.stats_object=(val) end def self.stats + @get_stats.stats.to_json + end + + def self.stats_hash @get_stats.stats end diff --git a/test/test_cli.rb b/test/test_cli.rb index eb0f1e98b2..22597f06f7 100644 --- a/test/test_cli.rb +++ b/test/test_cli.rb @@ -55,6 +55,8 @@ def test_control_for_tcp body = s.read s.close + assert_equal Puma.stats_hash, JSON.parse(Puma.stats, symbolize_names: true) + dmt = Puma::Configuration.new.default_max_threads assert_match(/{"started_at":"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z","backlog":0,"running":0,"pool_capacity":#{dmt},"max_threads":#{dmt},"requests_count":0}/, body.split(/\r?\n/).last) diff --git a/test/test_launcher.rb b/test/test_launcher.rb index 53505c3100..39fb7ab88d 100644 --- a/test/test_launcher.rb +++ b/test/test_launcher.rb @@ -139,7 +139,7 @@ def test_puma_stats launcher.events.on_booted {launcher.stop} launcher.run Puma::Server::STAT_METHODS.each do |stat| - assert_includes Puma.stats, stat + assert_includes Puma.stats_hash, stat end end @@ -154,7 +154,7 @@ def test_puma_stats_clustered launcher = launcher(conf) Thread.new do sleep Puma::Const::WORKER_CHECK_INTERVAL + 1 - status = Puma.stats[:worker_status].first[:last_status] + status = Puma.stats_hash[:worker_status].first[:last_status] Puma::Server::STAT_METHODS.each do |stat| assert_includes status, stat end