running rake task on 'production' and specifying environment? - ruby-on-rails

I have a host at Linode and am trying to run a Rake task on it, but I get a mySQL error saying it can't connect. It looks like it thinks it is on dev. I did some Googling and saw that I can do something like this:
bundle exec rails c
It loads the dev environment and I can't run User.all giving me an access denied error.
If I run bundle exec rails c RAILS_ENV=production I get the error:
Rails.env=production database is not configured (ActiveRecord::AdapterNotSpecified)
However, if I access it via the web, everything is OK. I was able to run rake db:seed before so I know that there's some way around this.
Accessing mySQL with the production credentials works fine.
Any ideas?

Try this:
rails c production
or, at the beginning:
RAILS_ENV=production rails c
It thinks you're passing RAILS_ENV=production as an argument when you put it at the end.

If you want to run your console in the context of the current bundle in your Gemfile and ensure you're using your Gemset use:
bundle exec rails c production

This works for me. It depends on how your server and all its dependencies are set up:
RAILS_ENV=production bundle exec rails console

Related

Is there a way to connect to a Rails app's console on a linux server?

I have deployed a rails application using capistrano on a linux server and it is running without any problems. When I connect to my remote server via ssh, the folder structure of my app is quite different from what I have on my local machine. I would like to go to project root and say rails console so that I can have access to the console of my application. Is there a way to achieve this?
When I go to ~MyApp/ folder and run rails console it says command rails not found. I think that is probably because the app is running in another folder.
bundle exec to the rescue inside of project folded:
$ RAILS_ENV=production bundle exec rails console
If you've installed with rbenv it could be that you are defaulting to the wrong Ruby version. So you can run rbenv exec to get it to run on the right version, along with bundle exec to run the right version of Rails
So try running this:
rbenv exec bundle exec rails console
To sum up for anyone having the same problem in the future,
I went to MyApp/releases/2020331231231231 which is the latest release of my app and there I used this command RAILS_ENV=production bundle exec rails console and I was able to do whatever I wanted to do in the console.
First you need to move inside the folder where the code is.
Check Capistrano config if deploy_to is defined. (If it's not it should be set to /var/www/#{application}/ by default read here)
Since Capistrano keep older versions of your app go to the current folder which is a symlink of the latest deployed version.
And run ENVIRONMENT=production bundle exec rails c or ENVIRONMENT=production bin/rails c. If doesn't work try with rbenv ENVIRONMENT=production rbenv exec bundle exec rails c

Delayed Job in Rails 4 with Capistrano

I cant figure how to start Delayed Jobs on a dedicated Ubuntu server.
It works fine on my localhost but when I run on my server
sudo RAILS_ENV=production bin/delayed_job restart
I get
sudo: bin/delayed_job: command not found
On top of that, if I run the "rake jobs:work RAILS_ENV=production" command Im getting the following error:
PG::FeatureNotSupported: ERROR: SELECT FOR UPDATE/SHARE is not allowed in subqueries
Apparently theres an issue with my psql version.
Is there any way I can get the script to work? Any effective Capistrano recipes available? All ive found on the web are old recipes for Rails 3 and older versions of capistrano.
Thanks in advance.
EDIT:
I have already bundled install the daemons gem and generated "delayed_job:active_record" on my local machine, then proceded to cap deploy which bundle installed and migrated in the production server.
The bin/delayed_job file exists in the server yet it fails with command not found.
And add this to config/environment.rb:
ENV['RAILS_ENV'] ||= 'production'
Then at your production server:
RAILS_ENV=production rake db:migrate
RAILS_ENV=test production generate delayed_job:active_record && RAILS_ENV=production rake db:migrate
Now after you do that:
RAILS_ENV=production script/delayed_job start
As for Capistrano error you are facing, please try to add the command like:
run "cd #{current_path}; #{sudo} RACK_ENV=production bundle exec #{current_path}/bin/delayed_job start"
You must run this on target server:
bundle exec rails generate delayed_job

How to run seed.rb file on Amazon ec2

Recently I hosted my Ruby on Rails application on Amazon EC2 using Elastic Beanstalk. Everything works fine except my seeds.rb file. My seeds.rb file is not executed at the time of hosting. I am using ActiveAdmin also and I define first admin on my seeds.rb file.
How can I create first admin user on Amazon by rails console? Is there any way to open Rails Console on Amazon EC2 ? I am trying to do this using putty but don't know how to do this. Please give me some pointers..
Are you not supposed to do something like this?
# .ebextensions/bundles_container.config
container_commands:
01-bundle-install:
command: "bundle install"
leader_only: true
02-bundle-db-migrate:
command: "bundle exec rake db:migrate"
leader_only: true
03-bundle-db-seed:
command: "bundle exec rake db:seed RAILS_ENV='staging'"
leader_only: true
You can also pass parameters if needed, or combine all those commands with "cmd1 && cmd2".
You need to create keypair to access the amazon instance(which i think you already have). Make sure that ssh access is enabled in the current selected security group.
You can connect to the amazon instance using
ssh -i path/to/keypair.pub ec2-user#ec2-an-ip-address.compute-1.amazonaws.com
Then cd into the app directory and run bundle exec rake db:seed RAILS_ENV='staging' assuming that you're running the app in staging environment.
In case you're here and the above solutions didn't work for you.
Apart from using the command provided in this answer above by benchwarmer:
https://stackoverflow.com/a/17232607/1216245
I had to run the seed command providing env vars for the master key and all rds settings.
bundle exec rake db:seed RAILS_ENV=production RAILS_MASTER_KEY=<your master key> RDS_HOSTNAME=<your rds hostname> RDS_PASSWORD=<...> RDS_USERNAME=<...> RDS_DB_NAME=<...> RDS_PORT=<...>
And it worked, finally :)
You can check all this in the Configuration panel for your environment in the AWS console (dashboard).

