每当gem'无法加载命令时:rake'

时间:2016-08-08 21:40:04

标签: ruby docker cron ruby-on-rails-3.2 whenever

寻求帮助。

我正在使用whenever gem(v0.9.7)和rake(v11.2.2)运行rails app(v3.2.5)。我也在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作业运行时,日志返回:

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 job命令:

/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而我得到相同的错误..

任何帮助都是适当的

3 个答案:

答案 0 :(得分:5)

我通过修改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) }

答案 1 :(得分:0)

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

sudo bundle update rake
sudo bundle install

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

答案 2 :(得分:-1)

我通过使用不同的图像并根据需要构建而不是使用docker hub image 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"]