【问题标题】:unicorn working_directory with symlink带有符号链接的独角兽工作目录
【发布时间】:2013-02-21 13:09:01
【问题描述】:

我们在使用 unicorn 进行热部署时遇到了问题。我们几乎使用规范的unicorn.rb 配置,将working_directory 设置为指向符号链接的文件夹,但不知何故,它在第一次启动时似乎卡在实际文件夹中并且无法遵循符号链接。

# config/unicorn.rb
if ENV['RAILS_ENV'] == 'production'
  worker_processes 4
else
  worker_processes 2
end

working_directory "/var/local/project/symlinkfolder"

# Listen on unix socket
listen "/tmp/unicorn.sock", :backlog => 64

pid "/var/run/unicorn/unicorn.pid"

stderr_path "/var/log/unicorn/unicorn.log"
stdout_path "/var/log/unicorn/unicorn.log"

preload_app true

before_fork do |server, worker|
  # the following is highly recomended for Rails + "preload_app true"
  # as there's no need for the master process to hold a connection
  if defined?(ActiveRecord::Base)
    ActiveRecord::Base.connection.disconnect!
  end

  # Before forking, kill the master process that belongs to the .oldbin PID.
  # This enables 0 downtime deploys.
  old_pid = "/var/run/unicorn/unicorn.pid.oldbin"
  if File.exists?(old_pid) && server.pid != old_pid
    begin
      Process.kill("QUIT", File.read(old_pid).to_i)
    rescue Errno::ENOENT, Errno::ESRCH
      # someone else did our job for us
    end
  end
end

after_fork do |server, worker|
  # the following is *required* for Rails + "preload_app true",
  if defined?(ActiveRecord::Base)
    ActiveRecord::Base.establish_connection
  end

  # this makes sure the logging-rails framework works when preload_app = true
  Logging.reopen
  # if preload_app is true, then you may also want to check and
  # restart any other shared sockets/descriptors such as Memcached,
  # and Redis.  TokyoCabinet file handles are safe to reuse
  # between any number of forked children (assuming your kernel
  # correctly implements pread()/pwrite() system calls)
end  

当我们发出USR2 时,我们会在独角兽日志中看到:

executing ["/var/local/project/project.d/6/vendor/bundle/ruby/1.9.1/bin/unicorn_rails", "-E", "staging", "-D", "-c", "/var/local/project/symlinkfolder/config/unicorn.rb"│·
, {12=>#<Kgio::UNIXServer:fd 12>}] (in /var/local/project/project.d/8)

所以 unicorn 以某种方式“卡在”第 6 版上,而实际的符号链接文件夹在第 8 版上......一旦我们在几次部署后为第 6 版修剪文件夹,这就会成为一个问题......

  • working_directory 设置为符号链接文件夹
  • 符号链接正确指向/var/local/project/project.d/[id] 文件夹
  • 我们在发送USR2 信号之前更新符号链接

我们错过了什么??

【问题讨论】:

    标签: ruby-on-rails-3 deployment signals unicorn


    【解决方案1】:

    解决方案是显式设置 unicorn 二进制路径,正如 http://unicorn.bogomips.org/Sandbox.html 上解释的(以某种令人困惑的方式)

    app_root = "/var/local/project/symlinkfolder"
    working_directory app_root
    # see http://unicorn.bogomips.org/Sandbox.html
    Unicorn::HttpServer::START_CTX[0] = "#{app_root}/vendor/bundle/ruby/1.9.1/bin/unicorn_rails"
    

    然后我们需要发出unicorn reload (kill -HUP) 命令,所以 unicorn 会重新加载配置文件。从那时起,发出USR2 信号就可以正常工作了。

    【讨论】:

      猜你喜欢
      • 2014-08-19
      • 2014-05-22
      • 2015-12-01
      • 2014-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-29
      • 1970-01-01
      相关资源
      最近更新 更多