Rails on development mode without Puma - ruby-on-rails

After I have installed Puma for production mode, it should not run on my local machine, however Puma is starting on development mode and stops after a moment with no errors.
$ rails server
=> Booting Puma
=> Rails 4.2.2 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[8707] Puma starting in cluster mode...
[8707] * Version 3.1.0 (ruby 2.3.0-p0), codename: El Niño Winter Wonderland
[8707] * Min threads: 1, max threads: 6
[8707] * Environment: development
[8707] * Process workers: 1
[8707] * Phased restart available
[8707] * Listening on tcp://localhost:3000
[8707] Use Ctrl-C to stop
It looks like is a bundler issue:
github.com/puma/puma/issues/983

It's not a real solution but a nice work around for people that using there server for production mode with Puma and want to work on local machine development mode with WEBrick. This solution base on mrvncaragay idea
1.
split you Gemfile to 3 files:
Gemfile_base
Gemfile_development
Gemfile_production
in Gemfile_base include all the gems that not test, development & production. There is not reason to include source 'https://rubygems.org' or in Gemfile_development or Gemfile_production file.
in Gemfile_development include only test & development gems
in Gemfile_production include only production gems
2.
Replace all the line in the Gemfile to:
source 'https://rubygems.org'
gemfiles = [ 'Gemfile_base', 'Gemfile_development' ]
#gemfiles = [ 'Gemfile_base', 'Gemfile_production' ]
gemfiles.each do |gemfile|
instance_eval File.read(gemfile)
end
3.
Deploy to production server
4.
Add Gemfile to .gitignore file
#bundle Puma in development mode bad wordaround
Gemfile
5.
Untrack Gemfile from source control
git rm --cached Gemfile
6.
Change the commit line in of Gemfile in production server from:
source 'https://rubygems.org'
gemfiles = [ 'Gemfile_base', 'Gemfile_development' ]
#gemfiles = [ 'Gemfile_base', 'Gemfile_production' ]
gemfiles.each do |gemfile|
instance_eval File.read(gemfile)
end
to:
source 'https://rubygems.org'
#gemfiles = [ 'Gemfile_base', 'Gemfile_development' ]
gemfiles = [ 'Gemfile_base', 'Gemfile_production' ]
gemfiles.each do |gemfile|
instance_eval File.read(gemfile)
end

Related

Puma Rails server won't start in daemon mode on MacOS 13 Ventura after update from MacOS 12

I just updated from MacOS 12.x to 13.0.1
Starting a Rails app with
➜ rails s
works fine
=> Booting Puma
=> Rails 5.2.8.1 application starting in development
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.12.6 (ruby 2.6.6-p146), codename: Llamas in Pajamas
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://localhost:3000
Use Ctrl-C to stop
But when I try with
➜ rails s -d
Output stops at
=> Booting Puma
=> Rails 5.2.8.1 application starting in development
=> Run `rails server -h` for more startup options
And no server is started
➜ ps -ef | grep puma
501 69877 51917 0 6:45 ttys000 0:00.00 grep puma
➜ ps -ef | grep rails
501 72493 51917 0 6:49 ttys000 0:00.00 grep rails
Well, I found a workaround. This is by no means perfect but if it helps anyone with the same problem:
nohup rails s </dev/null >/dev/null 2>&1 &
The & at the end will make it run in the background. All output/input goes to/from /dev/null and nohup makes sure it will run even after you quit the session.
The reason has nothing to do with the fact that you updated to MacOS Ventura.
Puma daemonization has been removed without replacement since version 5.0.0, and it has been extracted to the puma-daemon gem, which currently works only with Puma version ~> 5.
Therefore now you have two ways to make it work: with puma-daemon, or put it in no hanghup background mode.
METHOD 1: USING PUMA DAEMON GEM
bundle add puma-daemon
Add these line to your application’s Gemfile:
gem 'puma-daemon', require: false
gem 'puma', '~> 5'
Add these lines to config/puma.rb:
require 'puma/daemon'
daemonize
and ensure you have set up at least one worker (workers 1)
Now you should be able to run puma webserver as a daemon with rails server (please notice that you must remove any -d or --daemonize from the command line)
PUT IT IN NO HANGHUP BACKGROUND MODE
Just run rails server --no-log-to-stdout & to put the server in background and stop the output, and enclose it in a nohup >/dev/null command to get rid of the nohup.log file:
nohup rails server --no-log-to-stdout >/dev/null &
This method works with any Puma version and doesn't require the gem.
Don't mess around with the two ways.

