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.
Related
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?
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.
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
When i run the rails app in development mode,server logs are displaying in terminal properly. But when i run the app in production mode server is not displaying any server logs in terminal. and an error is displaying in the browser like "We're sorry, but something went wrong.".i am running the server with the following command.and i am using the thin server.
rails s -e production
this is the output i am getting in server mode when i start the server.and after this when i made any request no output is displaying.
prashant#prashant-pc:~/client_proj/template$ rails s -e production
=> Booting Thin
=> Rails 4.1.5 application starting in production 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
[DEPRECATION] requiring "RMagick" is deprecated. Use "rmagick" instead
Thin web server (v1.6.3 codename Protein Powder)
Maximum connections set to 1024
Listening on 0.0.0.0:3000, CTRL+C to stop
what might be issue i am not getting.
In production.rb you can add this option:
config.logger = Logger.new(STDOUT)
If you see this block in the production.rb
if ENV["RAILS_LOG_TO_STDOUT"].present?
logger = ActiveSupport::Logger.new(STDOUT)
logger.formatter = config.log_formatter
config.logger = ActiveSupport::TaggedLogging.new(logger)
end
then you can simply set environment variable to any value with export before starting the rails server
$ export RAILS_LOG_TO_STDOUT=true
Setting the environment variables RAILS_ENV=production and RAILS_LOG_TO_STDOUT=true did the job for me (with Rails 7.0.4)
BIt of a weird one this.
rails s
=> Booting Thin
=> Rails 3.2.13 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
Thin web server (v1.6.2 codename Doc Brown)
Maximum connections set to 1024
Listening on 0.0.0.0:3000, CTRL+C to stop
But when I call
puts Rails.env
in a controller, it says
production
In rails console it correctly says development
Rails.env
=> "development"
Any ideas.
I want to run in dev locally and production on my server. Particularly important as I'm testing functionality where it costs me money to run certain api calls and I have them faked if Rails.env.prodcution? is false.
Running rails 3.2.13