每当 gem 'failed to load command: rake'

Whenever gem 'failed to load command: rake'

寻求帮助。

我正在 运行使用 whenever gem (v0.9.7) 和 rake (v11) 安装 rails 应用程序 (v3.2.5) .2.2).我也在 docker 容器映像 ruby:2.3 中执行此操作(安装了 cron 并且 bundle install 是 运行)

这是我的 schedule.rb

set :environment, ENV['RAILS_ENV']

every '*/2 9,10,11,12,13,14,15,16 * * 1-5' do
    rake "import_csv", output: {:error => 'log/import_csv_errors.log', :standard => 'log/import_csv.log'}'
end

注意 RAILS_ENV 在容器启动时设置为 development

这是构建后容器上的我的 cron 作业 (crontab -l):

# Begin Whenever generated tasks for: /usr/src/app/config/schedule.rb
*/2 9,10,11,12,13,14,15,16 * * 1-5 /bin/bash -l -c 'cd /usr/src/app && RAILS_ENV=development bundle exec rake import_csv --silent >> log/import_csv.log 2>> log/import_csv_errors.log'

# End Whenever generated tasks for: /usr/src/app/config/schedule.rb

当此 cron 作业 运行s 时,日志 return:

import_csv_errors.log

Bundler::GemNotFound: Could not find rake-11.2.2 in any of the sources
  /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/spec_set.rb:95:in `block in materialize'
  /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/spec_set.rb:88:in `map!'
  /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/spec_set.rb:88:in `materialize'
  /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/definition.rb:140:in `specs'
  /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/definition.rb:185:in `specs_for'
  /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/definition.rb:174:in `requested_specs'
  /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/environment.rb:19:in `requested_specs'
  /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/runtime.rb:14:in `setup'
  /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler.rb:95:in `setup'
  /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/setup.rb:19:in `<top (required)>'
  /usr/local/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
  /usr/local/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'

import_csv.log

bundler: failed to load command: rake (/usr/local/bin/rake)

奇怪的是。如果我复制 cron 作业命令:

/bin/bash -l -c 'cd /usr/src/app && RAILS_ENV=development bundle exec rake import_csv --silent >> log/import_csv.log 2>> log/import_csv_errors.log'

和 运行 这在容器中,它工作正常,但如果 cron 作业 运行 它,我在日志中得到那些错误!!!我在这里迷路了...

我试过添加

env :PATH, ENV['PATH']
env :GEM_PATH, '/usr/local/bundle'

schedule.rb 的顶部,我尝试做

command 'cd /usr/src/app && RAILS_ENV=development bundle exec rake import_csv --silent >> log/import_csv.log 2>> log/import_csv_errors.log'

而不是在任务中使用 rake,我得到了同样的错误..

任何帮助都适用

我通过使用不同的图像并根据需要构建来解决我的问题,而不是使用 docker 中心图像 ruby:2.x

Dockerfile(已编辑以适合线程):

FROM ubuntu:14.04

# Installs needed to run rails on ubuntu 14.04 (must use mysql 5.6 or 5.5):
RUN apt-get update && apt-get install -y apache2 curl git build-essential libmysqlclient-dev mysql-server-5.6 nodejs make
RUN apt-get update && apt-get install -y ruby-dev zlib1g-dev
RUN gem install rails --version 3.2.5 --no-ri --no-rdoc

# Update ruby to v2.2 (optional)
RUN apt-get install -y software-properties-common && apt-add-repository ppa:brightbox/ruby-ng
RUN apt-get update && apt-get install -y ruby2.2

# Install cron
RUN apt-get install -y cron

# Finish the build
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

# If you code is in the same directory you ran docker build then:
COPY . /usr/src/app
RUN bundle install
RUN whenever --update-crontab
CMD ["passenger start"]

我已经更新了我的 rake 版本并且它对我有用。以下是我遵循的步骤:

sudo bundle update rake
sudo bundle install

打开 Rakefile 并将行 rake/rdoctask 替换为 require 'rdoc/task'

我通过修改 Dockerfile 修正了同样的错误:

RUN gem update --system 2.6.12
RUN gem install bundler --version 1.14.6

schedue.rb

ENV.each { |k, v| env(k, v) }