I can't get the whenever bundler combination work in production.
I'm trying to change the cron jobs to include rbenv:
http://snippets.aktagon.com/snippets/601-running-and-debugging-cronjobs-with-rbenv-and-bundler
Is this something to consider?
Here's how I got cron jobs working for my Capistrano/rbenv/rails setup:
# Set the path to include rbenv
PATH=$PATH:/home/deploy/.rbenv/shims/:/home/deploy/.rbenv/bin/:/bin:/usr/bin/
# Begin Whenever generated tasks for: /home/deploy/grunndata/current/config/schedule.rb
0 * * * * /bin/bash -l -c '.eval "$(rbenv init -)"; cd /home/deploy/grunndata/current && bundle exec rails runner -e production '\''CacheWorker.perform_async'\'''
I commented out whenever in the Capfile, so I have to add new whenever cron jobs myself, after listing them in my development environment and modifying them to fit the pattern above.
The -l
bash option runs an interactive shell, which should execute your startup files. Do you have eval "$(rbenv init -)"
in your .bash_profile?
I have the eval string in my .bashrc and it works fine otherwise.
.bash_profile
is executed for login shells and .bashrc
is executed for interactive non-login shells. So, you'll need to either add rbenv init to your .bash_profile
or modify the job_template
in your schedule.rb
. The easiest option is to source your .bashrc
in your .bash_profile
.
if [ -f ~/.bashrc ]; then
source ~/.bashrc
fi
this post works for me for Ubuntu, on AWS:
http://benscheirman.com/2013/12/using-rbenv-in-cron-jobs.
Eg for a periodical task each 15 minutes:
0,15,30,45 * * * * export PATH=/home/ubuntu/.rbenv/shims:/home/ubuntu/.rbenv/bin:/usr/bin:$PATH; eval "$(rbenv init -)"; cd /var/www/app/current && RAILS_ENV=production bundle exec rake my_task
you can find your rbenv path to replace with 'which rake' command:
eg.
$ which rake
/home/ubuntu/.rbenv/shims/rake
Ubuntu 16.04, rbenv, Capistrano, whenever, Rails 5
$ which rake
/home/deploy/.rbenv/shims/rake
schedule.rb
job_type :rbenv_rake, %Q{export PATH=/home/deploy/.rbenv/shims:/home/deploy/.rbenv/bin:/usr/bin:$PATH; eval "$(rbenv init -)"; \
cd :path && :environment_variable=:environment :bundle_command rake :task --silent :output }
job_type :rbenv_runner, %Q{export PATH=/home/deploy/.rbenv/shims:/home/deploy/.rbenv/bin:/usr/bin:$PATH; eval "$(rbenv init -)"; \
cd :path && :bundle_command :runner_command -e :environment ':task' :output }
#... and so on
# example of use
every 60.minutes do
rbenv_rake "ts:index"
end
It took me hours to finally find this solution. @askrynnikov's solution did not work for me. I'm also on Ubuntu, rebenv, Capistrano, whenever, and Rails 5.
All I needed to do was add env :PATH, ENV['PATH']
to the top of my schedule.rb
.
@austinarchibald's comment worked for me, finally. There should really be a mention of this in the readme for RBENV users.
Add to this the confusion of not really knowing about cron outputs, and it just failing silently...quite the fun 2 hours debugging this.
It took me hours to finally find this solution. @askrynnikov's solution did not work for me. I'm also on Ubuntu, rebenv, Capistrano, whenever, and Rails 5.
All I needed to do was add
env :PATH, ENV['PATH']
to the top of myschedule.rb
.
This worked for me as well. And just like gregblass, it was quite an experience debugging this! I am not sure if I it is a fundamental misunderstanding of the capistrano rbenv gem or some issue with the gem itself, but I had similar issue with delayed_job, where the bin/delayed_job file just would not get the executable permission when copied to the server by capistrano. So I wrote a task which I had run before invoking the delayed_job:restart task.
Actually, @javan gave an excellent explanation(https://github.com/javan/whenever/issues/486#issuecomment-58009257) of what is going on. However, in 2021, on Ubuntu 20.04 I had some mileage variation.
.bash_profile
it is ok to modify .profile
instead..bashrc
for some reason didn't work for me, so I ended up adding those lines from .bashrc
:export PATH="$HOME/.rbenv/bin:$PATH"
export PATH="$HOME/.rbenv/shims:$PATH"
eval "$(rbenv init -)"
export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"
into .profile
But once again - your export path may varies - see your .bashrc
.
On Ubuntu 20.04 this is at the top of ~/.bashrc
:
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac
You could put eval "$(rbenv init -)"
before that part and it'll run in non-interactive mode I'm guessing. I'm trying this now and will report back.
Just reporting back RE https://github.com/javan/whenever/issues/486#issuecomment-850666094
It worked well and now the commands run with the correct ruby environment. I'm not sure if it's an ideal setup since I don't like configurations that are order dependent but the default structure of this file gives us no choice.
Most helpful comment
It took me hours to finally find this solution. @askrynnikov's solution did not work for me. I'm also on Ubuntu, rebenv, Capistrano, whenever, and Rails 5.
All I needed to do was add
env :PATH, ENV['PATH']
to the top of myschedule.rb
.