We now give an example of how a before_fork hook can be used
to incrementally migrate off the old code base without hitting
a thundering herd (especially in the "preload_app false") case.
Also comment on the per-worker listen usage in the RDoc, not
just a hidden comment.
---
I just pushed this out earlier, this example was inspired by Chris
Wanstrath''s before_fork hook but modified to use SIGTTOU instead of
SIGQUIT for all but the last worker to minimize memory explosions
during upgrades.
The :tries and :delay options were also inspired by Chris and will be
in v0.93.0 which will probably be released.
lib/unicorn/configurator.rb | 21 +++++++++++++++++----
1 files changed, 17 insertions(+), 4 deletions(-)
diff --git a/lib/unicorn/configurator.rb b/lib/unicorn/configurator.rb
index 7e66f60..bff8f7e 100644
--- a/lib/unicorn/configurator.rb
+++ b/lib/unicorn/configurator.rb
@@ -25,9 +25,26 @@ module Unicorn
# # as there''s no need for the master process to hold a
connection
# defined?(ActiveRecord::Base) and
# ActiveRecord::Base.connection.disconnect!
+ #
+ # # the following allows a new master process to incrementally
+ # # phase out the old master process with SIGTTOU to avoid a
+ # # thundering herd (especially in the "preload_app false"
case)
+ # # when doing a transparent upgrade. The last worker spawned
+ # # will then kill off the old master process with a SIGQUIT.
+ # old_pid = "#{server.config[:pid]}.oldbin"
+ # if old_pid != server.pid
+ # begin
+ # sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU
+ # Process.kill(sig, File.read(old_pid).to_i)
+ # rescue Errno::ENOENT, Errno::ESRCH
+ # end
+ #
+ # # optionally throttle the master from forking too quickly by sleeping
+ # sleep 1
# end
#
# after_fork do |server, worker|
+ # # per-process listener ports for debugging/admin/migrations
# addr = "127.0.0.1:#{9293 + worker.nr}"
# server.listen(addr, :tries => -1, :delay => 5, :tcp_nopush =>
true)
#
@@ -50,10 +67,6 @@ module Unicorn
:worker_processes => 1,
:after_fork => lambda { |server, worker|
server.logger.info("worker=#{worker.nr} spawned pid=#{$$}")
-
- # per-process listener ports for debugging/admin:
- # addr = "127.0.0.1:#{8081 + worker.nr}"
- # server.listen(addr, :tries => -1, :delay => 5)
},
:before_fork => lambda { |server, worker|
server.logger.info("worker=#{worker.nr} spawning...")
--
Eric Wong