00001 <?php
00002
00010 class ForkController {
00011 var $children = array();
00012 var $termReceived = false;
00013 var $flags = 0, $procsToStart = 0;
00014
00015 static $restartableSignals = array(
00016 SIGFPE,
00017 SIGILL,
00018 SIGSEGV,
00019 SIGBUS,
00020 SIGABRT,
00021 SIGSYS,
00022 SIGPIPE,
00023 SIGXCPU,
00024 SIGXFSZ,
00025 );
00026
00031 const RESTART_ON_ERROR = 1;
00032
00033 public function __construct( $numProcs, $flags = 0 ) {
00034 if ( php_sapi_name() != 'cli' ) {
00035 throw new MWException( "MultiProcess cannot be used from the web." );
00036 }
00037 $this->procsToStart = $numProcs;
00038 $this->flags = $flags;
00039 }
00040
00051 public function start() {
00052
00053 pcntl_signal( SIGTERM, array( $this, 'handleTermSignal' ), false );
00054
00055 do {
00056
00057 if ( $this->procsToStart ) {
00058 if ( $this->forkWorkers( $this->procsToStart ) == 'child' ) {
00059 return 'child';
00060 }
00061 $this->procsToStart = 0;
00062 }
00063
00064
00065 $status = false;
00066 $deadPid = pcntl_wait( $status );
00067
00068 if ( $deadPid > 0 ) {
00069
00070 unset( $this->children[$deadPid] );
00071 if ( $this->flags & self::RESTART_ON_ERROR ) {
00072 if ( pcntl_wifsignaled( $status ) ) {
00073
00074
00075 $signal = pcntl_wtermsig( $status );
00076 if ( in_array( $signal, self::$restartableSignals ) ) {
00077 echo "Worker exited with signal $signal, restarting\n";
00078 $this->procsToStart++;
00079 }
00080 } elseif ( pcntl_wifexited( $status ) ) {
00081
00082 $exitStatus = pcntl_wexitstatus( $status );
00083 if ( $exitStatus > 0 ) {
00084 echo "Worker exited with status $exitStatus, restarting\n";
00085 $this->procsToStart++;
00086 }
00087 }
00088 }
00089
00090 if ( $this->procsToStart ) {
00091 usleep( 500000 );
00092 }
00093 }
00094
00095
00096 if ( function_exists( 'pcntl_signal_dispatch' ) ) {
00097 pcntl_signal_dispatch();
00098 } else {
00099 declare (ticks=1) { $status = $status; }
00100 }
00101
00102 if ( $this->termReceived ) {
00103 foreach ( $this->children as $childPid => $unused ) {
00104 posix_kill( $childPid, SIGTERM );
00105 }
00106 $this->termReceived = false;
00107 }
00108 } while ( count( $this->children ) );
00109 pcntl_signal( SIGTERM, SIG_DFL );
00110 return 'done';
00111 }
00112
00113 protected function prepareEnvironment() {
00114 global $wgCaches, $wgMemc;
00115
00116 wfGetLBFactory()->destroyInstance();
00117 $wgCaches = array();
00118 unset( $wgMemc );
00119 }
00120
00124 protected function forkWorkers( $numProcs ) {
00125 global $wgMemc, $wgCaches, $wgMainCacheType;
00126
00127 $this->prepareEnvironment();
00128
00129
00130 for ( $i = 0; $i < $numProcs; $i++ ) {
00131
00132 $pid = pcntl_fork();
00133 if ( $pid === -1 || $pid === false ) {
00134 echo "Error creating child processes\n";
00135 exit( 1 );
00136 }
00137
00138 if ( !$pid ) {
00139 $this->initChild();
00140 return 'child';
00141 } else {
00142
00143 $this->children[$pid] = true;
00144 }
00145 }
00146
00147 return 'parent';
00148 }
00149
00150 protected function initChild() {
00151 global $wgMemc, $wgMainCacheType;
00152 $wgMemc = wfGetCache( $wgMainCacheType );
00153 $this->children = null;
00154 pcntl_signal( SIGTERM, SIG_DFL );
00155 }
00156
00157 protected function handleTermSignal( $signal ) {
00158 $this->termReceived = true;
00159 }
00160 }