Missing required gems when execute cron created by whenever gem in production environment

末鹿安然 提交于 2019-12-08 00:46:49

问题


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 ?


回答1:


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)




回答2:


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']



回答3:


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



来源:https://stackoverflow.com/questions/12274875/missing-required-gems-when-execute-cron-created-by-whenever-gem-in-production-en

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!