Question

* * * * * /bin/bash -l -c 'cd /Users/boris/projects/MyApp/ && rails runner "Resque.enqueue(Place)"'

Basically I need to do the following:

  1. Load Ruby with RVM
  2. Navigate to MyApp Dir
  3. Run the following line: rails runner "Resque.enqueue(Place)

The above cron seems to be running, but its producing the following errors with rails runner whats going on?

/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/dependency.rb:52:in `initialize': Valid types are [:development, :runtime], not nil (ArgumentError)
    from /Users/boris/.rvm/gems/ruby-1.9.2-p180/gems/bundler-1.0.12/lib/bundler/resolver.rb:359:in `new'
    from /Users/boris/.rvm/gems/ruby-1.9.2-p180/gems/bundler-1.0.12/lib/bundler/resolver.rb:359:in `search'
    from /Users/boris/.rvm/gems/ruby-1.9.2-p180/gems/bundler-1.0.12/lib/bundler/resolver.rb:354:in `gems_size'
    from /Users/boris/.rvm/gems/ruby-1.9.2-p180/gems/bundler-1.0.12/lib/bundler/resolver.rb:179:in `resolve'
    from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/source_index.rb:95:in `sort_by'
    from /Users/boris/.rvm/gems/ruby-1.9.2-p180/gems/bundler-1.0.12/lib/bundler/resolver.rb:175:in `each'
    from /Users/boris/.rvm/gems/ruby-1.9.2-p180/gems/bundler-1.0.12/lib/bundler/resolver.rb:175:in `sort_by'
    from /Users/boris/.rvm/gems/ruby-1.9.2-p180/gems/bundler-1.0.12/lib/bundler/resolver.rb:175:in `resolve'
    from /Users/boris/.rvm/gems/ruby-1.9.2-p180/gems/bundler-1.0.12/lib/bundler/resolver.rb:160:in `start'
    from /Users/boris/.rvm/gems/ruby-1.9.2-p180/gems/bundler-1.0.12/lib/bundler/resolver.rb:128:in `resolve'
    from /Users/boris/.rvm/gems/ruby-1.9.2-p180/gems/bundler-1.0.12/lib/bundler/resolver.rb:127:in `catch'
    from /Users/boris/.rvm/gems/ruby-1.9.2-p180/gems/bundler-1.0.12/lib/bundler/resolver.rb:127:in `resolve'
    from /Users/boris/.rvm/gems/ruby-1.9.2-p180/gems/bundler-1.0.12/lib/bundler/definition.rb:151:in `resolve'
    from /Users/boris/.rvm/gems/ruby-1.9.2-p180/gems/bundler-1.0.12/lib/bundler/definition.rb:90:in `specs'
    from /Users/boris/.rvm/gems/ruby-1.9.2-p180/gems/bundler-1.0.12/lib/bundler/definition.rb:135:in `specs_for'
    from /Users/boris/.rvm/gems/ruby-1.9.2-p180/gems/bundler-1.0.12/lib/bundler/definition.rb:124:in `requested_specs'
    from /Users/boris/.rvm/gems/ruby-1.9.2-p180/gems/bundler-1.0.12/lib/bundler/environment.rb:23:in `requested_specs'
    from /Users/boris/.rvm/gems/ruby-1.9.2-p180/gems/bundler-1.0.12/lib/bundler/runtime.rb:11:in `setup'
    from /Users/boris/.rvm/gems/ruby-1.9.2-p180/gems/bundler-1.0.12/lib/bundler.rb:107:in `setup'
    from /Users/boris/.rvm/gems/ruby-1.9.2-p180/gems/bundler-1.0.12/lib/bundler/setup.rb:6
    from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/custom_require.rb:36:in `gem_original_require'
    from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/custom_require.rb:36:in `require'
    from /Users/boris/projects/chaggregator/config/boot.rb:6
    from script/rails:5:in `require'
    from script/rails:5
Was it helpful?

Solution

Actually the best way to do this is to use rvm wrapper. You can create a wrapper like this:

rvm wrapper ruby-1.9.3-p0@somegemset appname rails 

binary can be rails, rake, gem or whatever other ruby binary you have installed. What happens is that rvm creates a wrapper that will source the correct rvm environment before executing the rails command. On system wide rvm, the wrapper will normally be placed in /usr/local/rvm/bin/

Now from cron you can just do:

*/3 * * * * cd /path/to/your/app && appname_rails runner "Resque.enqueue(Place)" -e production

This will cd into your app directory and execute the rvm wrapper you just created every 3 minutes. This example is based on rails 3 and production environment.

OTHER TIPS

You need to source the right environment via RVM before any Ruby code runs. So include something like this in the command:

source /usr/local/rvm/environments/ruby-1.9.2-p180@my-gemset

So a potential solution would be:

SHELL=/bin/bash
* * * * * source /{path_to_rvm_environment_for_ruby}@{gemset} && cd /Users/boris/projects/MyApp/ && rails runner "Resque.enqueue(Place)"'

Lots of moving pieces here, here are a couple of pointers on things to check in your environment (ruby, rvm, bundle) - post your findings and will go from there.

Path's issue with bundler, to change or not to change?

Bundle bug

I guess the commands runs fine outside of cron? if you're running your cron job as a different user, check the environment of that user.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top