unicorn Ruby/Rack server user+dev discussion/patches/pulls/bugs/help
 help / color / mirror / code / Atom feed
From: Eric Wong <normalperson@yhbt.net>
To: mongrel-unicorn@rubyforge.org
Subject: [PATCH] configurator: update some migration examples
Date: Fri, 2 Oct 2009 01:11:02 -0700	[thread overview]
Message-ID: <20091002081102.GA11179@dcvr.yhbt.net> (raw)

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

                 reply	other threads:[~2009-10-02  8:13 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://yhbt.net/unicorn/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20091002081102.GA11179@dcvr.yhbt.net \
    --to=normalperson@yhbt.net \
    --cc=mongrel-unicorn@rubyforge.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://yhbt.net/unicorn.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).