Commit 262440004b82dbf97b386374ae0897c4eec33b74
1 parent
34a4b8a1
Exists in
master
and in
1 other branch
Unicorn: shut down old master upon first fork
After the first child forks, shut down the old master. Previously, the old master and it's workers would jut sit there forever. See: https://github.com/blog/517-unicorn https://github.com/sosedoff/capistrano-unicorn/blob/master/examples/rails3.rb#L30 Tested in production.
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 | ... | ... |