Capistrano-unicorn宝石获取错误的环境设置

时间:2013-09-19 18:10:03

标签: ruby-on-rails capistrano unicorn

我已经使用了这个gem一段时间了,只是尝试将实际的临时环境部署到我的登台服务器,我遇到了问题。 Unicorn以命令unicorn_rails-E production开头,尽管所有设置都是正确的。

我在deploy.rb中注意到我的unicorn_bin变量被设置为unicorn_rails。我在deploy.rb中取出了这个设置。但是unicorn:当默认值为unicorn_rails时,复制仍会执行unicorn命令。

我的vars都设置为deploy / staging.rb中的暂存,如多阶段设置wiki文档中所述,但我注意到-E仍然设置为生产。

相关信息:

这是部署后我的unicorn.log文件的输出:

executing ["/var/www/apps/myapp/shared/bundle/ruby/2.0.0/bin/unicorn_rails", "-c", "/var/www/apps/bundio/current/config/unicorn.rb", "-E", "production", "-D", {12=>#<Kgio::UNIXServer:/tmp/bundio.socket>, 13=>#<Kgio::TCPServer:fd 13>}] (in /var/www/apps/bundio/current)

这是cap -T的输出(默认为分段)

# Environments
rails_env          "staging"
unicorn_env        "staging"
unicorn_rack_env   "staging"

# Execution
unicorn_user       nil
unicorn_bundle     "/usr/local/rvm/gems/ruby-2.0.0-p247@global/bin/bundle"
unicorn_bin        "unicorn"
unicorn_options    ""
unicorn_restart_sleep_time  2

# Relative paths
app_subdir                         ""
unicorn_config_rel_path            "config"
unicorn_config_filename            "unicorn.rb"
unicorn_config_rel_file_path       "config/unicorn.rb"
unicorn_config_stage_rel_file_path "config/unicorn/staging.rb"

# Absolute paths
app_path                  "/var/www/apps/myapp/current"
unicorn_pid               "/var/www/apps/myapp/shared/pids/unicorn.myapp.pid"
bundle_gemfile            "/var/www/apps/myapp/current/Gemfile"
unicorn_config_path       "/var/www/apps/myapp/current/config"
unicorn_config_file_path  "/var/www/apps/myapp/current/config/unicorn.rb"
unicorn_config_stage_file_path
->                        "/var/www/apps/myapp/current/config/unicorn/staging.rb"

另一个奇怪的是,unicorn_rails -E标志应该引用rails环境,而unicorn -E应该引用机架环境 - 机架env应该只获得值开发和部署,但它被设置为生产,有点奇怪(见unicorn docs for settings of the RACK_ENV variable

对此的任何见解都将非常感激。在我的登台服务器上,我还将RAILS_ENV设置为暂存。我为其他环境设置了rails的东西,比如在我的环境文件夹中添加staging.rb,向database.yml添加一个临时部分等。

lib/capistrano-unicorn/config.rb中关于unicorn_rack_env的重要内容:

_cset(:unicorn_env)                { fetch(:rails_env, 'production' ) }
_cset(:unicorn_rack_env) do
# Following recommendations from http://unicorn.bogomips.org/unicorn_1.html
fetch(:rails_env) == 'development' ? 'development' : 'deployment'
end

提前致谢。

1 个答案:

答案 0 :(得分:0)

好的,经过很长一段时间没有正确的环境,我发现了这个问题!

基本上,我的init脚本在我的capistrano-unicorn bin正在做它之前就已经运行了。

因此,当capistrano-unicorn正在执行独角兽重启/重新加载/复制任务时,请确保考虑管理Unicorn及其工作人员的init.d或upstart脚本。

当我不得不调试过时的pid文件/已经运行/无法侦听套接字错误时,我没想过要查看这些脚本。但这是有道理的,因为新手在未运行时启动Unicorn,然后capistrano-unicorn也试图启动Unicorn。

我现在已将这些capistrano任务和钩子与Monit和Unicorn init脚本结合起来。

Capistrano任务:

namespace :monit do
  desc ' wait 20 seconds '
  task :wait_20_seconds do
    sleep 20
  end
  task :monitor_all, :roles => :app do
    sudo "monit monitor all"
  end

  task :unmonitor_all, :roles => :app do
    sudo "monit unmonitor all"
  end

  desc 'monitor unicorn in the monit rc file'
  task :monitor_unicorn, :roles => :app do
    sudo "monit monitor unicorn"
  end

  desc 'unmonitor unicorn in the monit rc file'
  task :unmonitor_unicorn, :roles => :app do
    sudo "monit unmonitor unicorn"
  end
end

Capistrano挂钩:

after 'deploy:restart', 'unicorn:duplicate'  # app preloaded. check https://github.com/sosedoff/capistrano-unicorn section for zero downtime

before 'deploy', "monit:unmonitor_unicorn"
before 'deploy:migrations', "monit:unmonitor_unicorn"

after 'deploy', 'monit:wait_20_seconds'
after "deploy:migrations", "monit:wait_20_seconds"

after 'monit:wait_20_seconds', 'monit:monitor_unicorn'

我使用Monit来监控我的独角兽过程:

在/ etc / monit / monitrc中:

check process unicorn
  with pidfile /var/www/apps/my_app/shared/pids/mypid.pid
  start program = "/usr/bin/sudo service unicorn start"
  stop program = "/usr/bin/sudo service unicorn stop"

在init脚本中,您将使用以下内容启动unicorn进程: unicorn_rails -c /var/www/apps/my_app/current/config/unicorn.rb -E staging -D 确保将-E标志设置为正确的环境。 capistrano-unicorn gem在deploy.rb中使用:set指令,允许您指定该独角兽进程的环境。