diff --git a/Gemfile b/Gemfile index 29f02f3..c9abd1a 100644 --- a/Gemfile +++ b/Gemfile @@ -5,3 +5,4 @@ gemspec gem "rails", ">= 7.0.1" gem "rake" gem "debug" +gem "puma" diff --git a/Gemfile.lock b/Gemfile.lock index 06d661f..8ec78b2 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -134,6 +134,8 @@ GEM racc (~> 1.4) psych (5.1.2) stringio + puma (6.4.2) + nio4r (~> 2.0) racc (1.8.1) rack (3.1.7) rack-session (2.0.0) @@ -199,6 +201,7 @@ PLATFORMS DEPENDENCIES debug propshaft! + puma rails (>= 7.0.1) rake diff --git a/lib/propshaft/puma_config.rb b/lib/propshaft/puma_config.rb new file mode 100644 index 0000000..832a7b3 --- /dev/null +++ b/lib/propshaft/puma_config.rb @@ -0,0 +1,36 @@ +begin + require "puma" + require "puma/configuration" +rescue LoadError +end + +class Propshaft::PumaConfig + def initialize + if defined?(Puma) + @resolved_config = Puma::Configuration.new.load.final_options + end + end + + def multiple_workers? + return false unless @resolved_config + @resolved_config[:workers] > 1 + end + + def self.perform_dev_check! + return true unless new.multiple_workers? + + message = "#" * 80 + "\n" + message += "# " + " " * 76 + " #\n" + message += "# " + "WARNING!!".center(76) + " #\n" + message += "# " + " " * 76 + " #\n" + message += "# Running multiple Puma workers in development is not supported with Propshaft #\n" + message += "# " + " " * 76 + " #\n" + message += "# " + "Make sure WEB_CONCURRENCY is not set or ensure".center(76) + " #\n" + message += "# " + "workers is not set for development in config/puma.rb.".center(76) + " #\n" + message += "# " + " " * 76 + " #\n" + message += "#" * 80 + "\n" + + puts message + false + end +end diff --git a/lib/propshaft/railtie.rb b/lib/propshaft/railtie.rb index 8f62e2e..4834db2 100644 --- a/lib/propshaft/railtie.rb +++ b/lib/propshaft/railtie.rb @@ -2,6 +2,7 @@ require "active_support/ordered_options" require "propshaft" require "propshaft/quiet_assets" +require "propshaft/puma_config" module Propshaft class Railtie < ::Rails::Railtie @@ -43,6 +44,10 @@ class Railtie < ::Rails::Railtie app.routes.prepend do mount app.assets.server, at: app.assets.config.prefix end + + if Rails.env.development? + exit 1 unless PumaConfig.perform_dev_check! + end end ActiveSupport.on_load(:action_view) do diff --git a/test/propshaft/puma_config_test.rb b/test/propshaft/puma_config_test.rb new file mode 100644 index 0000000..74835b1 --- /dev/null +++ b/test/propshaft/puma_config_test.rb @@ -0,0 +1,29 @@ +require "test_helper" +require "puma" +require "propshaft/puma_config" +require "minitest/mock" + +class Propshaft::PumaConfigTest < ActiveSupport::TestCase + class PumaConfigurationStub + def initialize(workers) + @workers = workers + end + + def load + OpenStruct.new(final_options: { workers: @workers }) + end + end + + { + 0 => false, + 1 => false, + 2 => true + }.each_pair do |workers, expected| + test "multiple_workers? is #{expected} when config resolves to #{workers}" do + mock = PumaConfigurationStub.new(workers) + Puma::Configuration.stub :new, mock do + assert Propshaft::PumaConfig.new.multiple_workers? == expected + end + end + end +end