Add an optional hook to perform arbitrary actions before forking.
3 files changed, 40 insertions(+), 1 deletions(-)

M lib/strelka.rb
M lib/strelka/multirunner.rb
M spec/strelka_spec.rb
M lib/strelka.rb +25 -1
@@ 22,7 22,7 @@ module Strelka
 	extend Loggability
 
 	# Library version constant
-	VERSION = '0.17.0'
+	VERSION = '0.18.0'
 
 	# Version-control revision constant
 	REVISION = %q$Revision$

          
@@ 78,6 78,11 @@ module Strelka
 	@after_configure_hooks = Set.new
 
 	##
+	# An Array of callbacks to be run before forking a handler process
+	singleton_attr_reader :before_fork_hooks
+	@before_fork_hooks = Set.new
+
+	##
 	# True if the after_configure hooks have already (started to) run.
 	singleton_predicate_reader :after_configure_hooks_run
 	@after_configure_hooks_run = false

          
@@ 123,6 128,25 @@ module Strelka
 	end
 
 
+	### Call the before-fork hooks.
+	def self::call_before_fork_hooks
+		self.log.debug "  calling %d before-fork hooks" % [ self.before_fork_hooks.length ]
+
+		self.before_fork_hooks.to_a.each do |hook|
+			self.log.debug "    %s line %s..." % hook.source_location
+			hook.call
+		end
+	end
+
+
+	### Register a callback to be run before forking a Strelka::Handler
+	### subprocess.
+	def self::before_fork( &block )
+		raise LocalJumpError, "no block given" unless block
+		self.before_fork_hooks << block
+	end
+
+
 	### Load the specified +config_file+, install the config in all objects with
 	### Configurability, and call any callbacks registered via #after_configure.
 	def self::load_config( config_file=nil, defaults=nil )

          
M lib/strelka/multirunner.rb +1 -0
@@ 70,6 70,7 @@ class Strelka::MultiRunner
 	### Start the handlers using fork().
 	def spawn_children
 		self.number.times do
+			Strelka.call_before_fork_hooks
 			pid = Process.fork do
 				Process.setpgrp
 				self.app_class.run

          
M spec/strelka_spec.rb +14 -0
@@ 189,5 189,19 @@ RSpec.describe Strelka do
 	end
 
 
+	it "provides a way to register blocks that should run before a fork" do
+		callback_ran = false
+		Strelka.before_fork { callback_ran = true }
+		Strelka.call_before_fork_hooks
+
+		expect( callback_ran ).to be( true )
+	end
+
+	it "raises an exception if .before_fork is called without a block" do
+		expect {
+			Strelka.before_fork
+		}.to raise_error( LocalJumpError, /no block given/i )
+	end
+
 end