Using foreman with more than one .env file - ruby-on-rails

With foreman, I'm trying to start my rails app using two .env files.
One is the regular .env file, but then a second one has extra variables to load. I'm trying to use the method mentioned in this guide to keep config variables out of code: https://devcenter.heroku.com/articles/config-vars
Here is my Procfile:
web: bundle exec rails server thin -p $PORT --env $RACK_ENV,var.env
My problem is that foreman doesn't seem to want to take the two arguments for --env, even though the docs say that it should be possible:
-e, --env
Specify an alternate environment file. You can specify more than one file by using: --env file1,file2.
When I try to run it with "foreman start," I get the error:
06:22:46 web.1 | You did not specify how you would like Rails to report deprecation notices for your development,var.env environment, please set config.active_support.deprecation to :log, :notify or :stderr at config/environments/development,var.env.rb
It just doesn't seem to want to split "development,var.env" and process them separately. When I looked at the foreman code, it looks like it should just do a simple split on the comma, so I can't tell if I'm doing something wrong or it's a bug.

I didn't use that feature yet but from the docs I would say your are trying to specify the command line parameter in the wrong place. Instead of adding it to the process in the Procfile you should add it to the foreman command itself:
foreman start --env .env,.env.local
That way the environment is set for all processes started by foreman.

Related

How to change WEBrick :AccessLog option when running RedMine?

I'm running RedMine via WEBrick using the following command line (simplified):
bundle exec rails server webrick -e production -p 3012 -P '/var/lib/redmine/redmine.pid'
I don't like how WEBrick outputs the peer address at the beginning of its access log lines (because I'm running it behind nginx and the peer address is always 127.0.0.1), so I want to change the access log format.
I know that I need to tune the :AccessLog config option for WEBrick, but I don't know how to get my hands on it. WEBrick is run by the rails server command, via the rack abstraction, and I don't see an obvious way to pass the necessary configuration to WEBrick.
So, is there any way to do it? Some command line switch? -c is the only switch that accepts some kind of configuration file, but it references "rackup", and I have no idea how to work with it.
Maybe it can be done by changing configuration files? I tried to modify additional_environment.rb by adding config[:AccessLog] = [ [ $stderr, WEBrick::AccessLog::COMMON_LOG_FORMAT ] ], but it had no effect (although the file was executed), so I assume this file's config is not what is passed to WEBrick.
I'm pretty sure there is some way to configure this option without creating a new Rails application and invoking WEBrick manually, and hopefully even without changing RedMine files.

Google App Engine: Ruby on Rails - Execute migrations automatically

I was wondering if it was possible to run migrations automatically during deployment with Google App Engine. I have been using AWS Elasticbeanstalk for a while and they were ran automatically but now I am considering moving to the Google App Engine for my future projects.
Right now, I must run this command manually:
bundle exec rake appengine:exec -- bundle exec rake db:migrate GAE_CONFIG=app.yml
Thank you
WARNING: As discussed in comments, there is a race condition in migrations if deployment is done on multiple containers in parallel, because it will try to run migration on all containers. Solution is being discussed in comments, i will update this answer when we land on something.
Disclaimer: This answer is not exactly what was asked for, but it solves same problem and it works. And from what i can tell from question, doing it with some appengine config is not a requirement, rather he just want the migrations to run automatically.
I will expand on my comment on question, here is something i tried and it works. I am strong believer of KISS(keep it simple and stupid). So instead of trying to figure out appengine(which i have never used anyway) if i were you, i would take a generic approach. Which is, to plug into rails server booting process and trigger migrations. For this we have multiple approaches.
With my understanding of appengine and suggested by this official doc link appengine has a app.yaml file, this file has an entry something like:
entrypoint: rails server
So we will use this entry point to plug in our code to run migrations before starting server. For this i did this:
Make a new file in bin directory, i named it
rails_with_migrations.sh you can name it whatever you like.
Give it execute permissions with chmod +x bin/rails_with_migrations.sh
Put this code inside it:
#!/bin/bash
bundle exec rake db:migrate
bundle exec rails $#
Of course you can give whatever RAILS_ENV you want to give these.
Now in app.yaml on the entrypoint section, instead of rails server give it bin/rails_with_migrations.sh server and it should be it. It worked on local, should work everywhere.
NOTE: In entrypoint: i have bin/rails_with_migrations.sh server here, server is rails command parameter, you can pass as much parameters as you like these all will be passed to rails server command with $#'s magic. It is there to allow you to pass port and any other parameters you may need to provide for your environment. Also it allows you to run rails console locally with bin/rails_with_migrations.sh console which will also cause migrations to get triggered.
UPDATE1: As per comment, i checked what happens if migration fails, and it starts server even if migration fail. We can alter this behavior of-course in our sh file.
UPDATE2: The shell-script with migration error code handling will look something like:
#!/bin/bash
bundle exec rake db:migrate
if [ $? -eq 0 ]
then
bundle exec rails $#
else
echo "Failure: migrations failed, please check application logs for more details." >&2
exit 1
fi
This update will prevent server from starting and causing a non zero exit code from the script, which should indicate that this command failed.

How to run initialization script on starting a Rails server?

I have a simple shell script (happy.sh) that I am currently running by hand (. ./happy.sh) every time I restart a rails server. It sets some environment variables that I need because of various APIs I'm using. However, I was wondering if there's some way of having rails run the script itself whenever I run "guard" or "rails s"
Thanks
If you use foreman, you can define all processes you needed started on application start into a Procfile. (including bbundle exec rails server -p $PORT)
By calling foreman start, all the process starts up.
More information can be gotten here on this cast
Proper way of setting ENV variables is putting them in bash_proflle or bashrc depending of linux distro.
vi ~/.bash_proflle
And then add something like
export MY_RAILS_VAR=123
Then you don't need to run any ENV initialization scripts on rails start.

Unable to see error in test.log in rails

I am having problem to generate a log to see the error of page break in log/test.log
Everything is generating except page break issue.
I have a project running on aws
when I start puma server using
bundle exec puma -e production -d -b unix:///tmp/run/pokerstop.sock
and generate log with tail -f log/production.log commands
I can see my production log
but when I start my puma sever in test environment and check log using
tail -f log/test.log command nothing comes out
sometime page goes break on server and I want check cause of page break in test server but nothing comes out
Please help me
Thanks.
Do you expect your rails app to be run in a test environment like development and production..?
If so, run your rails server in test environment as follows:
rails s -e test
You should see the logs of your application in /log/test.log file.
Also, make sure you have added database configuration for test environment in database.yml file.
Hope this helps you.!!

Rails - switch environment to production automatically

I am doing a project in rails, and i just run this project in console by following command:
$ rails server
But it runs in development mode. I want when i run "rails server", it runs in production mode, what script must i edit to set the environment ?
Easy:
rails server RAILS_ENV=production
Or:
rails s -e production
Or you meant without this extra thing? If that so, it depends on which server you use. You can install Puma for example, and add config file, in which you can specify the default environment.
This question could help in case of set rails env for ngnx or passenger.
All the possible operations on the rails server
-p port: Specify the port to run on
-b ip: Bind to a specific IP address
-e name: Use a specific Rails environment (like production)
-d: Run in daemon mode
-h: Display a help message with all command-line options

Resources