【问题标题】:Unicorn restart issue with capistranocapistrano 的独角兽重启问题
【发布时间】:2025-12-15 07:35:02
【问题描述】:

我们正在使用 cap 进行部署,并使用将 USR2 发送到 unicorn 进程以重新加载的脚本,它通常可以工作,但偶尔会失败。当这种情况发生时,查看 unicorn 日志会发现它正在一个不再存在的旧版本目录中寻找 Gemfile。

例外:

/usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.21/lib/bundler/definition.rb:14:in `build': /var/www/railsapps/inventory/releases/20111128233407/Gemfile not found (Bundler::GemfileNotFound)

澄清这不是当前版本,而是已被删除的旧版本。

当它工作时,它似乎确实工作正常 - 即它确实获取了新代码 - 所以我认为它不会因为引用旧版本而被卡住。

有什么想法吗?

【问题讨论】:

    标签: capistrano bundler unicorn


    【解决方案1】:

    在你的 unicorn.rb 中添加 before_exec 块

    current_path = "/var/www/html/my project/current"
    
    before_exec do |server|
      ENV['BUNDLE_GEMFILE'] = "#{current_path}/Gemfile"
    end
    

    在此处了解更多信息http://blog.willj.net/2011/08/02/fixing-the-gemfile-not-found-bundlergemfilenotfound-error/

    【讨论】:

    • 另外,不要忘记,如果 unicorn 配置文件被更改,您必须手动关闭并重新启动 unicorn 才能加载新设置。
    【解决方案2】:

    您应该在启动服务器之前设置BUNDLE_GEMFILE 环境变量,将其指向current/Gemfile

    【讨论】: