ruby-on-rails-3 - 带有符号链接(symbolic link)的 unicorn 工作目录

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

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

# 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 ,我们在 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)

所以 unicorn 在某种程度上“卡在”版本 6 上,而实际的符号链接(symbolic link)文件夹在版本 8 上……一旦我们在几次部署后为版本 6 修剪文件夹,这就会成为一个问题……
  • working_directory设置为符号链接(symbolic link)文件夹
  • 符号链接(symbolic link)指向 /var/local/project/project.d/[id]文件夹正确
  • 我们在发送 USR2 之前更新符号链接(symbolic link)信号

  • 我们错过了什么??

    最佳答案

    解决方案是显式设置 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信号正常工作。

    关于ruby-on-rails-3 - 带有符号链接(symbolic link)的 unicorn 工作目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15003063/

    相关文章:

    ruby-on-rails - Rails路由到root

    ruby-on-rails - CentOS + Rails + nginx + Unicorn + MySQL + RVM(?) 方法?

    mysql - mysql 和 postgres 之间的主要区别是什么?

    signals - "sc_signal<T> cannot have more than one driver"错误,SystemC

    ruby-on-rails-3 - Rails 在迁移中预填充表

    json - 推送到 openShift git 时找不到 metadata.json

    python - 基于jupyter笔记本创建项目

    vba - 部署 VBA 宏

    signals - PyQt5 在类之间发送信号?

    c - 如何在我自己的 C shell 中正确等待前台/后台进程?