当在生产环境中由gem创建时,执行cron时缺少必需的gem [英] Missing required gems when execute cron created by whenever gem in production environment

查看:198
本文介绍了当在生产环境中由gem创建时,执行cron时缺少必需的gem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有ruby在生产中运行rails应用程序,这个应用程序有 STOCK 模型,它应该每1分钟更新一次。

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

对于更新此模型,我创建简单的rake任务 db:populate:stocks ,当它只包含两个操作:

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


  1. 截断STOCK模型

  2. 使用来自webservice的最新数据提取填充STOCK模型



    这个rake任务应该每隔1分钟执行一次,为此目的,我使用每当gem 。这里是我的 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

$ b在生产中我使用 rvm 运行ruby 1.9.2-p180,rails 3.1.0和capistrano,这里我的capistrano任务更新cron选项卡:

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

和我的 schedule.rb 创建cron任务,如:

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

问题是cron任务失败要从 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>'

我在这里缺少什么,为什么cron作业无法加载我的env?我的 schedule.rb 有什么问题吗?

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

推荐答案

确保您的RVM默认为正确ruby版本中的宝石是。
然后尝试使用bundle exec rake ...
这样做会显式调用bundle中的gem(假设你使用RVM)

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)

这篇关于当在生产环境中由gem创建时,执行cron时缺少必需的gem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