我刚刚切换到在独角兽上部署我的 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