How to configure rails with puma on AWS elastic beanstalk app?

I have an EBS app using Ruby and Puma, in my rails app, I have puma.rb in /config/puma.rb as:
workers 2
threads 1, 6
app_dir = File.expand_path("../..", __FILE__)
# Default to production
rails_env = ENV['RAILS_ENV'] || "production"
environment rails_env
activate_control_app
# Allow puma to be restarted by `rails restart` command.
plugin :tmp_restart
and I have puma gem listed in Gemfile and installed
when I deploy to elastic beanstalk app, I get a successful deployment, and the app state is green, however, when I visit the app link, I get error This site can’t be reached
I tried to ssh into the ec2 instance, puma command will get:
$ puma
Puma starting in single mode...
* Version 2.16.0 (ruby 2.5.1-p57), codename: Midwinter Nights Trance
* Min threads: 0, max threads: 16
* Environment: production
ERROR: No application configured, nothing to run
so, what do I miss?

Rails tests are using server in development mode

I just set up Puma and it's working fine as a development server.
When I run an integration test, Puma is starting in development mode, not test mode.
Gemfile
group :test do
gem puma
end
test/test_helper.rb
class ActionDispatch::IntegrationTest
require 'rack/handler/puma'
Capybara.server = :puma
Capybara.register_server("puma") do |app, port|
server = Puma::Server.new(app)
server.add_tcp_listener(Capybara.server_host, port)
server.run
end
end
A test:
✗ RAILS_ENV="test" ruby -I test test/integration/computers_test.rb -n /polt/
Started with run options -n /polt/ --seed 3050
Puma starting in single mode...
* Version 3.8.2 (ruby 2.3.1-p112), codename: Sassy Salamander
* Min threads: 0, max threads: 4
* Environment: development
* Listening on tcp://127.0.0.1:49875
Use Ctrl-C to stop
...
Does the same behavior happen when you run rake test ?
I'd make sure in your config/puma.rb file (create one if you don't have one ) you have a line like environment ENV.fetch("RAILS_ENV") { "development" } - without it I was seeing the same behavior you were.
You need to set the RACK_ENV as the RAILS_ENV to :
RACK_ENV="test" RAILS_ENV="test" ruby -I test test/integration/computers_test.rb -n /polt/
Then the puma server will use the test environment:
* Min threads: 0, max threads: 4
* Environment: test
* Listening on tcp://127.0.0.1:49875
Add
ENV["RAILS_ENV"] = "test"
ENV["RACK_ENV"] = "test"
in your test file, before all tests.
This is actually similar to #ZedTuX answer, but I cannot comment there.

Prevent puma from running in locally (ruby on rails)

