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.
Related
I've a Rails app running in Kubernetes and try to run bin/rake spec
via exec in my running pod.
kubectl exec backend-xxxxx -- sh
bin/rake -T # lists my tasks (and works)
Other commands like rake db:migrate are working.
Now I try to run my specs, but equally what I try (using suite, models only or a single file) my pod crashes:
bin/rake spec
bin/rake spec:models
bin/rake spec spec/models/user_spec.rb
Output of rake task:
Running via Spring preloader in process 119
/usr/local/bin/ruby -I/usr/local/bundle/ruby/2.7.0/gems/rspec-core-3.10.1/lib:/usr/local/bundle/ruby/2.7.0/gems/rspec-support-3.10.2/lib /usr/local/bundle/ruby/2.7.0/gems/rspec-core-3.10.1/exe/rspec --pattern spec/\*\*\{,/\*/\*\*\}/\*_spec.rb
command terminated with exit code 137
This is my complete log:
Puma starting in single mode...
* Version 3.12.6 (ruby 2.7.2-p137), codename: Llamas in Pajamas
* Min threads: 5, max threads: 5
* Environment: development
Rails Version & environment: 6.1.4.1 - development
Elastic URL credentials provided
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop
Exiting with code 137 # <<<<<<----- this line appears when running rake spec
I've no idea why that happens. My Minikube has 12GB of assigned RAM (minikube config set memory 12288)
I was watching a (albeit old) security talk on insecure rails defaults. I was wondering if there is a way to tell if Rails now binds to ip 127.0.0.1, port 3000 by default? When I spin up rails s, I see
± |master {1} U:4 ✗| → rails s
=> Booting Puma
=> Rails 6.0.2.2 application starting in development
=> Run `rails server --help` for more startup options
/Users/pivotal/.rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/gems/actionpack-6.0.2.2/lib/action_dispatch/middleware/stack.rb:37: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
/Users/pivotal/.rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/gems/actionpack-6.0.2.2/lib/action_dispatch/middleware/static.rb:110: warning: The called method `initialize' is defined here
Puma starting in single mode...
* Version 4.3.3 (ruby 2.7.0-p0), codename: Mysterious Traveller
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://**127.0.0.1**:3000
* Listening on tcp://[::1]:3000
Use Ctrl-C to stop
As per this pretty old Stack overflow post, I tried adding the following to my boot.rb and got an error about my Spring versions.
Boot.rb
require 'rails/commands/server'
module Rails
class Server
def default_options
super.merge({Port: 10524, Host: '127.0.0.1'})
end
end
end
± |master {1} U:4 ✗| → rails s
You've tried to invoke Spring when it's already loaded (i.e. the Spring constant is defined).
This is probably because you generated binstubs with Spring 1.0, and you now have a Spring version > 1.0 on your system. To solve this, upgrade your bundle to the latest Spring version and then run `bundle exec spring binstub --all` to regenerate your binstubs. This is a one-time step necessary to upgrade from 1.0 to 1.1.
I do those steps, but still an error.
You are using Rails 6, and latest versions uses puma so does your app. If you want to change the port open config/puma.rb. There you will see the line
port ENV.fetch("PORT") { 3000 }
This line is what makes rails start in port 3000. Change it to any other port. In your case -
port ENV.fetch("PORT") { 10524 }
Also if you want to change the bind address replace port with bind
#port ENV.fetch("PORT") { 3000 }
bind 'tcp://192.168.0.1:10524'
Check https://github.com/puma/puma for more.
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?
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
I'm encountering a problem with a Rails app I just tried to deploy that I just can't seem to figure out. Rails wants to start only in development, even if RAILS_ENV=production is set. For example:
$ rails s --environment=production
Finished setting environment variables
=> Booting Puma
=> Rails 4.1.4 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Notice: server is listening on all interfaces (0.0.0.0). Consider using 127.0.0.1 (--binding option)
=> Ctrl-C to shutdown server
Puma 2.9.0 starting...
* Min threads: 0, max threads: 16
* Environment: production
* Listening on tcp://0.0.0.0:3000
Notice how puma correctly starts in production, but rails itself starts in development? Very strange. That "Finished setting environment variables" is from the following little script in my application.rb:
config.before_configuration do
env_file = File.join(Rails.root, 'config', 'local_env.yml')
YAML.load(File.open(env_file)).each do |key, value|
ENV[key.to_s] = value
end if File.exists?(env_file)
ENV['GOOGLE_ANALYTICS_ID'] = 'UA-XXXXXXXX-2' if Rails.env = 'development'
puts "Finished setting environment variables"
end
It sets environment variables based on a local_env.yml file I have, which does not include RAILS_ENV. Does anyone have any ideas here? Thanks
Wow. Reviewing my question just now, the answer hit me like a brick wall:
ENV['GOOGLE_ANALYTICS_ID'] = 'UA-XXXXXXXX-2' if Rails.env == 'development'
NOT THIS:
ENV['GOOGLE_ANALYTICS_ID'] = 'UA-XXXXXXXX-2' if Rails.env = 'development'
Moral: check your equal signs.