Execute ruby ​​stone via cron

I'm trying to solve a bit of a riddle here ... We've created a gem that is called via cron.

The secret is this: it works on the machine A

, but not on the B

environment machine as far as I can tell.

Both machines are Mac OS X 10.6.

I also understand that cron runs things in a minimalist environment.

crontab:

10 2 * * * /Users/michael/.rvm/gems/ruby-1.9.3-p194/bin/my_gem

      

Error on the machine B

:

/Users/michael/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/dependency.rb:247:in `to_specs': Could not find my_gem-toolchain (>= 0) amongst [bigdecimal-1.1.0, io-console-0.3, json-1.5.4, minitest-2.5.1, rake-0.9.2.2, rdoc-3.9.4] (Gem::LoadError)
    from /Users/michael/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/dependency.rb:256:in `to_spec'
    from /Users/michael/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems.rb:1231:in `gem'
    from /Users/michael/.rvm/gems/ruby-1.9.3-p194/bin/my_gem:22:in `<main>'

      

Here is the content /Users/michael/.rvm/gems/ruby-1.9.3-p194/bin/my_gem

#!/Users/michael/.rvm/rubies/ruby-1.9.3-p194/bin/ruby
#
# This file was generated by RubyGems.
#
# The application 'my_gem-toolchain' is installed as part of a gem, and
# this file is here to facilitate running it.
#

require 'rubygems'

version = ">= 0"

if ARGV.first
  str = ARGV.first
  str = str.dup.force_encoding("BINARY") if str.respond_to? :force_encoding
  if str =~ /\A_(.*)_\z/
    version = $1
    ARGV.shift
  end
end

gem 'my_gem-toolchain', version
load Gem.bin_path('my_gem-toolchain', 'my_gem', version)

      

Line 22: gem 'my_gem-toolchain', version

Here is the output from running env

through cron on both machines:

SHELL=/bin/sh
USER=michael
PATH=/usr/bin:/bin
PWD=/Users/michael
SHLVL=1
HOME=/Users/michael
LOGNAME=michael
_=/usr/bin/env

      

I believe it has something to do with GEM_PATH

not being installed. However, the A

parameter is GEM_PATH

not set on the machine , but everything works.

I would like to better understand how the ruby ​​works. Apparently I am still missing something.

Why does this work on machine A

but not machine B

?

+3


source to share


1 answer


RVM is a great tool for developers to maintain their environment, but it's a production nightmare. Once you do things from cron, you are essentially "in production" to maintain the runtime, even if it happens on cron on your dev machine.

I recommend that you install gem system-wide and run it from a script that makes no assumptions about where it is installed. Make sure you follow all the gem guidelines so that your dependencies are installed correctly, binaries are handled properly, etc. Once you've built the gem, you must install it through the Ruby system so that it is accessible to all users in the default location for your Ruby Ruby interpreter and all of its dependencies are installed.

Check out Rubygems Create your own gemstone guide for best practices.

Then install the resulting gem file using system ruby ​​and not in the RVM directory:



rvm use system
gem install my-gem-1.0.0.gem

      

This should get all the settings for it to work happily from cron and for any user. You probably don't want to run cron jobs that rely on RVM in your home directory.

To specifically answer your question, there are a number of reasons why it might work on A but not B. If you installed the gem system-wide on one machine and via RVM on another, it will affect where the dependencies were installed (by the entire system in your home directory); if you don't even have RVM on another machine then this is definitely the case. Your Gem may not be properly declaring its dependencies, so when you installed it on machine B, the dependencies were not installed there, but you manually installed them on machine A during development. Perhaps you have the same gems installed on both and the home directory is shared and everything else is duplicated, but the RVM magic in your shell configuration fails on both. There may be different users, therefore in different environments.Whatever the root cause, just take RVM out of the equation and follow Rubygems' best practices and you'll be good to go.

+2


source







All Articles