Cron作业错误帮助 - Rails Runner错误

时间:2011-05-23 16:50:28

标签: ruby-on-rails unix cron runner

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

基本上我需要做以下事情:

  1. Load Ruby with RVM
  2. 导航至MyApp Dir
  3. 运行以下行:rails runner "Resque.enqueue(Place)
  4. 上面的cron似乎正在运行,但是rails runner产生了以下错误 怎么了?

    /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
    

3 个答案:

答案 0 :(得分:4)

实际上,最好的方法是使用rvm包装器。您可以创建一个这样的包装器:

rvm wrapper ruby-1.9.3-p0@somegemset appname rails 

binary可以是rails,rake,gem或者你安装的任何其他ruby二进制文件。会发生什么是rvm创建一个包装器,它将在执行rails命令之前获取正确的rvm环境。在系统范围的rvm上,包装器通常会放在/ usr / local / rvm / bin /

现在从cron你可以做到:

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

这将进入你的app目录并执行你每3分钟创建一次的rvm包装器。此示例基于rails 3和生产环境。

答案 1 :(得分:1)

在运行任何Ruby代码之前,您需要通过RVM获取正确的环境。所以在命令中包含这样的内容:

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

所以潜在的解决方案是:

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

答案 2 :(得分:0)

这里有很多动人的作品,这里有几个关于你的环境要检查的东西的指针(ruby,rvm,bundle) - 发布你的发现并将从那里开始。

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

Bundle bug

我猜这些命令在cron之外运行良好?如果您以不同的用户身份运行您的cron作业,请检查该用户的环境。