From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-2.1 required=3.0 tests=ALL_TRUSTED,AWL,BAYES_00, URIBL_BLOCKED shortcircuit=no autolearn=unavailable version=3.3.2 X-Original-To: unicorn-public@bogomips.org Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id BFE3D1F7D5; Sat, 4 Oct 2014 01:57:07 +0000 (UTC) Date: Sat, 4 Oct 2014 01:57:07 +0000 From: Eric Wong To: =?utf-8?Q?Br=C3=A1ulio?= Bhavamitra Cc: unicorn-public Subject: Re: Master hooks needed Message-ID: <20141004015707.GA1951@dcvr.yhbt.net> References: <20141003122222.GA11445@dcvr.yhbt.net> <20141004012210.GA8134@dcvr.yhbt.net> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: List-Id: BrĂ¡ulio Bhavamitra wrote: > I think the hook is needed because I took too much time to figure out > the problem and much more time to figure out the solution (this > master_run variable). Also, I don't think this master_run solution is > elegant. A guard variable is fairly common practice for initialization. It's not always nice, but I do not consider the existing hooks to be elegant, either; they're only unfortunately necessary. I consider having redundant features to be even worse. How about the following documentation change instead? --- a/examples/unicorn.conf.rb +++ b/examples/unicorn.conf.rb @@ -54,12 +54,23 @@ GC.respond_to?(:copy_on_write_friendly=) and # fast LAN. check_client_connection false +# local variable to guard against running a hook multiple times +run_once = true + before_fork do |server, worker| # the following is highly recomended for Rails + "preload_app true" # as there's no need for the master process to hold a connection defined?(ActiveRecord::Base) and ActiveRecord::Base.connection.disconnect! + # Occasionally, it may be necessary to run non-idempotent code in the + # master before forking. Keep in mind the above disconnect! example + # is idempotent and does not need a guard. + if run_once + # do_something_once_here ... + run_once = false # prevent from firing again + end + # The following is only recommended for memory/DB-constrained # installations. It is not needed if your system can house # twice as many worker_processes as you have configured.