Could not find rake using whenever rails

2019-07-10 12:47发布

问题:

Hello I have a strange problem, I'm trying to get rake task working in whenever, but have this problem Could not find rake-10.4.2 in any of the sources (Bundler::GemNotFound). It's strange, because it searches this rake version in ruby 2.1.2 version /home/vyivrain/.rvm/rubies/ruby-2.1.2/lib/ruby/gems/2.1.0/gems/bundler-1.6.2/lib/bundler/spec_set.rb:92. However I'm using ruby 2.2.0 in rvm and the gem list is also using that version.
It's a simple cron job:

every 1.minute do
  rake 'process_email:handle', output: 'log/mail.log', environment: 'development'
end

Rakefile:

namespace :process_email do
  desc 'Handle email'
  task handle: :environment do
    MOBIZARD_MAILER.processor.retrieve_mail
  end
end

Mobizard mailer is my own gem, that retrieves mails through ruby mail gem, that uses same ruby 2.2.0 and same gem list.
Whole stack trace in mail.log looks like this:

/home/vyivrain/.rvm/rubies/ruby-2.1.2/lib/ruby/gems/2.1.0/gems/bundler-1.6.2/lib/bundler/spec_set.rb:92:in `block in materialize': Could not find rake-10.4.2 in any of the sources (Bundler::GemNotFound)
    from /home/vyivrain/.rvm/rubies/ruby-2.1.2/lib/ruby/gems/2.1.0/gems/bundler-1.6.2/lib/bundler/spec_set.rb:85:in `map!'
    from /home/vyivrain/.rvm/rubies/ruby-2.1.2/lib/ruby/gems/2.1.0/gems/bundler-1.6.2/lib/bundler/spec_set.rb:85:in `materialize'
    from /home/vyivrain/.rvm/rubies/ruby-2.1.2/lib/ruby/gems/2.1.0/gems/bundler-1.6.2/lib/bundler/definition.rb:133:in `specs'
    from /home/vyivrain/.rvm/rubies/ruby-2.1.2/lib/ruby/gems/2.1.0/gems/bundler-1.6.2/lib/bundler/definition.rb:178:in `specs_for'
    from /home/vyivrain/.rvm/rubies/ruby-2.1.2/lib/ruby/gems/2.1.0/gems/bundler-1.6.2/lib/bundler/definition.rb:167:in `requested_specs'
    from /home/vyivrain/.rvm/rubies/ruby-2.1.2/lib/ruby/gems/2.1.0/gems/bundler-1.6.2/lib/bundler/environment.rb:18:in `requested_specs'
    from /home/vyivrain/.rvm/rubies/ruby-2.1.2/lib/ruby/gems/2.1.0/gems/bundler-1.6.2/lib/bundler/runtime.rb:13:in `setup'
    from /home/vyivrain/.rvm/rubies/ruby-2.1.2/lib/ruby/gems/2.1.0/gems/bundler-1.6.2/lib/bundler.rb:120:in `setup'
    from /home/vyivrain/.rvm/rubies/ruby-2.1.2/lib/ruby/gems/2.1.0/gems/bundler-1.6.2/lib/bundler/setup.rb:17:in `<top (required)>'
    from /home/vyivrain/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /home/vyivrain/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'

Thx for answering.

回答1:

It is because the output is set with the -l flag, which is incorrectly interpreted by rvm.

You can read more about this issue here:

https://github.com/javan/whenever/issues/325



回答2:

TL;DR

Add this to schedule.rb

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

Ref: https://github.com/javan/whenever/issues/656


My guess is the GEM_PATH is not the same when running in a crontab and when running in your normal/dev environment. You could check it which this command

gem which rake

In my cases:

  • When running in crontab GEM_PATH would be something like /usr/local/lib/ruby/site_ruby/2.3.0/bundle
  • When running in "normal", the GEM_PATH is /usr/local/bundle

This script ENV.each { |k, v| env(k, v) } will auto add those envs to make sure ruby using the right GEM_PATH


Quote from this answer https://stackoverflow.com/a/20499839/1819549

The -l option (according to the man page) makes "bash act as if it had been invoked as a login shell". Login shells read certain initialization files from your home directory, such as .bash_profile. Since you set the value of TEST in your .bash_profile, the value you set on the command line gets overridden when bash launches.

So @Yury's answer works maybe because the command executes something in .bash_profile and I guess it update the GEM_PATH for rvm.