Whenever gem 'failed to load command: rake'
Asked Answered
D

3

7

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

Dioecious answered 8/8, 2016 at 21:40 Comment(3)
man i am stuck on the same error it has nothing to do with docker, if you do rake --help, it will run fine but for other rake tasks it gives this errorIrrevocable
Can you do find / -name "gems" and show me the outputIrrevocable
I just answered my issue below in this thread. Please review @IrrevocableDioecious
U
1

I have updated my rake version and it worked for me. Below are the steps I followed:

sudo bundle update rake
sudo bundle install

Open a Rakefile and replace the line rake/rdoctask with require 'rdoc/task'.

Unready answered 17/9, 2016 at 17:50 Comment(0)
M
10

I've fixed the same error by modifying the Dockerfile with:

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

And schedule.rb:

ENV.each { |k, v| env(k, v) }
Mulligatawny answered 7/5, 2017 at 13:56 Comment(2)
Adding the Environment Variables to cronjob solves the problem.Nieshanieto
+1 on the ENV.each { ... } — that did the trick. I'd been fussing with individual env variables and not sure why I didn't think of that (much easier) approachLouvre
U
1

I have updated my rake version and it worked for me. Below are the steps I followed:

sudo bundle update rake
sudo bundle install

Open a Rakefile and replace the line rake/rdoctask with require 'rdoc/task'.

Unready answered 17/9, 2016 at 17:50 Comment(0)
D
0

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"]
Dioecious answered 22/8, 2016 at 22:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.