Awatatah Awatatah - 3 months ago 122
Ruby Question

Whenever gem 'failed to load command: rake'

looking for some help.

I am running a rails app (v3.2.5) with the

whenever
gem (v0.9.7) and
rake
(v11.2.2). I am also doing this in a docker container image
ruby:2.3
(
cron
was installed and
bundle install
was ran)

Here is my
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


note
RAILS_ENV
is set at container launch to
development


Here is my cron job that is on the container after build (
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


When this cron job runs, the logs 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)


Now here is the odd thing. If I copy the cron job command:

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


and run this in the container, it works fine, but if the cron job runs it, I get thos errors in the logs!!! I am at a lost here...

I've tried adding

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


to the top of
schedule.rb
and I tried doing

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


Instead of using
rake
in the task and I get the same errors..

Any help is appriciated

Answer

I resolved my issue by using a different image and building as I needed it instead of using docker hub image ruby:2.x.

Dockerfile (edited to fit thread):

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