Chase
Chase

Reputation: 2826

Run puma workers in Production, but not in Development

I'm running the following puma config

threads_count = Integer(ENV["DB_POOL"] || ENV["MAX_THREADS"] || 15)
threads threads_count, threads_count
workers 3
preload_app!

rackup      DefaultRackup
port        ENV["PORT"]     || 3000
environment ENV["RACK_ENV"] || "development"

on_worker_boot do
  ActiveSupport.on_load(:active_record) do
    ActiveRecord::Base.establish_connection
  end
end

before_fork do
  ActiveRecord::Base.connection_pool.disconnect!
end

It's great for production, but I don't want to spin up 3 workers or use webrick in development. I tried wrapping the worker specific code in an environment check, but that breaks the puma DSL. Any ideas for running puma in non-clustered mode in development?

Upvotes: 3

Views: 4790

Answers (3)

rkrdo
rkrdo

Reputation: 1051

Check out the Configuration part on the docs.

What I did was set up the production config on config/puma/production.rb, so on production you would run puma with puma -C config/puma/production.rb (or however you run it on prod) and on development, rails server won't use that configuration

Upvotes: 0

Chase
Chase

Reputation: 2826

I figure out a working solution before seeing scorix's answer which I accepted, but I ended up with a slightly different solution. This allows you to set the worker count, so I can run 1 in staging and 3 in production for example.

threads_count = Integer(ENV["DB_POOL"] || ENV["MAX_THREADS"] || 15)
threads threads_count, threads_count
rackup      DefaultRackup
port        ENV["PORT"]     || 3000
environment ENV["RACK_ENV"] || "development"

if ENV["RACK_ENV"] == "production"
  workers ENV.fetch("WEB_CONCURRENCY") { 3 }
  preload_app!
  ActiveSupport.on_load(:active_record) do
    ActiveRecord::Base.establish_connection
  end
  before_fork do
    ActiveRecord::Base.connection_pool.disconnect!
  end
end

Upvotes: 3

scorix
scorix

Reputation: 2516

Rails is not defined in puma config file, so Rails.env can't be used here, but RACK_ENV is ok.

workers(ENV["RACK_ENV"] == "production" ? 3 : 0)

Upvotes: 7

Related Questions