带符号链接的unicorn working_directory

gin*_*ime 6 deployment signals unicorn ruby-on-rails-3

我们在使用独角兽进行热部署时遇到了麻烦.我们几乎使用规范unicorn.rb配置,设置working_directory指向symlink'd文件夹,但不知何故,当它第一次启动并且无法遵循符号链接时,它似乎停留在实际文件夹中.

# 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  
Run Code Online (Sandbox Code Playgroud)

当我们发出一个USR2,我们在unicorn日志中看到这个:

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)
Run Code Online (Sandbox Code Playgroud)

所以独角兽在某种程度上"卡在"版本6上,而实际的符号链接文件夹是在版本8上...一旦我们在几次部署之后修剪版本6的文件夹,这就成了问题...

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

我们错过了什么?

gin*_*ime 7

解决方案是明确设置独角兽二进制路径,如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"
Run Code Online (Sandbox Code Playgroud)

然后我们需要发出一个unicorn reload(kill -HUP)命令,因此unicorn重新加载配置文件.从那时起,发出USR2信号就能正常工作.