foreman fails to load environment variables - ruby-on-rails

I'm using rails 4.0.0 and ruby 2.0.0
When I start the server with foreman some of the environment variables fail to load. It really bugs me that some of the variables are loaded.
foreman start -e development.env
Procfile
web: bundle exec passenger start -p $PORT -e $RAILS_ENV
worker: bundle exec rake jobs:work RAILS_ENV=$RAILS_ENV
development.env file
S3_BUCKET=bucketname
AWS_ACCESS_KEY_ID=accesskey
AWS_SECRET_ACCESS_KEY=secretaccesskey
RAILS_ENV=development
PORT=3000
In my application.rb file i've added some logging to help debug this problem
puts "PORT is #{ENV["PORT"].inspect}"
puts "RAILS_ENV is #{ENV["RAILS_ENV"].inspect}"
puts "S3_BUCKET is #{ENV["S3_BUCKET"].inspect}"
puts "AWS_ACCESS_KEY_ID is #{ENV["AWS_ACCESS_KEY_ID"].inspect}"
puts "AWS_SECRET_ACCESS_KEY is #{ENV["AWS_SECRET_ACCESS_KEY"].inspect}"
Once I start the server this is the output for the logging code
23:34:52 worker.1 | PORT is nil
23:34:52 worker.1 | RAILS_ENV is "development"
23:34:52 worker.1 | S3_BUCKET is nil
23:34:52 worker.1 | AWS_ACCESS_KEY_ID is nil
23:34:52 worker.1 | AWS_SECRET_ACCESS_KEY is nil
Why oh Why ? :-(
When I load the rails console with foreman it successfully loads the variables
foreman run -e development.env rails c

Try modify your development.env like
export S3_BUCKET=bucketname
export AWS_ACCESS_KEY_ID=accesskey
export AWS_SECRET_ACCESS_KEY=secretaccesskey
export RAILS_ENV=development
export PORT=3000
Then in the terminal
$ source /path/to/development.env
$ foreman start
Advanced
You can use dotenv to manage some of your environment variables without polluting your system environment. Though it can't manage those environment variables required for server booting like PORT.

Related

Set Rails API in Heroku to development and impact of the Procfile?

I'm new to Heroku and despite all the documentation, I'm a little unsure what the profile is for. I can set a port and the environment as follows, but Heroku always starts in production mode (which makes sense) and not with the specified port.
I suppose that the port cannot be set because it is determined by Heroku?
Is the Procfile only for the command "heroku local" to test?
Because when I run "heroku ps" I get info about the procfile, but the API runs without the procfile port in production mode.
Thank you for any explanation!
Procfile:
web: bundle exec puma -t 5:5 -p ${PORT:-3000} -e ${RACK_ENV:-development}
Output of heroku ps after deploying:
=== web (Hobby): bundle exec puma -t 5:5 -p ${PORT:-3000} -e ${RACK_ENV:-development} (1)
web.1: up 2020/07/27 13:50:23 +0200 (~ 1m ago)
Output of heroku logs at the same time:
Version 3.12.6 (ruby 2.5.8-p224), codename: Llamas in Pajamas
2020-07-27T11:49:32.219309+00:00 app[web.1]: * Min threads: 5, max threads: 5
2020-07-27T11:49:32.219309+00:00 app[web.1]: * Environment: production
2020-07-27T11:49:33.740321+00:00 app[web.1]: * Listening on tcp://0.0.0.0:10269
Heroku will set both $PORT and $RACK_ENV for Rails apps when they're deployed. You can confirm this by running heroku config --app <yourapp>. The construct ${PORT:-3000} means "use the PORT variable if it's present, otherwise use the value 3000.
In any case, you can't run a Heroku app on a port other than the one defined in $PORT, which is randomized for each dyno. Whatever that's set to will be forwarded to from ports 80 and 443 for HTTP/S.
If you want to override the RACK_ENV, you can run heroku config:set RACK_ENV=development.

Redis connection error on Heroku

I have a Rails app which uses Resque for background jobs. This works fine locally, but after deploying to Heroku I get a connection error:
Redis::CannotConnectError (Error connecting to Redis on localhost:6379 (Errno::ECONNREFUSED)):
I see it tries to connect to localhost, which is not correct. I'm using the Heroku Redis :: Redis add-in and I have added the redis gem. This is how initializers.redis.rb looks like:
$redis = Redis.new(url: ENV["REDIS_URL"])
And this is my Procfile:
web: bundle exec puma -C config/puma.rb
resque: env TERM_CHILD=1 bundle exec rake resque:work QUEUE=* COUNT=1
In the config vars REDIS_URL is added. What is going wrong here?
It seems that when using Puma or Unicorn on Heroku, you need to add it also to the boot process. I'm using Puma, so I added this to config/puma.rb:
on_worker_boot do
# ...
if defined?(Resque)
Resque.redis = ENV["REDIS_URL"] || "redis://127.0.0.1:6379"
end
end
Here is a more detailed explanation for both Puma and Unicorn:
https://devcenter.heroku.com/articles/deploying-rails-applications-with-the-puma-web-server
https://devcenter.heroku.com/articles/rails-unicorn#caveats

Getting started with Rails on Heroku using a Procfile

Using a vanilla rails install using git (in fact following the heroku guide here https://devcenter.heroku.com/articles/rails3)
However it mentions the creation of a Procfile
web: bundle exec rails server thin -p $PORT -e $RACK_ENV
Yet if you run this is needs using foreman start, you receive an error because you haven't defined the RACK_ENV
20:45:26 web.1 | started with pid 26364 20:45:27 web.1 |
/SomeLocalPath/.rvm/gems/ruby-1.9.2-p318/gems/railties-3.2.2/lib/rails/commands/server.rb:33:in
`parse!': missing argument: -e (OptionParser::MissingArgument)
Where should this -e argument be stored for this all to work?
I guess you mean that you are getting this error on your local development machine.
You can set the RACK_ENV when starting foreman like this, for example:
RACK_ENV=development foreman start
Or you could use a different procfile for development (e.g. "Procfile-dev") which has the value for the option -e inline, like this:
web: bundle exec rails server thin -p 3000 -e development
and call it with:
foreman start -f Procfile-dev
(On Heroku, it should just work, because when you run "heroku config -s" while you are in your app-folder, you should see "RACK_ENV=production", so the needed environment variable is set correctly here).

issue getting rack_env with foreman / heroku facebook app

Having a hard time getting my local setup properly. Trying to run foreman and getting an issue:
web: bundle exec thin -R config.ru start -p $PORT -e ${RACK_ENV:-development}
RACK_ENV:"FACEBOOK_APP_ID=275479742529226"
RACK_ENV:"FACEBOOK_SECRET=xxx"
running "foreman start"
15:21:21 RACK_ENV.1 | process terminated
15:21:21 web.1 | started with pid 23406
15:21:21 system | sending SIGTERM to all processes
15:21:21 RACK_ENV.1 | started with pid 23407
15:21:21 system | sending SIGTERM to pid 23406
15:21:21 RACK_ENV.1 | started with pid 23408
15:21:21 RACK_ENV.1 | /Users/mc/.rvm/gems/ruby- 1.9.2-p290/gems/foreman-
0.41.0/bin/foreman-runner: line 36: exec: "FACEBOOK_APP_ID=275479742529226": not found
apparently I'm setting my fb keys completely incorrect for the env.. if anyone could help out. I've tried setting up a .env file in the root as well, which contain
FACEBOOK_APP_ID=964173273189
FACEBOOK_SECRET=xxx
Removing everything from the procfile (which I believe is incorrect anyways) and running foreman, shouldn't it pickup the .env file in the root by default? which isn't proving any luck.
5:44:20 web.1 | started with pid 23705
15:44:24 web.1 | missing env vars: please set FACEBOOK_APP_ID and FACEBOOK_SECRET
with your app credentials
15:44:24 web.1 | process terminated
15:44:24 system | sending SIGTERM to all processes
Thanks
I don't think the issue is that foreman isn't finding .env, it's that the syntax of your Procfile is incorrect. Every line in the Procfile after the : should executable on the command line.
Create your Procfile with the following contents:
web: bundle exec thin -R config.ru start -p $PORT -e $RACK_ENV
And set your .env file to:
RACK_ENV=development
FACEBOOK_APP_ID=1234
FACEBOOK_SECRET=xyz
Note the RACK_ENV var needs to be explicitly set whereas PORT does not since Foreman automatically sets it.

foreman start error (server.rb:33, missing argument...)

After trying to start foreman, I get this error (note that it does seem to work on heroku though so I guess this is a strictly local problem):
hrn039:textthechange jon$ foreman start
02:20:00 web.1 | started with pid 7363
02:20:01 web.1 | /Users/jon/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.1/lib/rails/commands/server.rb:33:in `parse!': missing argument: -e (OptionParser::MissingArgument)
02:20:01 web.1 | from /Users/jon/.rvm/gems/ruby-1.9.3-p0/gems/rack-1.4.1/lib/rack/server.rb:280:in `parse_options'
02:20:01 web.1 | from /Users/jon/.rvm/gems/ruby-1.9.3-p0/gems/rack-1.4.1/lib/rack/server.rb:180:in `options'
02:20:01 web.1 | from /Users/jon/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.1/lib/rails/commands/server.rb:54:in `set_environment'
02:20:01 web.1 | from /Users/jon/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.1/lib/rails/commands/server.rb:42:in `initialize'
02:20:01 web.1 | from /Users/jon/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.1/lib/rails/commands.rb:50:in `new'
02:20:01 web.1 | from /Users/jon/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.1/lib/rails/commands.rb:50:in `<top (required)>'
02:20:01 web.1 | from script/rails:6:in `require'
02:20:01 web.1 | from script/rails:6:in `<main>'
02:20:01 web.1 | process terminated
02:20:01 system | sending SIGTERM to all processes
The Procfile only has one line as specified by heroku
web: bundle exec rails server thin -p $PORT -e $RACK_ENV
And my gemfile has
gem 'thin'
Google isn't being very helpful with this error.
Thanks!
This is not about executing on Heroku - see that the original question about is about executing with Foreman - which is local execution.
You can replicate your error by executing the following:
rails server thin -e
That's effectively what Foreman is running, given your Procfile:
web: bundle exec rails server thin -p $PORT -e $RACK_ENV
So I'm going to guess that you're not passing in an argument to -e. ie. you haven't defined RACK_ENV locally.
What you can do is create a .env file in your local directory, something like
RACK_ENV=development
PORT=3000
Foreman will automatically pick up the local .env file and set the environment appropriately, before creating the process based on your process type declaration.
I had the same problem with rails v3.2 in Ubuntu 10.04.
I managed to get thin running by doing the following steps:
Change your Procfile as follows:
web: bundle exec rails server thin -p $PORT -e development
Add $stdout.sync = true on the top of your config.ru file, to direct server output to your terminal (otherwise you do not get output in your terminal)
Tell me if it works!
I just ran into this same problem. If you change your Procfile to just
web: bundle exec rails server thin -p $PORT
it should work. Note, this uses the default Thin port of 5000 rather than 3000 (which means you'll need to go to http://localhost:5000 to see your app.
Having just
web: bundle exec rails server thin
in your Procfile will use port 3000, but this will cause errors on Heroku.

Resources