Commit b508e914b21a5ecbc375bbb77dfba5653e37e2c1

Authored by Shuky Dvir
2 parents 34a4b8a1 26244000
Exists in master and in 1 other branch production

Merge pull request #431 from iFixit/unicorn-kill-old-master

Unicorn: shut down old master upon first fork
Showing 1 changed file with 24 additions and 0 deletions   Show diff stats
config/unicorn.rb
... ... @@ -3,3 +3,27 @@
3 3 worker_processes 3 # amount of unicorn workers to spin up
4 4 timeout 30 # restarts workers that hang for 30 seconds
5 5 preload_app true
  6 +
  7 +# Taken from github: https://github.com/blog/517-unicorn
  8 +# Though everyone uses pretty miuch the same code
  9 +before_fork do |server, worker|
  10 + ##
  11 + # When sent a USR2, Unicorn will suffix its pidfile with .oldbin and
  12 + # immediately start loading up a new version of itself (loaded with a new
  13 + # version of our app). When this new Unicorn is completely loaded
  14 + # it will begin spawning workers. The first worker spawned will check to
  15 + # see if an .oldbin pidfile exists. If so, this means we've just booted up
  16 + # a new Unicorn and need to tell the old one that it can now die. To do so
  17 + # we send it a QUIT.
  18 + #
  19 + # Using this method we get 0 downtime deploys.
  20 +
  21 + old_pid = "#{server.config[:pid]}.oldbin"
  22 + if File.exists?(old_pid) && server.pid != old_pid
  23 + begin
  24 + Process.kill("QUIT", File.read(old_pid).to_i)
  25 + rescue Errno::ENOENT, Errno::ESRCH
  26 + # someone else did our job for us
  27 + end
  28 + end
  29 +end
... ...