summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2014-10-04 02:14:21 +0000
committerEric Wong <e@80x24.org>2014-10-04 02:14:21 +0000
commit3318b070c6513a3baa7ce7ac26f4835f46ccff1f (patch)
treedf4f4576b41bcfe557919af9d9c055b53ac3c5e1
parent4b2782a926d8f131b1e7382be35e3abb77bf4be5 (diff)
There may be code in a before_fork hook which should run only once,
document an example using a guard variable since it may not be
immediately obvious to all users.

Inspired-by: BrĂ¡ulio Bhavamitra <braulio@eita.org.br>

http://bogomips.org/unicorn-public/m/20141004015707.GA1951@dcvr.yhbt.net.html
-rw-r--r--examples/unicorn.conf.rb11
1 files changed, 11 insertions, 0 deletions
diff --git a/examples/unicorn.conf.rb b/examples/unicorn.conf.rb
index 9dce58a..4b28a5a 100644
--- 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.