@@ -72,6 +72,13 @@ class fork_daemon
72
72
*/
73
73
private $ child_function_timeout = array (self ::DEFAULT_BUCKET => '' );
74
74
75
+ /**
76
+ * Function the parent invokes before forking a child
77
+ * @access private
78
+ * @var integer $parent_function_prefork
79
+ */
80
+
8000
private $ parent_function_prefork = '' ;
81
+
75
82
/**
76
83
* Function the parent invokes when a child is spawned
77
84
* @access private
@@ -101,7 +108,7 @@ class fork_daemon
101
108
* @var integer $child_function_sighup
102
109
*/
103
110
private $ child_function_sighup = array (self ::DEFAULT_BUCKET => '' );
104
-
111
+
105
112
/**
106
113
* Max number of seconds to wait for a child process
107
114
* exit once it has been requested to exit
@@ -380,6 +387,18 @@ public function register_child_run($function_name, $bucket = self::DEFAULT_BUCKE
380
387
return false ;
381
388
}
382
389
390
+ /**
391
+ * Allows the app to set call back functions to cleanup resources before forking
392
+ * @access public
393
+ * @param array names of functions to be called.
394
+ * @return bool true if the callback was successfully registered, false if it failed
395
+ */
396
+ public function register_parent_prefork ($ function_names )
397
+ {
398
+ $ this ->parent_function_prefork = $ function_names ;
399
+ return true ;
400
+ }
401
+
383
402
/**
384
403
* Allows the app to set the call back function for when a child process is spawned
385
404
* @access public
@@ -513,7 +532,7 @@ public function register_parent_child_exit($function_name, $bucket = self::DEFAU
513
532
514
533
return false ;
515
534
}
516
-
535
+
517
536
/**
518
537
* Allows the app to set the call back function for logging
519
538
* @access public
@@ -560,6 +579,9 @@ public function __construct()
560
579
pcntl_signal (SIGQUIT , SIG_IGN );
561
580
pcntl_signal (SIGTRAP , SIG_IGN );
562
581
pcntl_signal (SIGSYS , SIG_IGN );
582
+
583
+ /* add barracuda specific prefork functions (doesn't hurt anything) */
584
+ $ this ->parent_function_prefork = array ('db_clear_connection_cache ' , 'memcache_clear_connection_cache ' );
563
585
}
564
586
565
587
/**
@@ -607,7 +629,7 @@ public function signal_handler_sighup($signal_number)
607
629
);
608
630
}
609
631
}
610
-
632
+
611
633
/**
612
634
* Handle parent registered sigchild callbacks.
613
635
*
@@ -651,7 +673,7 @@ public function signal_handler_sigchild($signal_number)
651
673
// respawn helper processes
652
674
if ($ child ['status ' ] == self ::HELPER && $ child ['respawn ' ] === true )
653
675
{
654
- Log:: message ('Helper process ' . $ child_pid . ' died, respawning ' , LOG_LEVEL_INFO );
676
+ $ this -> log ('Helper proces
E294
s ' . $ child_pid . ' died, respawning ' , self :: LOG_LEVEL_INFO );
655
677
$ this ->helper_process_spawn ($ child ['function ' ], $ child ['arguments ' ], $ child ['identifier ' ], true );
656
678
}
657
679
}
@@ -665,7 +687,7 @@ public function signal_handler_sigchild($signal_number)
665
687
}
666
688
}
667
689
}
668
-
690
+
669
691
/**
670
692
* Handle both parent and child registered sigint callbacks
671
693
*
@@ -679,7 +701,7 @@ public function signal_handler_sigint($signal_number)
679
701
// kill child processes
680
702
if (self ::$ parent_pid == getmypid ())
681
703
{
682
- foreach ($ this ->forked_children as $ pid => $ pid_info )
704
+ foreach ($ this ->forked_children as $ pid => & $ pid_info )
683
705
{
684
706
// tell helpers not to respawn
685
707
if ($ pid_info ['status ' ] == self ::HELPER )
@@ -690,10 +712,10 @@ public function signal_handler_sigint($signal_number)
690
712
}
691
713
692
714
sleep (1 );
693
-
715
+
694
716
// checking for missed sigchild
695
717
$ this ->signal_handler_sigchild (SIGCHLD );
696
-
718
+
697
719
$ start_time = time ();
698
720
699
721
// wait for child processes to go away
@@ -705,7 +727,7 @@ public function signal_handler_sigint($signal_number)
705
727
{
706
728
$ this ->log ('force killing child pid: ' . $ pid , self ::LOG_LEVEL_INFO );
707
729
posix_kill ($ pid , SIGKILL );
708
-
730
+
709
731
// remove the killed process from being tracked
710
732
unset($ this ->forked_children [$ pid ]);
711
733
}
@@ -724,7 +746,8 @@ public function signal_handler_sigint($signal_number)
724
746
else
725
747
{
726
748
// invoke child cleanup callback
727
- $ this ->invoke_callback ($ this ->child_function_exit [$ this ->child_bucket ], $ parameters = array ($ this ->child_bucket ), true );
749
+ if (isset ($ this ->child_bucket ))
750
+ $ this ->invoke_callback ($ this ->child_function_exit [$ this ->child_bucket ], $ parameters = array ($ this ->child_bucket ), true );
728
751
}
729
752
730
753
exit (-1 );
@@ -897,7 +920,7 @@ public function children_running($bucket = self::DEFAULT_BUCKET)
897
920
}
898
921
return $ count ;
899
922
}
900
-
923
+
901
924
/**
902
925
* Check if the current processes is a child
903
926
*
@@ -921,7 +944,7 @@ static public function is_child()
921
944
* @param string $identifier helper process unique identifier
922
945
* @param bool $respawn whether to respawn the helper process when it dies
923
946
*/
924
- public function helper_process_spawn ($ function_name , $ arguments , $ idenfifier = '' , $ respawn = true )
947
+ public function helper_process_spawn ($ function_name , $ arguments = array () , $ idenfifier = '' , $ respawn = true )
925
948
{
926
949
if ((is_array ($ function_name ) && method_exists ($ function_name [0 ], $ function_name [1 ])) || function_exists ($ function_name ))
927
950
{
@@ -937,14 +960,14 @@ public function helper_process_spawn($function_name, $arguments, $idenfifier = '
937
960
{
938
961
declare (ticks = 1 );
939
962
940
- Log:: message ('Calling function ' . $ function_name , LOG_LEVEL_DEBUG );
963
+ $ this -> log ('Calling function ' . $ function_name , self :: LOG_LEVEL_DEBUG );
941
964
call_user_func_array ($ function_name , $ arguments );
942
965
exit (0 );
943
966
}
944
967
else
945
968
{
946
969
declare (ticks = 1 );
947
- Log:: message ('Spawned new helper process with pid ' . $ pid , LOG_LEVEL_INFO );
970
+ $ this -> log ('Spawned new helper process with pid ' . $ pid , self :: LOG_LEVEL_INFO );
948
971
949
972
$ this ->forked_children [$ pid ] = array (
950
973
'ctime ' => time (),
@@ -959,7 +982,7 @@ public function helper_process_spawn($function_name, $arguments, $idenfifier = '
959
982
}
960
983
else
961
98
10000
4
{
962
- Log:: message ("Unable to spawn undefined helper function ' " . $ function_name . "' " , LOG_LEVEL_ERR );
985
+ $ this -> log ("Unable to spawn undefined helper function ' " . $ function_name . "' " , self :: LOG_LEVEL_ERR );
963
986
}
964
987
}
965
988
@@ -976,7 +999,7 @@ public function helper_process_respawn($identifier)
976
999
{
977
1000
if ($ child ['status ' ] == self ::HELPER && $ child ['identifier ' ] == $ identifier )
978
1001
{
979
- Log:: message ('Forcing helper process \'' . $ identifier . '\' with pid ' . $ pid . ' to respawn ' , LOG_LEVEL_INFO );
1002
+ $ this -> log ('Forcing helper process \'' . $ identifier . '\' with pid ' . $ pid . ' to respawn ' , self :: LOG_LEVEL_INFO );
980
1003
posix_kill ($ pid , SIGKILL );
981
1004
}
982
1005
}
@@ -1102,13 +1125,11 @@ private function process_work_unit($bucket = self::DEFAULT_BUCKET)
1102
1125
*/
1103
1126
private function fork_work_unit ($ work_unit , $ identifier = '' , $ bucket = self ::DEFAULT_BUCKET )
1104
1127
{
1105
- // clear all database connections if the database engine is enabled
1106
- if (function_exists ('db_clear_connection_cache ' ))
1107
- db_clear_connection_cache ();
1108
-
1109
- // clear all memcache connections if memcache is in use
1110
- if (function_exists ('memcache_clear_connection_cache ' ))
1111
- memcache_clear_connection_cache ();
1128
+ // prefork callback
1129
+ foreach ($ this ->parent_function_prefork as $ function )
1130
+ {
1131
+ $ this ->invoke_callback ($ function , array (), true );
1132
+ }
1112
1133
1113
1134
// turn off signals temporarily to prevent a SIGCHLD from interupting the parent before $this->forked_children is updated
1114
1135
declare (ticks = 0 );
@@ -1285,7 +1306,7 @@ private function log($message, $severity)
1285
1306
// Barracuda specific logging class, to keep internal code working
1286
1307
elseif (method_exists ('Log ' , 'message ' ))
1287
1308
{
1288
- return Log:: message ($ message , $ severity );
1309
+ return $ this -> log ($ message , $ severity );
1289
1310
}
1290
1311
1291
1312
return true ;
0 commit comments