rake db tasks disappear in deployment - ruby-on-rails-3

I have a Rails 3.2.8 app which is running nicely in development on my machine, with Postgres as the db backend for ActiveRecord. However, when I deploy my code to an EC2 server, either via capistrano or by rsync'ing my project to the EC2 instance, rake no longer lists any of the db: tasks:
$ rake -T
rake about # List versions of all Rails frameworks and the environment
rake assets:clean # Remove compiled assets
rake assets:precompile # Compile all the assets named in config.assets.precompile
rake doc:app # Generate docs for the app -- also available doc:rails, doc:guides, doc:plugins (options: TEMPLATE=/rdoc-t...
rake load_gml # Load GML source files into the environment database
rake log:clear # Truncates all *.log files in log/ to zero bytes
rake middleware # Prints out your Rack middleware stack
rake notes # Enumerate all annotations (use notes:optimize, :fixme, :todo for focus)
rake notes:custom # Enumerate a custom annotation, specify with ANNOTATION=CUSTOM
rake rails:template # Applies the template supplied by LOCATION=(/path/to/template) or URL
rake rails:update # Update configs and some other initially generated files (or use just update:configs, update:scripts, or u...
rake routes # Print out all defined routes in match order, with names.
rake secret # Generate a cryptographically secure secret key (this is typically used to generate a secret for cookie se...
rake stats # Report code statistics (KLOCs, etc) from the application
rake test # Runs test:units, test:functionals, test:integration together (also available: test:benchmark, test:profil...
rake test:recent # Run tests for {:recent=>"test:prepare"} / Test recent changes
rake test:single # Run tests for {:single=>"test:prepare"}
rake test:uncommitted # Run tests for {:uncommitted=>"test:prepare"} / Test changes since last checkin (only Subversion and Git)
rake time:zones:all # Displays all time zones, also available: time:zones:us, time:zones:local -- filter with OFFSET parameter,...
rake tmp:clear # Clear session, cache, and socket files from tmp/ (narrow w/ tmp:sessions:clear, tmp:cache:clear, tmp:sock...
rake tmp:create # Creates tmp directories for sessions, cache, sockets, and pids
rake -P also reports no db: tasks.
I'm guessing it's some sort of dependency problem, but I can't see what that would be since I'm declaring all my gem dependencies in a Bundler Gemfile. Postgres 9.2.1 is installed and working, but even if it wasn't I don't get why the rake task would vanish.
Any clues as to what's going on gratefully received!

And it turns out to be stupid-simple: I had copied the deploy.rb file from a previous project which didn't use ActiveRecord, so I hadn't set the role db:. I'm still not sure why that meant that the rsync'ed copy of my project also didn't have any rake db: tasks, but I'm not going to spend any more cycles worrying about that.

Related

Capistrano 3 runs every command twice (new install) - Configuration issue

I just completed my capistrano installation for the first time. Most of everything is left to default settings, I configured my server, its authentification, and the remote folder, as well as the access to my git repository.
I use capistrano to deploy php code to my server.
cap staging deploy and cap production deploy function, but they run every command twice. It sometimes causes problems when those tasks are executed too quickly on the server, returning error codes, which stops the deploying process.
an example of my output when running cap staging deploy
DEBUG[47ecea59] Running /usr/bin/env if test ! -d ~/www/test_server/repo; then echo "Directory does not exist '~/www/test_server/repo'" 1>&2; false; fi on ftp.cluster013.ovh.net
DEBUG[47ecea59] Command: if test ! -d ~/www/test_server/repo; then echo "Directory does not exist '~/www/test_server/repo'" 1>&2; false; fi
DEBUG[c450e730] Running /usr/bin/env if test ! -d ~/www/test_server/repo; then echo "Directory does not exist '~/www/test_server/repo'" 1>&2; false; fi on ftp.cluster013.ovh.net
DEBUG[c450e730] Command: if test ! -d ~/www/test_server/repo; then echo "Directory does not exist '~/www/test_server/repo'" 1>&2; false; fi
It does the same with every single task, except the one I defined myself (in my deploy.rb, I defined a :set_distant_server task that moves around files with server info)
I am pretty sure I missed something during the initial configuration.
Here is my capfile, still to default settings :
# Load DSL and Setup Up Stages
require 'capistrano/setup'
# Includes default deployment tasks
require 'capistrano/deploy'
# Includes tasks from other gems included in your Gemfile
# require 'capistrano/rvm'
# require 'capistrano/rbenv'
# require 'capistrano/chruby'
#require 'capistrano/bundler'
#require 'capistrano/rails/assets'
#require 'capistrano/rails/migrations'
# Loads custom tasks from `lib/capistrano/tasks' if you have any defined.
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }
Followed by my deploy.rb file:
# config valid only for Capistrano 3.1
lock '3.2.1'
set :scm, :git
set :application, 'Application name'
# I use token authentification
set :repo_url, 'https://XXXXXXXXXXX:#XXXXXXX.git'
set :role, 'web'
# Default value for :log_level is :debug
set :log_level, :debug
set :tmp_dir, 'www/test_server/tmp'
set :keep_releases, 8
role :deploy_server, "XXXuser_name#XXXX_server"
task :set_distant do
on roles(:deploy_server) do
execute 'echo ------------******* STAGING *******------------'
execute 'cp ~/www/test_server/current/access_distant.php ~/www/test_server/current/access.php'
execute 'cp ~/www/test_server/current/session_distant.php ~/www/test_server/current/session.php'
end
end
after "deploy:finished", :set_distant
Here is my staging.rb, much shorter:
server 'XXX_server', user: 'XXXuser_name', roles: %w{web}, port: 22, password: 'XXXpassword'
set :deploy_to, '~/www/test_server'
set :branch, 'staging'
And my production.rb, very similar:
server 'XXX_server', user: 'XXXuser_name', roles: %w{web}, port: 22, password: 'XXXpassword'
set :deploy_to, '~/www/beta/'
I'm pretty sure I missed a step in all the prerequisites to make it run nicely. I am new to ruby, to gems, and didn't use shell for a very long time.
Does anyone see why those commands are run twice, and how I could fix it?
In advance, many many thanks.
Additional info:
Ruby version: ruby -v
ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-darwin13.0]
Capistrano version: cap -V
Capistrano Version: 3.2.1 (Rake Version: 10.1.0)
I did not create a Gemfile or set it up, I understood it was not needed in Capistrano 3. Anyway, I would not know how to do it.
I was having this same issue and realized I didn't need both
role :web
and
server '<server>'
I got rid of role :web and that got rid of the 2nd execution.

