简体   繁体   中英

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?

Rails不PUMA配置文件中定义,所以Rails.env在这里不能使用,但RACK_ENV是确定的。

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM