MediaWiki  1.23.2
ForkController.php
Go to the documentation of this file.
1 <?php
33  protected $children = array(), $childNumber = 0;
34  protected $termReceived = false;
35  protected $flags = 0, $procsToStart = 0;
36 
37  protected static $restartableSignals = array(
38  SIGFPE,
39  SIGILL,
40  SIGSEGV,
41  SIGBUS,
42  SIGABRT,
43  SIGSYS,
44  SIGPIPE,
45  SIGXCPU,
46  SIGXFSZ,
47  );
48 
53  const RESTART_ON_ERROR = 1;
54 
55  public function __construct( $numProcs, $flags = 0 ) {
56  if ( PHP_SAPI != 'cli' ) {
57  throw new MWException( "ForkController cannot be used from the web." );
58  }
59  $this->procsToStart = $numProcs;
60  $this->flags = $flags;
61  }
62 
74  public function start() {
75  // Trap SIGTERM
76  pcntl_signal( SIGTERM, array( $this, 'handleTermSignal' ), false );
77 
78  do {
79  // Start child processes
80  if ( $this->procsToStart ) {
81  if ( $this->forkWorkers( $this->procsToStart ) == 'child' ) {
82  return 'child';
83  }
84  $this->procsToStart = 0;
85  }
86 
87  // Check child status
88  $status = false;
89  $deadPid = pcntl_wait( $status );
90 
91  if ( $deadPid > 0 ) {
92  // Respond to child process termination
93  unset( $this->children[$deadPid] );
94  if ( $this->flags & self::RESTART_ON_ERROR ) {
95  if ( pcntl_wifsignaled( $status ) ) {
96  // Restart if the signal was abnormal termination
97  // Don't restart if it was deliberately killed
98  $signal = pcntl_wtermsig( $status );
99  if ( in_array( $signal, self::$restartableSignals ) ) {
100  echo "Worker exited with signal $signal, restarting\n";
101  $this->procsToStart++;
102  }
103  } elseif ( pcntl_wifexited( $status ) ) {
104  // Restart on non-zero exit status
105  $exitStatus = pcntl_wexitstatus( $status );
106  if ( $exitStatus != 0 ) {
107  echo "Worker exited with status $exitStatus, restarting\n";
108  $this->procsToStart++;
109  } else {
110  echo "Worker exited normally\n";
111  }
112  }
113  }
114  // Throttle restarts
115  if ( $this->procsToStart ) {
116  usleep( 500000 );
117  }
118  }
119 
120  // Run signal handlers
121  if ( function_exists( 'pcntl_signal_dispatch' ) ) {
122  pcntl_signal_dispatch();
123  } else {
124  declare( ticks = 1 ) {
125  $status = $status;
126  }
127  }
128  // Respond to TERM signal
129  if ( $this->termReceived ) {
130  foreach ( $this->children as $childPid => $unused ) {
131  posix_kill( $childPid, SIGTERM );
132  }
133  $this->termReceived = false;
134  }
135  } while ( count( $this->children ) );
136  pcntl_signal( SIGTERM, SIG_DFL );
137  return 'done';
138  }
139 
146  public function getChildNumber() {
147  return $this->childNumber;
148  }
149 
150  protected function prepareEnvironment() {
151  global $wgMemc;
152  // Don't share DB, storage, or memcached connections
153  wfGetLBFactory()->destroyInstance();
157  $wgMemc = null;
158  }
159 
165  protected function forkWorkers( $numProcs ) {
166  $this->prepareEnvironment();
167 
168  // Create the child processes
169  for ( $i = 0; $i < $numProcs; $i++ ) {
170  // Do the fork
171  $pid = pcntl_fork();
172  if ( $pid === -1 || $pid === false ) {
173  echo "Error creating child processes\n";
174  exit( 1 );
175  }
176 
177  if ( !$pid ) {
178  $this->initChild();
179  $this->childNumber = $i;
180  return 'child';
181  } else {
182  // This is the parent process
183  $this->children[$pid] = true;
184  }
185  }
186 
187  return 'parent';
188  }
189 
190  protected function initChild() {
193  $this->children = null;
194  pcntl_signal( SIGTERM, SIG_DFL );
195  }
196 
197  protected function handleTermSignal( $signal ) {
198  $this->termReceived = true;
199  }
200 }
ObjectCache\clear
static clear()
Clear all the cached instances.
Definition: ObjectCache.php:52
ForkController\RESTART_ON_ERROR
const RESTART_ON_ERROR
Pass this flag to __construct() to cause the class to automatically restart workers that exit with no...
Definition: ForkController.php:53
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
LockManagerGroup\destroySingletons
static destroySingletons()
Destroy the singleton instances.
Definition: LockManagerGroup.php:63
ForkController
Class for managing forking command line scripts.
Definition: ForkController.php:32
$wgMemc
globals will be eliminated from MediaWiki replaced by an application object which would be passed to constructors Whether that would be an convenient solution remains to be but certainly PHP makes such object oriented programming models easier than they were in previous versions For the time being MediaWiki programmers will have to work in an environment with some global context At the time of globals were initialised on startup by MediaWiki of these were configuration which are documented in DefaultSettings php There is no comprehensive documentation for the remaining however some of the most important ones are listed below They are typically initialised either in index php or in Setup php For a description of the see design txt $wgTitle Title object created from the request URL $wgOut OutputPage object for HTTP response $wgUser User object for the user associated with the current request $wgLang Language object selected by user preferences $wgContLang Language object associated with the wiki being viewed $wgParser Parser object Parser extensions register their hooks here $wgRequest WebRequest to get request data $wgMemc
Definition: globals.txt:25
ForkController\handleTermSignal
handleTermSignal( $signal)
Definition: ForkController.php:197
wfGetCache
wfGetCache( $inputType)
Get a cache object.
Definition: GlobalFunctions.php:3948
ForkController\start
start()
Start the child processes.
Definition: ForkController.php:74
ForkController\getChildNumber
getChildNumber()
Get the number of the child currently running.
Definition: ForkController.php:146
FileBackendGroup\destroySingleton
static destroySingleton()
Destroy the singleton instance.
Definition: FileBackendGroup.php:55
ForkController\$termReceived
$termReceived
Definition: ForkController.php:34
ForkController\forkWorkers
forkWorkers( $numProcs)
Fork a number of worker processes.
Definition: ForkController.php:165
MWException
MediaWiki exception.
Definition: MWException.php:26
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
ForkController\$childNumber
$childNumber
Definition: ForkController.php:33
flags
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add in any and then calling but I prefer the flexibility This should also do the output encoding The system allocates a global one in $wgOut Title Represents the title of an and does all the work of translating among various forms such as plain database etc For and for historical it also represents a few features of articles that don t involve their such as access rights See also title txt Article Encapsulates access to the page table of the database The object represents a an and maintains state such as flags
Definition: design.txt:34
wfGetLBFactory
& wfGetLBFactory()
Get the load balancer factory object.
Definition: GlobalFunctions.php:3669
ForkController\prepareEnvironment
prepareEnvironment()
Definition: ForkController.php:150
$wgMainCacheType
CACHE_MEMCACHED $wgMainCacheType
Definition: memcached.txt:63
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
ForkController\$procsToStart
$procsToStart
Definition: ForkController.php:35
ForkController\$flags
$flags
Definition: ForkController.php:35
ForkController\$restartableSignals
static $restartableSignals
Definition: ForkController.php:37
ForkController\$children
$children
Definition: ForkController.php:33
ForkController\__construct
__construct( $numProcs, $flags=0)
Definition: ForkController.php:55
ForkController\initChild
initChild()
Definition: ForkController.php:190