Using whenever gem with sinatra - ruby

#config/scheduler.rb
set :output, 'log/cron.log'
every 1.minutes do
rake 'reports:generate'
end
When I run crontab -l I get the following output:
* * * * * /bin/bash -l -c 'cd <path-to-app> bundle exec rake reports:generate --silent >> log/cron.log 2>&1'
First off, it seems to use my system ruby which is 1.8.7, is there a way to change this?
Secondly, the task is not run, but the only thing that shows up in my cron.log is the following:
Your Ruby version is 1.8.7, but your Gemfile specified 2.0.0
I've tried puts to the log with no success, so I have basically no idea what's going on. It seems the task is being initiated, but then fails without anything being written to the log. I have no idea how to go about debugging this, so please help.
I'm on OS X, and my application is running on Sinatra.
edit: I'm using rbenv to manage my ruby versions, so I guess I have to tell the cron job to load rbenv somehow? When I cd into the app's folder and run ruby -v I get
ruby 2.0.0p353 (2013-11-22 revision 43784) [x86_64-darwin12.5.0]

It seems the problem was with rbenv, and not necessarily sinatra or whenever. This solved my problem:
#schedlue.rb
command "cd #{Dir.pwd} && RACK_ENV=#{environment} rbenv exec bundle exec rake reports:generate"
By adding rbenv exec we successfully loaded the correct ruby version before running the rake task.

Related

turn off ruby version warning

Ubuntu 18.04, ruby 2.6.6
Cron jobs submitted for redmine generate warnings that are then mailed to admin, creating a steady stream of emails every 15 minutes:
Subject: Cron <root#mysys> su -l rubyuser -c " cd /opt/redmine/redmine ; bundle exec rake --trace redmine:email:receive_imap RAILS_ENV="production" host=imap.gmail.com port=993 ssl=1 username=issueadmin#someplace.com password=pw folder=Inbox move_on_success=Processed project=Issues status=open tracker=Bug priority=Normal >> /opt/redmine/redmine/log/issues.log 2>&1"
The mail contains the usual ruby warning message:
Unknown ruby interpreter version (do not know how to handle): >=2.3.0,<2.7.0ifBundler::VERSION>=1.12.0.
I have tried turning off the warning using
rvm rvmrc warning ignore allGemfiles
su -l rubyuser -c " cd /opt/redmine/redmine ; rvm rvmrc warning ignore allGemfiles"
but it has no effect.
Can someone tell me how to fix this / turn this off?
This is a system-wide ruby, installed at /usr/local/rvm. It appears to be caused by the Gemfile installed at /opt/redmine/redmine, which contains the line
ruby '>= 2.3.0', '< 2.7.0' if Bundler::VERSION >= '1.12.0'
I've tried tweaking that line a bit but I still get the error.
Matthew Boeh gave me the following answer which solves the immediate issue:
It looks like this is an issue with RVM, which tries to read the
Gemfile to determine which version of Ruby to switch to. It's limited
in what it can parse. I believe if you put a .ruby-version or .rvmrc
in that directory it will ignore the Gemfile.
I added "2.6.6" to a .ruby-version for the project.
The reason may be slightly more nuanced, as rvm doesn't consider 2.6.6 an "official" release for some reason, which may be the reason it complains. It does consider 2.7.0 one. At least that's my recollection; don't have access to the machine right now and I would need to install a 2.7 to find out.

Running bundle exec rake inside cron cannot find specific version of rake

