【问题标题】:Capistrano-unicorn gem getting wrong environment setCapistrano-unicorn gem 设置错误的环境
【发布时间】:2013-09-19 18:10:02
【问题描述】:

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

我注意到在 deploy.rb 中我的 unicorn_bin 变量设置为 unicorn_rails。我在 deploy.rb 中去掉了这个设置。然而 unicorn:duplicate 仍然执行unicorn_rails 命令,此时默认应该是unicorn

正如多阶段设置 wiki 文档中所述,我的 var 都设置为在 deploy/staging.rb 中暂存,但我注意到 -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 应该引用 rack env——rack env 应该只获取值 developmentement 和 deployment,但它被设置为生产环境,这有点奇怪(见unicorn docs for settings of the RACK_ENV variable

对此的任何见解将不胜感激。在我的登台服务器上,我还将 RAILS_ENV 设置为登台。我已经为另一个环境设置了 Rails 的东西,比如在我的环境文件夹中添加 staging.rb,在 database.yml 中添加一个 staging 部分等。

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

提前致谢。

【问题讨论】:

    标签: ruby-on-rails capistrano unicorn


    【解决方案1】:

    好的,好久没有正确的环境,我发现了问题!

    基本上,我的 init 脚本在我的 capistrano-unicorn bin 执行操作之前运行。

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

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

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

    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"
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-12
      • 2018-11-03
      • 1970-01-01
      • 1970-01-01
      • 2011-12-12
      相关资源
      最近更新 更多