Sunspot Solr error on rake sunspot:index

I am trying to use Sunspot gem for Solr on a Rails 3 app. I have followed RailsCast #278, since I am using Windows I have to do rake sunspot:solr:run and that seems to start with no error. However when I try to run rake sunspot:reindex I get the error:
No connection could be made because the target machine actively refused it. - connect(2)
I am using mysql2 gem for mysql. Please advise.
I have to do rake sunspot:solr:run and that seems to start with no error
It sounds like the connection is not being accepted at all to Solr, so this part is my first suspicion — you should get a lot of fairly verbose output from Solr when starting it. Can you share some output of that?
Also, what version of java are you running? (java -version)
I had the same problem running "bundle exec rake sunspot:solr:reindex" on Windows. The solution is to add the correct RAILS_ENV option, for me it was test, e.g.
bundle exec rake sunspot:solr:reindex RAILS_ENV=test
Yes, you'll have to have solr server running beforehand:
bundle exec rake sunspot:solr:run RAILS_ENV=test

Rake error fixed by bundle exec, but deployment not working

I pushed an update to my Rails app production server, and in the update there was a new database migration. I ran rake db:migrate and got the common error seen here. I ran the rake again in bundle exec bash and it was successful. But after restarting my apache server, I'm now getting the 500 Error page. This update worked fine on my localhost, and was mostly this update to the db with supporting changes in the according view and controller/routing.
I don't even know why this error appeared this time, as I have pushed db updates successfully before using only rake. Nonetheless, the rake was successful. The 500 error page only shows on pages that require that specific new ActiveRecord. Any ideas on how to debug?
EDIT: My problem was an extremely simple one. I merely forgot to include the environment with the rake:
bundle exec rake db:migrate RAILS_ENV=production
Unfortunately, it took quite a while to narrow that down, as I couldn't use IRB to check the db entries until I followed these steps.
Did you run rake db:migrate on your server? Also be sure to set the RAILS_ENV flag so your production database is updated:
rake db:migrate RAILS_ENV=production

Resources