I'm recently started to use Puma for my production server with nginx, however, when I now try to run my app locally, it tries to run Puma with all my production settings and fails. How can I prevent Puma from running locally?
AFAIK all I've done was added the puma gem to my gemfile, so I don't know how it's accessing my server config (I'm just not too knowledgeable in this area). I have it in my production group:
group :production do
gem 'pg'
gem 'rails_12factor'
gem 'puma'
end
Error:
→ rails s
=> Booting Puma
=> Rails 4.2.6 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[8917] Puma starting in cluster mode...
[8917] * Version 3.4.0 (ruby 2.0.0-p645), codename: Owl Bowl Brawl
[8917] * Min threads: 1, max threads: 6
[8917] * Environment: development
[8917] * Process workers: 1
[8917] * Phased restart available
[8917] * Listening on tcp://localhost:3000
[8917] Use Ctrl-C to stop
/rbenv/versions/2.0.0-p645/lib/ruby/gems/2.0.0/gems/puma-3.4.0/lib/puma/runner.rb:103:in `reopen': No such file or directory - /Users/me/mll/shared/log/puma.stdout.log (Errno::ENOENT)
Additionally, though less important to me right now, is it in my benefit to run Puma locally? If so, any tips/resources on how I can do that?
You need to put puma in your production group. Like this:
group :production do
gem 'puma'
end
That way puma will only be used on production and not development.
Update
Make sure that your bin/rails file looks like this:
#!/usr/bin/env ruby
APP_PATH = File.expand_path('../../config/application', __FILE__)
require_relative '../config/boot'
require 'rails/commands'
Having the exact same issue with Ben. I try to use web brick on local while developing and testing, and puma in production.
Gem file is perfectly define as puma only sits in production group. 'bin/rails' and 'config/application' are checked, same as #Răzvan Ciocănel suggested. Still boot with 'puma' on local.
At last, have a look into the 'bundle install' gem list, 'puma' is installed along with other 'production' gem. Run 'bundle install --without production' and now local will boot with web brick as ROR default.
I guess as long as puma is bundle installed, it will be loaded on local unless you config something to force it off. So the solution might be to remove the gem in the bundle list in development and test.
Rails 3 project (it keeps hanging around longer than expected...)
I changed from unicorn to puma in the production group of my gemfile.
group :production do
gem 'puma'
end
Then then when trying to run tests, or a dev server (which should have been thin) I got:
C:\Rails Projects\Rep>rails s
Could not find gem 'puma x86-mingw32' in any of the gem sources listed in your Gemfile or available on this machine.
Run `bundle install` to install missing gems.
After poking around a bit (including finding this question) I gave up and decided to go ahead and use puma in dev. I ran bundle install and tried running server and voila, thin was working again.
Then I realized I still only had it in the production group--I looked back at the bundle install and nothing was installed, puma still isn't installed. But now that bundle install has been run since the edit to the gemfile, everything works again.
C:\Rails Projects\Rep>rails s
=> Booting Thin
=> Rails 3.2.22.2 application starting in development on http://0.0.0.0:3000
So I guess the 'missing gem' error I wasn't because it was trying to actually run puma but rather some kind of bundler generated error due to unattempted gemfile? Just putting this out in case it speed things up for anyone in similar circumstances who ends up here.

How to check rails environment?

How check rails environment on Ubuntu Server?
command: Rails.env => command not found
command: rails.env => command not found
One liner if you are in app root
rails r "puts Rails.env"
It sounds like you tried to run Rails.env in a shell. That won't work because Rails.env is Ruby code, not a Unix shell command.
How are you deploying and starting your rails app on the server? The Rails environment is determined by whatever the value of the RAILS_ENV environment variable is when the server starts. You might have some configuration file somewhere that specifies it, or maybe you just start your server with a command of the form RAILS_ENV=production my_rails_server? I would need to know more details about exactly what commands you run to start the server in order to really answer this. Are you using unicorn, mongrel, Webrick, or something else?
You can check complete details about your rails app. By typing this command "rake about". Will give you brief details about which version of ruby have you installed on your machine, rails version etc. For example -
About your application's environment
Rails version ------> 4.2.6
Ruby version ------> 2.3.1-p112 (x86_64-linux)
RubyGems version ----> 2.5.1
Rack version ----> 1.6.4
JavaScript Runtime -------> Node.js (V8)
Middleware ------> Rack::Sendfile, ActionDispatch::Static,
Application root ----> /data/www/testapp
Environment ------> development
Database adapter -----> mysql2
Database schema version -----> 0
On your Rails Application directory type :
rake about
rails r -e production 'p Rails.env'
production
rails r -e production 'p Rails.env.production?'
true
rails r 'p Rails.env'
development
rails r -e development 'p Rails.env.development?'
true
rails r -e test 'p Rails.env.test?'
true
PS If rails command not found try to use path bin/:
bin/rails r 'p Rails.env'
development
PS2 If use rvm, check installed ruby versions:
rvm list
ruby-2.2.0 [ x86_64 ]
ruby-2.2.4 [ x86_64 ]
ruby-2.6.2 [ x86_64 ]
ruby-2.7.0 [ x86_64 ]
ruby-2.7.1 [ x86_64 ]
=> ruby-2.7.2 [ x86_64 ]
* ruby-2.7.3 [ x86_64 ]
ruby-3.0.0 [ x86_64 ]
# => - current
# =* - current && default
# * - default
Select to version:
rvm use ruby-3.0.0
Bundle install:
bundle
You can also check your environment from your Rails console in the shell. Start at the application directory path.
rails console<enter>
after you see the output from your console... (your output will most likely differ)
Running via Spring preloader in process XXXXX
Loading development environment (Rails X.x.x)
irb(main):001:0>
At the promt type
Rails.env<enter>
Unless you have custom environments, one of the following environment is loaded
=> "development"
=> "production"
=> "test"

Resources