Chase Gilliam Chase Gilliam - 5 months ago 66
Ruby Question

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?

Answer

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)
Comments