From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS14383 205.234.109.0/24 X-Spam-Status: No, score=-0.6 required=5.0 tests=FREEMAIL_FROM, MSGID_FROM_MTA_HEADER,RP_MATCHES_RCVD,T_DKIM_INVALID shortcircuit=no autolearn=unavailable version=3.3.2 Path: news.gmane.org!not-for-mail From: Suraj Kurapati Newsgroups: gmane.comp.lang.ruby.unicorn.general Subject: Re: feature request - when_ready() hook Date: Mon, 8 Mar 2010 14:58:10 -0800 Message-ID: References: <20091126060519.GC22762@dcvr.yhbt.net> <20091126195348.GB26561@dcvr.yhbt.net> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Trace: dough.gmane.org 1268138064 17863 80.91.229.12 (9 Mar 2010 12:34:24 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Tue, 9 Mar 2010 12:34:24 +0000 (UTC) To: unicorn list Original-X-From: mongrel-unicorn-bounces@rubyforge.org Tue Mar 09 13:34:19 2010 Return-path: Envelope-to: gclrug-mongrel-unicorn@m.gmane.org X-Original-To: mongrel-unicorn@rubyforge.org Delivered-To: mongrel-unicorn@rubyforge.org X-Greylist: delayed 926 seconds by postgrey-1.31 at rubyforge.org; Mon, 08 Mar 2010 18:21:01 EST DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:content-type; bh=PtawMPIkZcGuD1oyogWMrd8xu3TSOnMH6UkN7u9kJrw=; b=hpbyiTxF37bRCH74fRhhi5FXztKDesqmF66F3FlHcgCaoVJ3VGhD9zZuytEK1u9pTQ aaDpZL9dFN7ncea8u21RBK/aSzbfCNZQDVtotOVMNYIPcj5FcDceBQL1OW8/D6OuT6Ld f+80Ys80HKQJEVnauEvgg4JRwi9eg3aEpvC1E= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; b=w0eWSBCkITVVh5D88sFjcO40uaCi7Gl7vbKbIk3UFuHR1dUeBrlqBA5HFPbwXy/At4 aCiKvHuQRFJxlUhZ1d6UqE4FKw7tYn+oMDg7sMIffyNTaqrILtA5l/yaFbYC/ia9JPFO ++JgmBRO8wxwKCZocT9B4UWyf/srcX7GJrKxY= In-Reply-To: X-BeenThere: mongrel-unicorn@rubyforge.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: mongrel-unicorn-bounces@rubyforge.org Errors-To: mongrel-unicorn-bounces@rubyforge.org Xref: news.gmane.org gmane.comp.lang.ruby.unicorn.general:436 Archived-At: Received: from rubyforge.org ([205.234.109.19]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1NoyUM-00029K-46 for gclrug-mongrel-unicorn@m.gmane.org; Tue, 09 Mar 2010 13:24:42 +0100 Received: from rubyforge.org (rubyforge.org [127.0.0.1]) by rubyforge.org (Postfix) with ESMTP id CCC3C1588074; Mon, 8 Mar 2010 18:29:14 -0500 (EST) Received: from mail-pz0-f200.google.com (mail-pz0-f200.google.com [209.85.222.200]) by rubyforge.org (Postfix) with ESMTP id D2EB2185828D for ; Mon, 8 Mar 2010 18:21:01 -0500 (EST) Received: by pzk38 with SMTP id 38so1355447pzk.33 for ; Mon, 08 Mar 2010 15:21:01 -0800 (PST) Received: by 10.142.209.6 with SMTP id h6mr3711991wfg.295.1268089090072; Mon, 08 Mar 2010 14:58:10 -0800 (PST) Hi Eric, On Mon, Nov 30, 2009 at 3:47 PM, Suraj Kurapati wrote: > from Capistrano, I send SIGUSR2 to the existing Unicorn master (which > will become the old Unicorn master), wait 90 seconds, and then send > SIGQUIT to the old Unicorn master. [...] The only thing I'm worried > about is that I'll have to keep adjusting this timeout as the > infrastructure my app depends upon becomes slower/faster. A > when_ready() hook would really do wonders for me Inspired by your solution[1] to a recent question about forking, I solved this problem by temporarily sneaking into the build_app! method and killing the old Unicorn master after the Rails app has been built: before_fork do |server, worker| # # 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_file = server.config[:pid].to_s + '.oldbin' if File.exist? old_pid_file and server.pid != old_pid_file and worker.nr == server.worker_processes-1 then # # wait until Rails app is built and ready to serve (we do this by # sneaking into Unicorn's build_app! method) by the last worker # process inside the new Unicorn before stopping old Unicorn # orig_meth_name = :build_app! orig_meth_impl = server.method(orig_meth_name) server_metaclass = class << server; self; end server_metaclass.class_eval do # replace Unicorn's method with our own sneaky version define_method orig_meth_name do # behave like Unicorn's original method orig_meth_impl.call # do our sneaky business! (kill the old Unicorn) begin Process.kill :QUIT, File.read(old_pid_file).to_i rescue Errno::ENOENT, Errno::ESRCH # ignore end # restore Unicorn's original method server_metaclass.class_eval do define_method orig_meth_name, orig_meth_impl end end end end end Thanks for your help! :) [1]: http://article.gmane.org/gmane.comp.lang.ruby.unicorn.general/425 _______________________________________________ Unicorn mailing list - mongrel-unicorn@rubyforge.org http://rubyforge.org/mailman/listinfo/mongrel-unicorn Do not quote signatures (like this one) or top post when replying