Skip to content

Commit

Permalink
Bail on multiple Puma workers
Browse files Browse the repository at this point in the history
  • Loading branch information
theodorton committed Sep 18, 2024
1 parent 0cdb94e commit 915df22
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 0 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ gemspec
gem "rails", ">= 7.0.1"
gem "rake"
gem "debug"
gem "puma"
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -199,6 +201,7 @@ PLATFORMS
DEPENDENCIES
debug
propshaft!
puma
rails (>= 7.0.1)
rake

Expand Down
36 changes: 36 additions & 0 deletions lib/propshaft/puma_config.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions lib/propshaft/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
29 changes: 29 additions & 0 deletions test/propshaft/puma_config_test.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 915df22

Please sign in to comment.