From 262440004b82dbf97b386374ae0897c4eec33b74 Mon Sep 17 00:00:00 2001 From: Daniel Beardsley Date: Fri, 8 Mar 2013 03:39:45 -0700 Subject: [PATCH] Unicorn: shut down old master upon first fork --- config/unicorn.rb | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+), 0 deletions(-) diff --git a/config/unicorn.rb b/config/unicorn.rb index 31f3f45..58b2d13 100644 --- a/config/unicorn.rb +++ b/config/unicorn.rb @@ -3,3 +3,27 @@ worker_processes 3 # amount of unicorn workers to spin up timeout 30 # restarts workers that hang for 30 seconds preload_app true + +# Taken from github: https://github.com/blog/517-unicorn +# Though everyone uses pretty miuch the same code +before_fork do |server, worker| + ## + # When sent a USR2, Unicorn will suffix its pidfile with .oldbin and + # immediately start loading up a new version of itself (loaded with a new + # version of our app). When this new Unicorn is completely loaded + # it will begin spawning workers. The first worker spawned will check to + # see if an .oldbin pidfile exists. If so, this means we've just booted up + # a new Unicorn and need to tell the old one that it can now die. To do so + # we send it a QUIT. + # + # Using this method we get 0 downtime deploys. + + old_pid = "#{server.config[: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 -- libgit2 0.21.2