Running different rails webserver as per choice - ruby-on-rails

now by default whenever I run rails s it starts rails with puma
I want to able to run webrick in my development environment as puma doesn't work well when debugging with byebug
# Gemfile
gem 'puma'
# config/puma.rb
workers Integer(ENV['WEB_CONCURRENCY'] || 2)
threads_count = Integer(ENV['MAX_THREADS'] || 5)
threads threads_count, threads_count
preload_app!
rackup DefaultRackup
port ENV['PORT'] || 3000
environment ENV['RACK_ENV'] || 'development'
on_worker_boot do
ActiveRecord::Base.establish_connection
end

You can achieve this by restricting the puma gem to production (and maybe test too if it suits your needs) environments only.
# Gemfile
group :test, :production do
gem 'puma'
end
Then run bundle install --without test production.

Related

Clustered puma not accepting more than 1 request concurrently

I have setup my puma web server based on heroku manual: Deploying Rails Applications with the Puma Web Server
config/puma.rb:
workers Integer(ENV['WEB_CONCURRENCY'] || 2)
threads_count = Integer(ENV['RAILS_MAX_THREADS'] || 2)
threads threads_count, threads_count
preload_app!
rackup DefaultRackup
port ENV['PORT'] || 3000
environment ENV['RACK_ENV'] || 'development'
on_worker_boot do
ActiveRecord::Base.establish_connection
end
Procfile contains:
web: bundle exec puma -C config/puma.rb
Now when I try to access action which has sleep 10 which delays the response and then try to access some other action, my understanding was that Puma would handle these 2 requests concurrently (since I have 2 workers). But instead it waits for the first request which sleeps to finish then it proceeds to the second one. Am i missing something?
My setup is:
Ruby 2.2.4
Rails 4.2.0
EDIT:
Ok, so I found out that on Heroku, it works, so question is, why it doesn't work in development mode?
rails s does not use the Procfile, you may want to look at using a tool like foreman: https://github.com/ddollar/foreman
update: this was the solution:
config.allow_concurrency = true in the rails development config.

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

Setting Number of Puma Workers on Heroku

Why do I have 4 Puma workers if I've set WEB_CONCURRENCY=3 in my Heroku config?
In this question, I learned that New Relic calls Puma workers "app instances".
Here is my puma.rb configuration:
workers Integer(ENV['WEB_CONCURRENCY'] || 2)
threads_count = Integer(ENV['MAX_THREADS'] || 1)
threads threads_count, threads_count
preload_app!
rackup DefaultRackup
port ENV['PORT'] || 3000
environment ENV['RACK_ENV'] || 'development'
on_worker_boot do
# Valid on Rails 4.1+ using the `config/database.yml` method of setting `pool` size
ActiveRecord::Base.establish_connection
end
Heroku config:
WEB_CONCURRENCY: 3
Puma has one master process.
It does not deal with request. It monitors and manages(restart or something) workers.
If you set 3 concurrency, there are 4 process. 3 workers(managing request) and 1 master precess(managing workers)

Prepard statement already exist issue - puma incorrect config

I am using rails 4 with puma as my web server. I am using postgres on heroku
I am getting error saying that prepared statement already 'a3' exist
This is my conf for puma
# REF https://devcenter.heroku.com/articles/deploying-rails-applications-with-the-puma-web-server
workers Integer(ENV['WEB_CONCURRENCY'] || 2)
threads_count = Integer(ENV['MAX_THREADS'] || 5)
threads threads_count, threads_count
preload_app!
rackup DefaultRackup
port ENV['PORT'] || 3000
environment ENV['RACK_ENV'] || 'development'
on_worker_boot do
# Worker specific setup for Rails 4.1+
# See: https://devcenter.heroku.com/articles/deploying-rails-applications-with-the-puma-web-server#on-worker-boot
ActiveRecord::Base.establish_connection
end
I think my config for ActiveRecord connection is causing a problem
This might help but I don't know how to do it for puma Postgres: prepared statement already exists
The rails issue linked from the question you referred to in your question has some suggestions.
The most popular seems to be removing the rack-timeout gem if it is included in your Gemfile. Does sound like a guaranteed solution but has helped someone people on that thread.

puma initializer does not work with rails 4.2

I have installed puma many times before and have never had this problem. I am following heroku's instructions verbatim. I have created a Procfile with this inside:
web: bundle exec puma -C config/puma.rb
Here is the configuration file puma.rb:
workers Integer(ENV['WEB_CONCURRENCY'] || 2)
threads_count = Integer(ENV['MAX_THREADS'] || 5)
threads threads_count, threads_count
preload_app!
rackup DefaultRackup
port ENV['PORT'] || 3000
environment ENV['RACK_ENV'] || 'development'
on_worker_boot do
# Worker specific setup for Rails 4.1+
# See: https://devcenter.heroku.com/articles/deploying-rails-applications-with-the-puma-web-server#on-worker-boot
ActiveRecord::Base.establish_connection
end
When I start the server by running rails s or rails s puma i get
=> Booting Puma
=> Rails 4.2.0 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
Exiting
/Users/david/projects/abc/config/initializers/puma.rb:1:in `<top (required)>': undefined method `workers' for main:Object (NoMethodError)
What is happening and how do I fix it? I am using sqlite3 in development and postgresql in production but i get the same error in both environments. I am using rails 4.2.0 and ruby 2.2.0.
You need to have gem 'puma' in your Gemfile
Edit: The puma.rb should not be located in the initializers folder, but outside in the config folder. Thanks Nicolai
The puma.rb should not be located in the initializers folder, but outside in the config folder

Resources