puma initializer does not work with rails 4.2 - ruby-on-rails

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

Related

Sinatra app in Rails 4.1 config

I want to fake an API with a little Sinatra app, following this method. Meaning I have a Rails app, and in the spec/support folder, a very simple Sinatra app:
module FakePrograms
class Application < Sinatra::Base
get "/API/V1/programs" do
{
programs: [
...
]
}.to_json
end
end
end
Part of the goal is to launch this app locally, so I can work on my Rails app with a fake API. Problem: when I do ruby spec/support/fake_programs.rb, the app can't boot, and I get a
config/puma.rb:14:in `block in _load_from':
uninitialized constant
#<Class:#<Puma::DSL:0x007fac0b0e0380>>::ActiveRecord (NameError)
Looks like Sinatra is booting using my Rails configuration. I don't need ActiveRecord nor Puma for my fake API.
I've read this question and this other one, but theese are in different context, because they need their Sinatra app to share routes with the Rails app.
Content of 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
# 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 was looking at the config.ru file, but #max is right, it's not correlated.
The Puma README says:
By default, if no configuration file is specified, Puma will look for a configuration file at config/puma.rb.
Which is why your app is using this config file.
It goes on to say:
If you want to prevent Puma from looking for a configuration file in those locations, provide a dash as the argument to the -C (or --config) flag:
$ puma -C "-"
Since Puma can handle Sinatra apps directly, you could start your app like this:
$ puma -c "-" spec/support/fake_programs.rb
If you want to start your app using ruby with Puma as the built in server (as you are doing now), I think adding this to your Sinatra app should work (neither Sinatra’s :server_settings or Puma’s :config_files are very well documented):
set :server, 'puma'
set :server_settings, {:config_files => "-"}

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.

RoR in Windows not working for pre-existing app built with RoR in linux

I've installed Ruby on Rails using RailsInstaller and also postgresql in Windows 8. I'm trying to run rails server using files for a pre existing app but I'm getting the error 'Worker mode not supported on JRuby or Windows'.
In my config/puma.rb file i've set workers to 0, then get an error about daemon mode not supported on windows. basically each time i change something i get more errors.
I've fixed up environment variables, gems, etc (like in other posts) such as this Cannot install Puma gem on Ruby on Rails. there any hope of running a pre-existing RoR app built in linux on a windows machine?
When I run rails server for the RoR 'blog' example it works fine, so I know that RoR is definitely working in windows!
This is my -'de-identified' config/puma.rb file. Is it because in windows I have no /var/app folder?? I've played around with directories etc to no avail.
`
#!/usr/bin/env puma
# start puma with:
# RAILS_ENV=production bundle exec puma -C ./config/puma.rb
workers 0
theident = 'nameofthing'
application_path = '/var/app/'+ theident + '.address.com.au/current'
railsenv = 'production'
directory application_path
environment railsenv
daemonize false
pidfile "#{application_path}/tmp/pids/puma-#{railsenv}.pid"
state_path "#{application_path}/tmp/pids/puma-#{railsenv}.state"
stdout_redirect"#{application_path}/log/puma-#{theident}.log"
threads 0, 16
bind "unix:///var/run/puma/" + theident + "_app.sock" `
I have changed those directories to the current path and now running 'rails server' starts going , but localhost:3000 is a page not working. I am getting errors around SIGUSR1 not working , SIGUSR2 not working ,etc
The "workers" method isn't supported for JRuby nor Windows, so the best solution would be to remove the line from puma.rb causing the error. In my case I removed;
workers Integer(ENV['WEB_CONCURRENCY'] || 2)
So remaining I have;
threads_count = Integer(ENV['RAILS_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
It might be different for you, but the specific line will start with "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.

Running different rails webserver as per choice

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.

Resources