2
votes

I've ruby on rails application running in production, this apps have STOCK model and it should be updated every 1 minute.

For updating this model i create simple rake task db:populate:stocks, when it contain only two operation :

  1. truncate the STOCK model
  2. fill STOCK model with latest data fetch from webservice

this rake task should be execute every 1 minute, for this purpose i use whenever gem. Here my schedule.rb :

env :PATH, ENV['PATH']
set :job_template, nil
set :output, {:standard => '/home/admin/shared/log/cron.log', :error => '/home/admin/shared/log/cron-error.log'}

job_type :rake, "cd :path && rake :task RAILS_ENV=:environment --trace :output"

every 1.minute do
  rake "db:populate:stocks"
end

In production i'm using rvm running ruby 1.9.2-p180, rails 3.1.0 and capistrano, here my capistrano task for update cron tab :

after "deploy:update_code" do
 run "cd #{release_path} && whenever --clear-crontab RAILS_ENV=production"
 run "cd #{release_path} && whenever --update-crontab RAILS_ENV=production"
end

And my schedule.rb create cron task like :

# Begin Whenever generated tasks for: RAILS_ENV=production
PATH=/home/admin/.rvm//gems/ruby-1.9.2-p180@admin/bin:/home/admin/.rvm//gems/ruby-1.9.2-p180@global/bin:/home/admin/.rvm//rubies/ruby-1.9.2-p180/bin:/home/admin/.rvm//bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

* * * * * cd /home/admin/releases/20120904103658 && RAILS_ENV=production rake db:populate:stocks --trace >> /home/admin/shared/log/cron.log 2>> /home/admin/shared/log/cron-error.log

THE PROBLEM is the cron task failed to execute the rake task, from cron-error.log :

/home/admin/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/site_ruby/1.9.1/rubygems.rb:314:in `bin_path': can't find gem rake ([">= 0"]) with executable rake (Gem::GemNotFoundException)
from /home/admin/.rvm//gems/ruby-1.9.2-p180@admin/bin/rake:19:in `<main>'

What i'm missing here, why the cron job failed to load my env ? Is there any problem with my schedule.rb ?

3

3 Answers

1
votes

Make sure your RVM defaults to the correct ruby version where the gems are. Then try to use bundle exec rake ... by doing so it will explicitly invoke whatever the gems in the bundle (assuming you are using RVM)

1
votes

I have similar problem and I fix with

rvm cron setup

in server. This add rvm env variables (PATH, GEM_HOME,GEM_PATH...) to my cronjob.

I have to delete the set command

env :PATH, ENV['PATH']
0
votes

modifying PATH is not enough, check rvm help cron - there are few options that will help manage rvm in crontab.