GemNotFound rake when using whenever in development - ruby-on-rails

I'm getting this one error I don't understand (I'm fairly new to rails) when using the gem 'whenever' in development mode.
What I did was update config/schedule.rb and run
whenever --update-crontab --set environment='development'
crontab -l lists:
* * * * * /bin/bash -l -c 'cd /home/vic/Desktop/WorkflowProject && bundle exec bin/rails runner -e development '\''Task.new2'\'' >> log/whenever.log 2>&1'
at config/schedule.rb
set :output, 'log/whenever.log'
every 1.minute do
runner "Task.new2"
end
at log/whenever.log
bundler: failed to load command: bin/rails (bin/rails)
Bundler::GemNotFound: Your bundle is locked to rake (11.3.0), but that version could not be found in any of the sources listed in your Gemfile. If you haven't changed sources, that means the author of rake (11.3.0) has removed it. You'll need to update your bundle to a different version of rake (11.3.0) that hasn't been removed in order to install.
/home/vic/.rvm/gems/ruby-2.3.1/gems/bundler-1.13.2/lib/bundler/definition.rb:179:in `rescue in specs'
/home/vic/.rvm/gems/ruby-2.3.1/gems/bundler-1.13.2/lib/bundler/definition.rb:173:in `specs'
/home/vic/.rvm/gems/ruby-2.3.1/gems/bundler-1.13.2/lib/bundler/definition.rb:233:in `specs_for'
/home/vic/.rvm/gems/ruby-2.3.1/gems/bundler-1.13.2/lib/bundler/definition.rb:222:in `requested_specs'
/home/vic/.rvm/gems/ruby-2.3.1/gems/bundler-1.13.2/lib/bundler/runtime.rb:118:in `block in definition_method'
/home/vic/.rvm/gems/ruby-2.3.1/gems/bundler-1.13.2/lib/bundler/runtime.rb:19:in `setup'
/home/vic/.rvm/gems/ruby-2.3.1/gems/bundler-1.13.2/lib/bundler.rb:99:in `setup'
/home/vic/.rvm/gems/ruby-2.3.1/gems/bundler-1.13.2/lib/bundler/setup.rb:20:in `<top (required)>'
/home/vic/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
/home/vic/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
I would grealy appreciate the help, thank you very much!

Try the below commands.
gem install rubygems-bundler
gem regenerate_binstubs
Ref: this

Are you sure to have bundler installed? Try running bundle install inside your application directory.
If no, see Jayaprakash's answer above.
If yes, try bundle exec whenever --update-crontab --set environment='development'.
Update:
Ahh, missed that you are seeing error when your task executes.
Try this in your config/schedule.rb
env :PATH, ENV['PATH']
set :output, "#{ Whenever.path }/log/whenever.log"
every 1.minute do
runner "Task.new2"
end

Related

cant find gem bundler when running cron with whenever?

I am using whenever gem to write a simple cron job like.
every 1.minute do
runner "ValidModel.clean"
end
I see that this cron fires at the right time but the rails command ValidModel.clean doesn't succeed. It gives the following error.
/home/deploy/.rvm/rubies/ruby-2.4.2/lib/ruby/site_ruby/2.4.0/rubygems.rb:271:in `find_spec_for_exe': can't find gem bundler (>= 0.a) (Gem::GemNotFoundException)
from /home/deploy/.rvm/rubies/ruby-2.4.2/lib/ruby/site_ruby/2.4.0/rubygems.rb:299:in `activate_bin_path'
from /home/deploy/.rvm/gems/ruby-2.4.2/bin/bundle:23:in `<main>'
from /home/deploy/.rvm/gems/ruby-2.4.2/bin/ruby_executable_hooks:15:in `eval'
from /home/deploy/.rvm/gems/ruby-2.4.2/bin/ruby_executable_hooks:15:in `<main>'
I am using RVM. Is this issue related with using RVM with whenever? I appreciate any help!
UPDATE:
In gemfile i have put
gem 'whenever', require: false
when i run crontab -l it lists the following
PATH=/home/deploy/apps/app/shared/bundle/ruby/2.4.0/bin:/home/deploy/.rvm/gems/ruby-2.4.2/bin:/home/deploy/.rvm/gems/ruby-2.4.2#global/bin:/home/deploy/.rvm/rubies/ruby-2.4.2/bin:/home/deploy/.rvm/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
GEM_PATH=""
* * * * * /bin/bash -l -c 'cd /home/deploy/apps/app/releases/20180121061036 && bundle exec bin/rails runner -e production '\''ValidModel.clean'\'' >> /home/deploy/apps/app/releases/20180121061036/log/cron.log 2>&1'
I appreciate any insights! Thanks a lot!

Could not find rake using whenever rails

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.
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
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.

Gemfile not found when running Cron job with Capistrano 3 and whenever gem

My cron job works fine on my local machine after running whenever -w, after deploy to my VPS, I get this error, release 20150415044915 doesn't exist. any idea?
I looked at my crontab -e, the path also looks fine where 20150502114703 is the correct release:
0 1 * * 1 /bin/bash -l -c 'cd /home/hey_production/releases/20150502114703 && bin/rails runner ....
Error Log:
/usr/local/rvm/gems/ruby-2.1.3/gems/bundler-1.7.3/lib/bundler/definition.rb:22:in `build': /home/hey_production/releases/20150415044915/Gemfile not found (Bundler::GemfileNotFound)
from /usr/local/rvm/gems/ruby-2.1.3/gems/bundler-1.7.3/lib/bundler.rb:154:in `definition'
from /usr/local/rvm/gems/ruby-2.1.3/gems/bundler-1.7.3/lib/bundler.rb:117:in `setup'
from /usr/local/rvm/gems/ruby-2.1.3/gems/bundler-1.7.3/lib/bundler/setup.rb:17:in `<top (required)>'
from /usr/local/rvm/rubies/ruby-2.1.3/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:135:in `require'
from /usr/local/rvm/rubies/ruby-2.1.3/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:135:in `rescue in require'
from /usr/local/rvm/rubies/ruby-2.1.3/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:144:in `require'
from bin/rails:14:in `<main>'
Basically the environment variable is missing that tells the cron where to look for a Gemfile. so you need to add that variable in your environment at the time when cron tries to run this.
You can do that in Your schedule.rb:
env BUNDLE_GEMFILE, ENV["/home/hey_production/current/Gemfile"]
or directly inside crontab file with the command crontab -e(before the cron entries):
BUNDLE_GEMFILE="/home/hey_production/current/Gemfile"
Hope it helps.
EDIT
Forgot the symbol above in schedule.rb
The line in schedule.rb should be like this.
env :BUNDLE_GEMFILE, ENV["/#{path}/Gemfile"]
or
env :BUNDLE_GEMFILE, ENV["/home/hey_production/current/Gemfile"]
As a follow up on previous answer, set the env variable in whenever inside schedule.rb before the schedule blocks like:
def production?
#environment == 'production'
end
set :output, {:error => '/home/current/log/cron_error.log', :standard => '/home/current/log/cron.log'}
every 2.hour, roles: [:utility] do
runner "/home/current/lib/cron_jobs/launch_pending_emails.rb"
end
and inside your deploy.rb environment specific file ie: staging.rb set the env:
set :whenever_roles, [:utility]
set :whenever_environment, defer { stage }
set(:whenever_command) { "STAGE=#{stage} bundle exec whenever" }
require 'whenever/capistrano'

Rails Deployment with capistrano and start resque workers

I'm using capistrano 3.2.1 with capistrano-resque gem to restart rasque workers on deployment. Always getting this error.
If I start workers manually under current directory on production server it starts fine.
cd /home/azureuser/apps/app_production/current && ( RBENV_ROOT=~/.rbenv RBENV_VERSION=2.1.1 /usr/bin/env nohup RBENV_ROOT=~/.rbenv RBENV_VERSION=2.1.1 ~/.rbenv/bin/rbenv exec bundle exec rake RACK_ENV=production RAILS_ENV=production QUEUE="mailer" PIDFILE=/home/azureuser/apps/app_production/shared/tmp/pids/resque_work_1.pid BACKGROUND=yes VERBOSE=1 INTERVAL=5 environment resque:work >> log/resque.log 2>> log/resque.log )
cap aborted!
SSHKit::Runner::ExecuteError: Exception while executing as azureuser#thesend.cloudapp.net: nohup exit status: 127
nohup stdout: Nothing written
nohup stderr: Nothing written
/Library/Ruby/Gems/2.0.0/gems/sshkit-1.7.1/lib/sshkit/runners/parallel.rb:16:in `rescue in block (2 levels) in execute'
/Library/Ruby/Gems/2.0.0/gems/sshkit-1.7.1/lib/sshkit/runners/parallel.rb:12:in `block (2 levels) in execute'
SSHKit::Command::Failed: nohup exit status: 127
After I worked out that capistrano-rbenv was the problem, I removed that gem from my gemfile and also my capfile.
I then just added the following to my deploy.rb file:
set :default_env, {
PATH: "$HOME/.rbenv/shims:$HOME/.rbenv/bin:$PATH",
RBENV_ROOT: '~/.rbenv',
RBENV_VERSION: '2.2.2'
}
Now capistrano-resque works correctly as does unicorn etc..
I fix this based on the #archfish recommendation on capistrano-resque incompatibility between capistrano-resque and capistrano-rbenv issue. Then you should add or modify the :rbenv_prefix on deploy.rb, like:
set :rbenv_prefix, "env RBENV_ROOT=#{fetch(:rbenv_path)} RBENV_VERSION=#{fetch(:rbenv_ruby)} #{fetch(:rbenv_path)}/bin/rbenv exec"
Note: the :rbenv_prefix usually is used like:
set :rbenv_prefix, "RBENV_ROOT=#{fetch(:rbenv_path)} RBENV_VERSION=fetch(:rbenv_ruby)} #{fetch(:rbenv_path)}/bin/rbenv exec"

rake aborted! You have already activated rake 10.0.2, but your Gemfile requires rake 0.9.2.2

I am trying to do "rake db:migrate" and it is giving me this error.
Andy:AcademyAir Andy$ rake db:migrate
/Users/Andy/.rvm/gems/ruby-1.9.2-p290/gems/bundler-1.2.2/lib/bundler/runtime.rb:197: warning: Insecure world writable dir /usr in PATH, mode 040777
rake aborted!
You have already activated rake 10.0.2, but your Gemfile requires rake 0.9.2.2. Using bundle exec may solve this.
/Users/Andy/.rvm/gems/ruby-1.9.2-p290/gems/bundler-1.2.2/lib/bundler/runtime.rb:31:in `block in setup'
/Users/Andy/.rvm/gems/ruby-1.9.2-p290/gems/bundler-1.2.2/lib/bundler/runtime.rb:17:in `setup'
/Users/Andy/.rvm/gems/ruby-1.9.2-p290/gems/bundler-1.2.2/lib/bundler.rb:116:in `setup'
/Users/Andy/.rvm/gems/ruby-1.9.2-p290/gems/bundler-1.2.2/lib/bundler/setup.rb:7:in `<top (required)>'
/Users/Andy/Desktop/AcademyAir/config/boot.rb:6:in `<top (required)>'
/Users/Andy/Desktop/AcademyAir/config/application.rb:1:in `<top (required)>'
/Users/Andy/Desktop/AcademyAir/Rakefile:5:in `<top (required)>'
(See full trace by running task with --trace)
Try this bundle exec rake db:migrate
Remove rake 10.0.2 gem
By using gem uninstall rake and remove 10.0.2 version of rake gem
And Then Try rake db:migrate
Why Error comes : In you gem list two version (10.0.2 and 0.9.2.2) of rake gem install so that this error comes.*
Another Solution is you can do bundle update
For me, I just ran bundle update and everything works right again.
I was able to solve this by opening up Gemfile and changing gem 'rake', '~> 0.9.2.2' to gem 'rake', '~> 10.0.1'
In your Gemfile, explicitly set the latest rake version by:
gem 'rake', '~> 10.0.1'
And then run
$ bundle update rake
Then try
I've just ran into the same problem.
I inserted in my gemfilen gem 'rake', '~> 10.0.1' [in your case it should be '10.0.2']
I deleted my gemfile lock
I ran rake db:migration again and it worked.
I got this tip here: Activated Ruby RAKE 10.0.1, require 10.0.0
Update: In my case I didn't have rake duplicated. I just have in my gems the 10.0.1 version.
Solved the same issue by running:
bundle update
This will update your rake gem to the latest version and allow you to run the migration.
If you are using a gemset: be sure to run bundle install after you've updated rake to update your local gemset as well.
It happens because you are using rake from the system. (latest version by default)
The solution is use follow command:
bundle exec rake db:migrate
Also, you can create alias. Because this command is too big and difficult to write.
echo "alias be='bundle exec'" >> ~/.bash_profile
source ~/.bash_profile
Then you can use follow short command:
be rake db:migrate
change the version which located both in the gemfile and gemlock to the version number shown in the console, it will be done

Resources