8000 fix syntax of logging messages · notion/forkdaemon-php@ab54a0d · GitHub
[go: up one dir, main page]

Skip to content

Commit ab54a0d

Browse files
committed
fix syntax of logging messages
add ability to set preforking callbacks fix issue with child function exit callbacks being called on helper processes
1 parent 1c6410a commit ab54a0d

File tree

1 file changed

+45
-24
lines changed

1 file changed

+45
-24
lines changed

fork_daemon.php

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ class fork_daemon
7272
*/
7373
private $child_function_timeout = array(self::DEFAULT_BUCKET => '');
7474

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+
7582
/**
7683
* Function the parent invokes when a child is spawned
7784
* @access private
@@ -101,7 +108,7 @@ class fork_daemon
101108
* @var integer $child_function_sighup
102109
*/
103110
private $child_function_sighup = array(self::DEFAULT_BUCKET => '');
104-
111+
105112
/**
106113
* Max number of seconds to wait for a child process
107114
* exit once it has been requested to exit
@@ -380,6 +387,18 @@ public function register_child_run($function_name, $bucket = self::DEFAULT_BUCKE
380387
return false;
381388
}
382389

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+
383402
/**
384403
* Allows the app to set the call back function for when a child process is spawned
385404
* @access public
@@ -513,7 +532,7 @@ public function register_parent_child_exit($function_name, $bucket = self::DEFAU
513532

514533
return false;
515534
}
516-
535+
517536
/**
518537
* Allows the app to set the call back function for logging
519538
* @access public
@@ -560,6 +579,9 @@ public function __construct()
560579
pcntl_signal(SIGQUIT, SIG_IGN);
561580
pcntl_signal(SIGTRAP, SIG_IGN);
562581
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');
563585
}
564586

565587
/**
@@ -607,7 +629,7 @@ public function signal_handler_sighup($signal_number)
607629
);
608630
}
609631
}
610-
632+
611633
/**
612634
* Handle parent registered sigchild callbacks.
613635
*
@@ -651,7 +673,7 @@ public function signal_handler_sigchild($signal_number)
651673
// respawn helper processes
652674
if ($child['status'] == self::HELPER && $child['respawn'] === true)
653675
{
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);
655677
$this->helper_process_spawn($child['function'], $child['arguments'], $child['identifier'], true);
656678
}
657679
}
@@ -665,7 +687,7 @@ public function signal_handler_sigchild($signal_number)
665687
}
666688
}
667689
}
668-
690+
669691
/**
670692
* Handle both parent and child registered sigint callbacks
671693
*
@@ -679,7 +701,7 @@ public function signal_handler_sigint($signal_number)
679701
// kill child processes
680702
if (self::$parent_pid == getmypid())
681703
{
682-
foreach ($this->forked_children as $pid => $pid_info)
704+
foreach ($this->forked_children as $pid => &$pid_info)
683705
{
684706
// tell helpers not to respawn
685707
if ($pid_info['status'] == self::HELPER)
@@ -690,10 +712,10 @@ public function signal_handler_sigint($signal_number)
690712
}
691713

692714
sleep(1);
693-
715+
694716
// checking for missed sigchild
695717
$this->signal_handler_sigchild(SIGCHLD);
696-
718+
697719
$start_time = time();
698720

699721
// wait for child processes to go away
@@ -705,7 +727,7 @@ public function signal_handler_sigint($signal_number)
705727
{
706728
$this->log('force killing child pid: ' . $pid, self::LOG_LEVEL_INFO);
707729
posix_kill($pid, SIGKILL);
708-
730+
709731
// remove the killed process from being tracked
710732
unset($this->forked_children[$pid]);
711733
}
@@ -724,7 +746,8 @@ public function signal_handler_sigint($signal_number)
724746
else
725747
{
726748
// 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);
728751
}
729752

730753
exit(-1);
@@ -897,7 +920,7 @@ public function children_running($bucket = self::DEFAULT_BUCKET)
897920
}
898921
return $count;
899922
}
900-
923+
901924
/**
902925
* Check if the current processes is a child
903926
*
@@ -921,7 +944,7 @@ static public function is_child()
921944
* @param string $identifier helper process unique identifier
922945
* @param bool $respawn whether to respawn the helper process when it dies
923946
*/
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)
925948
{
926949
if ((is_array($function_name) && method_exists($function_name[0], $function_name[1])) || function_exists($function_name))
927950
{
@@ -937,14 +960,14 @@ public function helper_process_spawn($function_name, $arguments, $idenfifier = '
937960
{
938961
declare(ticks = 1);
939962

940-
Log::message('Calling function ' . $function_name, LOG_LEVEL_DEBUG);
963+
$this->log('Calling function ' . $function_name, self::LOG_LEVEL_DEBUG);
941964
call_user_func_array($function_name, $arguments);
942965
exit(0);
943966
}
944967
else
945968
{
946969
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);
948971

949972
$this->forked_children[$pid] = array(
950973
'ctime' => time(),
@@ -959,7 +982,7 @@ public function helper_process_spawn($function_name, $arguments, $idenfifier = '
959982
}
960983
else
96198 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);
963986
}
964987
}
965988

@@ -976,7 +999,7 @@ public function helper_process_respawn($identifier)
976999
{
9771000
if ($child['status'] == self::HELPER && $child['identifier'] == $identifier)
9781001
{
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);
9801003
posix_kill($pid, SIGKILL);
9811004
}
9821005
}
@@ -1102,13 +1125,11 @@ private function process_work_unit($bucket = self::DEFAULT_BUCKET)
11021125
*/
11031126
private function fork_work_unit($work_unit, $identifier = '', $bucket = self::DEFAULT_BUCKET)
11041127
{
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+
}
11121133

11131134
// turn off signals temporarily to prevent a SIGCHLD from interupting the parent before $this->forked_children is updated
11141135
declare(ticks = 0);
@@ -1285,7 +1306,7 @@ private function log($message, $severity)
12851306
// Barracuda specific logging class, to keep internal code working
12861307
elseif (method_exists('Log', 'message'))
12871308
{
1288-
return Log::message($message, $severity);
1309+
return $this->log($message, $severity);
12891310
}
12901311

12911312
return true;

0 commit comments

Comments
 (0)
0