Capistrano 2 Delayed_Job任务无法使用RBEnv配置的Ruby

时间:2014-04-19 03:37:14

标签: ruby capistrano rbenv

Capistrano 2.15rbenv一起使用, config / deploy / production.rb 具有:

set :bundle_flags, "--deployment --quiet --binstubs"
set :default_environment, {
   'RAILS_ENV' => 'production',
   'RBENV_ROOT' => "$HOME/.rbenv",
   'PATH' => "$RBENV_ROOT/shims:$RBENV_ROOT/bin:$PATH"
}

config / deploy.rb 中测试ruby环境和版本的任务:

# Test Ruby Environment
desc "Test Ruby Environment"
task :test_ruby_environment, :roles => :app do
  run "env; ruby -v"
end

cap test_ruby_environment的输出省略了PATH中的$ RBENV_ROOT目录:

** [out :: ex.org] RBENV_ROOT=/home/app/.rbenv
** [out :: ex.org] PATH=/shims:/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

当我需要红宝石时,delayed_job(3.0.5)守护程序正在运行系统ruby 由rbenv配置。这方面的证据是来自delayed_job的错误描述:

/home/app/shared/bundle/ruby/1.8/gems/delayed_job-3.0.5/lib/delayed/backend/base.rb:95:in `invoke_job'

显示系统安装了1.8个宝石源。

为什么这不能获取rbenv配置的ruby?

1 个答案:

答案 0 :(得分:0)

好吧,事实证明,因为:default_environment 是一个哈希值,Capistrano不一定按顺序生成环境设置。

在RBENV_ROOT之前生成PATH设置时,PATH中使用的环境变量为空。

解决方案不是依赖于PATH中的RBENV_ROOT:

set :bundle_flags, "--deployment --quiet --binstubs"
set :rbenv_root, '$HOME/.rbenv'
# be careful, these might not play back in order written
set :default_environment, {
    'RAILS_ENV' => 'production',
    'RBENV_ROOT' => fetch(:rbenv_root),
    'PATH' => "#{fetch(:rbenv_root)}/shims:#{fetch(:rbenv_root)}/bin:$PATH"
}

现在输出有:

 ** [out :: ex.org] RBENV_ROOT=/home/app/.rbenv
 ** [out :: ex.org] PATH=/home/app/.rbenv/shims:/home/app/.rbenv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

delayed_job任务使用rbenv配置的ruby运行。