"file too short" on bundle install

I'm getting occasional "file too short" messages when running bundle exec rake:
rake aborted!
/var/lib/jenkins/.rvm/gems/ruby-1.9.3-p327/bundler/gems/amatch-0f95f4ce269f/lib/amatch_ext.so: file too short - /var/lib/jenkins/.rvm/gems/ruby-1.9.3-p327/bundler/gems/amatch-0f95f4ce269f/lib/amatch_ext.so
Is there a way to make bundler more fault-tolerant and try to re-run when it encounters these spurious failures?
Why might they be happening in the first place? Multiple processes may be executing rake tasks simultaneously - can this corrupt rvm's gem repository, and if so how do I avoid the problem?
if you use it in multiple processes then use bundle --standalone - assuming every process is ran from different path - if they all use the same path then you could try bundle --path /path/for/gems$$/ the $$ will be replaced with process pid - but --path is recorded option and this will not help as only the last run will be visible in this directory.
best would be to limit amount of runs that are performed at the same time.
other option would be modifying GEM_HOME at runtime, but this can get complicated with jenkins so most likely this would not work:
OLD_GEM_HOME=$GEM_HOME
GEM_HOME=$( mktemp -d )
cp -r $OLD_GEM_HOME/ $GEM_HOME/
bundle install
# other commands
rm -rf $GEM_HOME/
GEM_HOME=$OLD_GEM_HOME

How can I get rails to use pg by default?

It's not a huge burden but I would really like to be able to change the default gem-set on my rails apps when I create them so that they're ready for Heroku.
What is the best way to go about doing this?
You can specify the database with -d when running rails new:
Usage:
rails new APP_PATH [options]
Options:
-d, [--database=DATABASE] # Preconfigure for selected database (options: mysql/oracle/postgresql/sqlite3/frontbase/ibm_db/sqlserver/jdbcmysql/jdbcsqlite3/jdbcpostgresql/jdbc)
# Default: sqlite3
Description:
You can specify extra command-line arguments to be used every time
'rails new' runs in the .railsrc configuration file in your home directory.
So for PostgreSQL this is:
rails new myapp -d postgresql
To make this the default put -d postgresql into ~/.railsrc
Another option is to change gem 'sqlite3' to gem 'pg' in your Gemfile as suggested in Getting Started with Rails 3.x on Heroku.

Rails 3 / whenever cron not firing

I am using whenever to fire a rake task every 5 minutes for my app.
schedule.rb:
every 5.minutes do
rake "audit",
:environment => 'development'
end
"whenever" in console:
0,5,10,15,20,25,30,35,40,45,50,55 * * * * /bin/bash -l -c 'cd /Users/john/Sites/rambler && RAILS_ENV=development bundle exec rake audit --silent'
"rake audit" in console works properly.
So all looks good .... except it doesn't work. Nothing happens every five minutes.
Is this because I am trying to run it in development / local?
Thanks!
You need to update your cron file every time you change it.
After you have addded your cron job do this:
whenever --update-crontab 'project_name'
Also I only found whenever working fine in production mode only.
UPDATE:
I have found that we can use whenever in development mode also. Just add
set :environment, "development"
set :output, {:error => "log/error.log", :standard => "log/cron.log"}
to your scehdule.rb file. ( The log one is optional but still you can use that for testing purpose)
Finally I have solved how to run the gem Whenever. It's working good on production, but not in development mode (I think that to working good in dev mode you must do some tricks).
see this answer for working in dev mode: Cron not working in Whenever gem
Then, these are the processes to do:
install the gem
write your scheduler.rb file
push to the remote server
login to the remote server (for example with ssh)
see if whenever is good uploaded by running in terminal: whenever
update whenever crontab by running: whenever --update-crontab
restart the server crontab (for example in ubuntu): sudo service cron restart
check if crontab are good implemented on the server: crontab -l
That is!

jruby pass jvm arguments to rake task

How do i pass jvm arguments like Xmx to a rake task in jruby?
Am using rvm and running the rake task "rake db:migrate".
Thanks!
Rather that put the entire path, if jruby is already the Ruby you're using (for example, with rvm) such that just typing rake would be using jruby, you can use a double dash to send the arguments to the jvm.
Examples:
bundle exec rake -- -J-Xmx1024m (if you're using bundler)
rake -- -J-Xmx1024m
Found it!
jruby -J-Xmx2048m -S /home/prats/.rvm/gems/jruby-1.6.1#myapp/bin/rake db:migrate