【问题标题】:Rake task fails when ran by CronRake 任务在由 Cron 运行时失败
【发布时间】:2020-11-18 11:47:27
【问题描述】:

我有一个 rake 任务,当我从应用程序目录中的命令行运行它时,它运行良好,但是当 cron 运行它时,它输出以下错误:

/var/lib/gems/2.6.0/gems/bundler-1.17.3/lib/bundler/spec_set.rb:91:in `block in materialize': Could not find spring-2.1.0 in any of the sources (Bundler::GemNotFound)
    from /var/lib/gems/2.6.0/gems/bundler-1.17.3/lib/bundler/spec_set.rb:85:in `map!'
    from /var/lib/gems/2.6.0/gems/bundler-1.17.3/lib/bundler/spec_set.rb:85:in `materialize'
    from /var/lib/gems/2.6.0/gems/bundler-1.17.3/lib/bundler/definition.rb:170:in `specs'
    from /var/lib/gems/2.6.0/gems/bundler-1.17.3/lib/bundler/definition.rb:237:in `specs_for'
    from /var/lib/gems/2.6.0/gems/bundler-1.17.3/lib/bundler/definition.rb:226:in `requested_specs'
    from /var/lib/gems/2.6.0/gems/bundler-1.17.3/lib/bundler/runtime.rb:108:in `block in definition_method'
    from /var/lib/gems/2.6.0/gems/bundler-1.17.3/lib/bundler/runtime.rb:20:in `setup'
    from /var/lib/gems/2.6.0/gems/bundler-1.17.3/lib/bundler.rb:107:in `setup'
    from /var/lib/gems/2.6.0/gems/bundler-1.17.3/lib/bundler/setup.rb:20:in `<top (required)>'
    from /usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'

这是 cronjob:

*/15 * * * * cd /var/www/myapp && /usr/local/bin/bundle exec /usr/local/bin/rake myapp:heartbeat RAILS_ENV=production > /var/www/myapp/log/heartbeat.log 2>&1

cronjob 过去一直在工作,但我最近将 ruby​​ 升级到了 2.6,我猜这个过程中出了点问题。

如果我从我得到的应用程序文件夹之外运行以下命令:

$ bundle show spring
Could not locate Gemfile or .bundle/ directory

但我从 app 文件夹中得到:

$ bundle show spring
/var/www/myapp/vendor/cache/ruby/2.6.0/gems/spring-2.1.0 

但考虑到 cronjob 首先将 cds 放入 app 文件夹,我猜这应该不是问题。知道发生了什么以及如何解决它吗?

更多细节:

$ ruby -v
ruby 2.6.5p114 (2019-10-01 revision 67812) [x86_64-linux-gnu]

$ bundle -v
Bundler version 1.17.3

$ gem env
RubyGems Environment:
  - RUBYGEMS VERSION: 3.0.3
  - RUBY VERSION: 2.6.5 (2019-10-01 patchlevel 114) [x86_64-linux-gnu]
  - INSTALLATION DIRECTORY: /var/lib/gems/2.6.0
  - USER INSTALLATION DIRECTORY: /home/julien/.gem/ruby/2.6.0
  - RUBY EXECUTABLE: /usr/bin/ruby2.6
  - GIT EXECUTABLE: /usr/bin/git
  - EXECUTABLE DIRECTORY: /usr/local/bin
  - SPEC CACHE DIRECTORY: /home/julien/.gem/specs
  - SYSTEM CONFIGURATION DIRECTORY: /etc
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86_64-linux
  - GEM PATHS:
     - /var/lib/gems/2.6.0
     - /home/julien/.gem/ruby/2.6.0
     - /usr/lib/x86_64-linux-gnu/rubygems-integration/2.6.0
     - /usr/share/rubygems-integration/2.6.0
     - /usr/share/rubygems-integration/all
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :backtrace => false
     - :bulk_threshold => 1000
  - REMOTE SOURCES:
     - https://rubygems.org/
  - SHELL PATH:
     - /home/julien/bin
     - /home/julien/.local/bin
     - /usr/local/sbin
     - /usr/local/bin
     - /usr/sbin
     - /usr/bin
     - /sbin
     - /bin
     - /usr/games
     - /usr/local/games

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 16.04.5 LTS
Release:    16.04
Codename:   xenial

【问题讨论】:

  • 看起来你在你的项目中配置了 rvm 来改变你的 ruby​​ 版本和 gemset,所以比较你的项目内和项目外的 ruby -v 和 gem env 的结果,你会发现不同,如果有不同,您必须为外部设置默认的 ruby​​ 相同的 ruby​​ 和 gemset。配置可以是ruby-version 等
  • 我使用的是 rvm,但在升级 ruby​​ 时将其删除。 ruby -v和gem env在项目文件夹内外完全一样

标签: ubuntu ruby-on-rails-4 cron rake bundler


【解决方案1】:

所以终于设法让它工作了,我在 crontab 中设置了环境变量并从作业命令中删除了完整路径,它终于工作了,这是工作版本的工作方式:

/etc/crontab

SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOME=/root

*/15 * * * * root cd /var/www/myapp/ && bundle exec rake myapp:heartbeat RAILS_ENV=production > /var/www/myapp/log/heartbeat.log 2>&1

【讨论】:

    猜你喜欢
    • 2011-07-27
    • 2014-12-09
    • 1970-01-01
    • 2014-05-27
    • 2015-04-02
    • 2016-11-03
    • 2012-09-02
    • 2011-12-24
    • 1970-01-01
    相关资源
    最近更新 更多