0

我刚刚切换到在独角兽上部署我的 Rails 项目。在我的 capistrano 食谱deploy:restart中是

task :restart, :except => { :no_release => true } do
    run "cd #{shared_path}/pids && kill -s USR2 `cat unicorn.pid`"
end

但是我不时发现,有时部署过程会成功完成,但是尽管更新了 unicorn.pid,但独角兽进程仍然保持不变。

例如,我在 12 月 15 日部署,并监控unicorn.pid更新,但如果我运行ps -ef | grep unicorn,我可以看到 unicorn 进程在我上次部署时仍在启动,因此它引用了最后一个发布文件夹,导致麻烦。

这是为什么?

以下是我的unicorn.rb文件:

env = ENV["RAILS_ENV"]

case env
when 'pre', 'production'
  @app_path = '/home/deployer/deploy/myproject'
end

if env == 'pre' || env == 'production'
  user 'deployer', 'staff'
  shared_path = "#{@app_path}/shared"
  stderr_path "#{shared_path}/log/unicorn.stderr.log"
  stdout_path "#{shared_path}/log/unicorn.stdout.log"

  if env == 'production'
    worker_processes 6
  else
    worker_processes 2
  end

  working_directory "#{@app_path}/current" # available in 0.94.0+

  listen "/tmp/myproject.sock", :backlog => 64


  timeout 30

  pid "#{shared_path}/pids/unicorn.pid"


  preload_app true
  GC.respond_to?(:copy_on_write_friendly=) and
  GC.copy_on_write_friendly = true

  before_fork do |server, worker|

    defined?(ActiveRecord::Base) and
    ActiveRecord::Base.connection.disconnect!

    old_pid = "#{shared_path}/pids/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|
    defined?(ActiveRecord::Base) and
    ActiveRecord::Base.establish_connection
  end
end
4

1 回答 1

0

我不确定为什么会这样,但我个人在通过 Capistrano 部署时不会杀死 Unicorn PID。

相反,我通过 god gem 启动 unicorn,并配置 god 文件,以便在文件被触摸时重新启动 unicorn。

当我通过 Capistrano 部署时,我会在每次部署后触摸文件,强制重启独角兽进程

namespace :deploy do
  desc "Touch restart.txt, which will restart all God processes"
  task :restart do
    run "touch #{ current_path }/tmp/restart.txt"
end

您还需要将此模块添加到 .god 文件中:

module God
  module Conditions
    class RestartFileTouched < PollCondition
      attr_accessor :restart_file
      def initialize
        super
      end

      def process_start_time
        Time.parse(`ps -o lstart  -p #{self.watch.pid}`)
      end

      def restart_file_modification_time
        File.mtime(self.restart_file)
      end

      def valid?
        valid = true
        valid &= complain("Attribute 'restart_file' must be specified", self) if self.restart_file.nil?
        valid
      end

      def test
        process_start_time < restart_file_modification_time
      end
    end
  end
end
于 2013-01-16T02:02:33.773 回答