about summary refs log tree commit homepage
path: root/lib/yahns/config.rb
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2013-10-22 22:46:25 +0000
committerEric Wong <e@80x24.org>2013-10-22 23:08:46 +0000
commit28163a34ceff8334b2af4c5c30ec1e642efb9be1 (patch)
tree6219d20af729502cd20b942fb8700bffea741f16 /lib/yahns/config.rb
parentb79ba40ff559f9676c7bad530eeab6b7eb6a91f1 (diff)
downloadyahns-28163a34ceff8334b2af4c5c30ec1e642efb9be1.tar.gz
These can be used to disconnect/reconnect to databases
and other external connections.  These are named to match
the documentation of pthread_atfork(3)
Diffstat (limited to 'lib/yahns/config.rb')
-rw-r--r--lib/yahns/config.rb40
1 files changed, 36 insertions, 4 deletions
diff --git a/lib/yahns/config.rb b/lib/yahns/config.rb
index f1b3612..c955e0f 100644
--- a/lib/yahns/config.rb
+++ b/lib/yahns/config.rb
@@ -73,10 +73,40 @@ class Yahns::Config # :nodoc:
     end
   end
 
-  def worker_processes(nr)
-    # TODO: allow zero
-    var = _check_in_block(nil, :worker_processes)
+  def worker_processes(nr, &blk)
+    var =_check_in_block(nil, :worker_processes)
     @set[var] = _check_int(var, nr, 1)
+    if block_given?
+      @block = CfgBlock.new(var, nil)
+      instance_eval(&blk)
+      @block = nil
+    end
+  end
+
+  %w(prepare parent child).each do |x|
+    fn = "atfork_#{x}"
+    eval(
+    %Q(def #{fn}(*args, &blk);) <<
+    %Q(  _check_in_block(:worker_processes, :#{fn});) <<
+    %Q(  _add_hook("worker_", :#{fn}, block_given? ? blk : args[0]);) <<
+    %Q(end)
+    )
+  end
+
+  def _add_hook(pfx, var, my_proc)
+    case my_proc
+    when Proc
+      my_proc.arity == 0 or raise ArgumentError,
+                         "#{var}=#{my_proc.inspect} should not take arguments"
+    else
+      raise ArgumentError, "invalid type: #{var}=#{my_proc.inspect}"
+    end
+
+    # this sets:
+    # :worker_atfork_prepare, :worker_atfork_parent, :worker_atfork_child
+    key = :"#{pfx}#{var}"
+    @set[key] = [] unless @set.include?(key)
+    @set[key] << my_proc
   end
 
   # sets the +path+ for the PID file of the yahns master process
@@ -338,7 +368,9 @@ class Yahns::Config # :nodoc:
       io.sync = true
     end
 
-    [ :logger, :pid, :worker_processes ].each do |var|
+    [ :logger, :pid, :worker_processes,
+      :worker_atfork_prepare, :worker_atfork_parent, :worker_atfork_child
+    ].each do |var|
       val = @set[var]
       server.__send__("#{var}=", val) if val != :unset
     end