about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2009-03-04 11:27:50 -0800
committerEric Wong <normalperson@yhbt.net>2009-03-04 11:27:50 -0800
commit279e1b0d4bf27e759d46460035dd6dacccef35f7 (patch)
tree820c9b19102dcc25f66057fc4169825226f30d1e
parent551789f8619b567333bbf41020323f4e1624841c (diff)
downloadunicorn-279e1b0d4bf27e759d46460035dd6dacccef35f7.tar.gz
This is useful for freeing certain resources you
do NOT want passed to child processes.
-rw-r--r--lib/unicorn.rb1
-rw-r--r--lib/unicorn/configurator.rb21
2 files changed, 19 insertions, 3 deletions
diff --git a/lib/unicorn.rb b/lib/unicorn.rb
index 0727456..d442f63 100644
--- a/lib/unicorn.rb
+++ b/lib/unicorn.rb
@@ -327,6 +327,7 @@ module Unicorn
         Dir.chdir(@start_ctx[:cwd])
         cmd = [ @start_ctx[:zero] ] + @start_ctx[:argv]
         logger.info "executing #{cmd.inspect} (in #{Dir.pwd})"
+        @before_exec.call(self) if @before_exec
         exec(*cmd)
       end
     end
diff --git a/lib/unicorn/configurator.rb b/lib/unicorn/configurator.rb
index e01dbe1..d080a37 100644
--- a/lib/unicorn/configurator.rb
+++ b/lib/unicorn/configurator.rb
@@ -28,6 +28,9 @@ module Unicorn
       :before_fork => lambda { |server, worker_nr|
           server.logger.info("worker=#{worker_nr} spawning...")
         },
+      :before_exec => lambda { |server|
+          server.logger.info("forked child re-executing...")
+        },
       :pid => nil,
       :backlog => 1024,
       :preload_app => false,
@@ -106,6 +109,16 @@ module Unicorn
       set_hook(:before_fork, block)
     end
 
+    # sets the before_exec hook to a given Proc object.  This
+    # Proc object will be called by the master process right
+    # before exec()-ing the new unicorn binary.  This is useful
+    # for freeing certain OS resources that you do NOT wish to
+    # share with the reexeced child process.
+    # There is no corresponding after_exec hook (for obvious reasons).
+    def before_exec(&block)
+      set_hook(:before_exec, block, 1)
+    end
+
     # sets the timeout of worker processes to +seconds+
     # This will gracefully restart all workers if the value is lowered
     # to prevent them from being timed out according to new timeout rules
@@ -173,12 +186,14 @@ module Unicorn
       @set[var] = path
     end
 
-    def set_hook(var, my_proc) #:nodoc:
+    def set_hook(var, my_proc, req_arity = 2) #:nodoc:
       case my_proc
       when Proc
         arity = my_proc.arity
-        (arity == 2 || arity < 0) or raise ArgumentError,
-                        "#{var}=#{my_proc.inspect} has invalid arity: #{arity}"
+        (arity == req_arity) or \
+          raise ArgumentError,
+                "#{var}=#{my_proc.inspect} has invalid arity: " \
+                "#{arity} (need #{req_arity})"
       when NilClass
         my_proc = DEFAULTS[var]
       else