I am setting up cron to run some rake tasks for my ruby on rails server. They are generated by whenever and look like this:
* * * * * /bin/bash -l -c 'cd /var/www && RAILS_ENV=production /usr/local/bin/bundle exec /usr/local/bin/rake my:task --silent >> /var/www/log/cron.log 2>&1'
bundle and rake executables seem to be found, but inside cron.log I find this error:
bundler: failed to load command: /usr/local/bin/rake (/usr/local/bin/rake)
Bundler::GemNotFound: Could not find rake-12.3.2 in any of the sources
/usr/local/lib/ruby/gems/2.5.0/gems/bundler-1.16.6/lib/bundler/spec_set.rb:91:in `block in materialize'
...
...
As I can see it tried to load rake-12.3.2, which is a correct version, from /usr/local/lib/ruby/gems/2.5.0/gems, which is not a correct path, because there is no rake-12.3.2. Correct version of rake is under another path /usr/local/bundle/gems, but I have no luck setting this path for cron to use.
I have tried adding GEM_PATH="/usr/local/bundle/gems" to cron file, but it doesn't work.
I am not using rvm or rbenv, ruby is installed directly.

How to fix "Unknown ruby interpreter version (do not know how to handle): RUBY_VERSION."

Today i just want to setup a jekyll Blog on my Mac, and have already install ruby 2.3.0, but when make '$ jekyll serve', it's error. and show this in terminal:
Error information:
Unknown ruby interpreter version (do not know how to handle): RUBY_VERSION.
So it looks like
bundle exec jekyll new
will create the a Gemfile with the line
ruby RUBY_VERSION
I believe you'll want to edit that file to be e.g.
ruby '2.1.1'
Coincidentally today I am also trying to setup Jekyll and am seeing the same problem. I am using RVM and it otherwise works fine (running multiple Rails dev sites locally). When I run env | grep 'RUBY' I get:
$ env | grep 'RUBY'
MY_RUBY_HOME=/Users/myusername/.rvm/rubies/ruby-2.0.0-p247
RUBY_VERSION=ruby-2.0.0-p247
However, I just continued and ran bundle install, then bundle exec jekyll serve and the site booted up without issue.
Seems like there is no variable RUBY_VERSION in your env.
Try in your shell: env | grep 'RUBY'
Output should be like this:
RUBY_VERSION=ruby-2.3.0
If you don't see anything, you need to reinstall ruby.
Use RVM or other ruby version manager. Here is a good manual
I used to have ruby '~> 2.6.3' in Gemfile and got same warning.
I changed it to ruby '2.6.3' and it fixed it.

Why bundle exec rake does not run to proper version of ruby

I am currently having a strange issue with bundler and ruby.
if I type:
$ which ruby
I get:
/home/martinos/.rubies/1.8.7-p370/bin/ruby
And when I type:
$ which bundle
I get
/home/martinos/.gem/ruby/1.8.7/bin/bundle
But for some reason when I run
$ bundle exec rake db:migrate
The task is run with ruby 1.9.3 (I have written a puts RUBY_VERSION in environment.rb)
Any one as an idea why this happens?
Here is more infos:
When I type:
$ which rake
I get:
/home/martinos/.gem/ruby/1.8.7/bin/rake
But if I
$ head -1 `which rake`
I get:
#!/usr/bin/env ruby1.9.1
There are a variety of pieces that could be in play. The first is that it could be a conflict between your Ruby version management tools and your global gems. Meaning, I suppose it is possible that you only have a Rake version that can work on Ruby 1.9.1 that is in your global set. So when you fire up Rake it is forced to run in Ruby 1.9.1.
What you may want to do is create a directory specific gemset. If you're using RVM you can see the documentation on how to do that by looking at their Gemset documentation. Once that is in place with the Ruby version you want to test with, then do a gem install of Rake at the version that will work with that Ruby version. At that point you should find that the Ruby version being used to run Rake in that directory will be the same as the version you have running.
I apologize if this does not answer your question, or if you have thought of this approach already. Trying to wrap my head around this without the ability to reproduce the problem is a tricky deal.

Ruby - No gem to load in cronjob

I use rvm. and ruby 1.9.2 is installed in rvm.
I have a script and it runs fine in terminal.
I have wrote a cron job for the same script:
27 * * * * su - sayuj -c 'cd /path/to/dir; /home/sayuj/.rvm/rubies/ruby-1.9.2-p136/bin/ruby script.rb >> /var/log/script.log 2>&1'
Then it shows no gem is to load error. I think the cron is not referring to the rvm ruby 1.9.2.
How can I fix this issue?
You need to change cd /path/to/dir; to the real directory.
Then, create a file named .rvmrc with this content inside:
rvm use 1.9.2
It'll execute every time you enter the directory, setting to the correct version of ruby.

Resources