4 * Class for managing forking command line scripts.
5 * Currently just does forking and process control, but it could easily be extended
6 * to provide IPC and job dispatch.
8 * This class requires the posix and pcntl extensions.
10 * @ingroup Maintenance
12 class ForkController {
13 var $children = array();
14 var $termReceived = false;
15 var $flags = 0, $procsToStart = 0;
17 static $restartableSignals = array(
30 * Pass this flag to __construct() to cause the class to automatically restart
31 * workers that exit with non-zero exit status or a signal such as SIGSEGV.
33 const RESTART_ON_ERROR = 1;
35 public function __construct( $numProcs, $flags = 0 ) {
36 if ( php_sapi_name() != 'cli' ) {
37 throw new MWException( "MultiProcess cannot be used from the web." );
39 $this->procsToStart = $numProcs;
40 $this->flags = $flags;
44 * Start the child processes.
46 * This should only be called from the command line. It should be called
47 * as early as possible during execution.
49 * This will return 'child' in the child processes. In the parent process,
50 * it will run until all the child processes exit or a TERM signal is
51 * received. It will then return 'done'.
53 public function start() {
55 pcntl_signal( SIGTERM, array( $this, 'handleTermSignal' ), false );
58 // Start child processes
59 if ( $this->procsToStart ) {
60 if ( $this->forkWorkers( $this->procsToStart ) == 'child' ) {
63 $this->procsToStart = 0;
68 $deadPid = pcntl_wait( $status );
71 // Respond to child process termination
72 unset( $this->children[$deadPid] );
73 if ( $this->flags & self::RESTART_ON_ERROR ) {
74 if ( pcntl_wifsignaled( $status ) ) {
75 // Restart if the signal was abnormal termination
76 // Don't restart if it was deliberately killed
77 $signal = pcntl_wtermsig( $status );
78 if ( in_array( $signal, self::$restartableSignals ) ) {
79 echo "Worker exited with signal $signal, restarting\n";
80 $this->procsToStart++;
82 } elseif ( pcntl_wifexited( $status ) ) {
83 // Restart on non-zero exit status
84 $exitStatus = pcntl_wexitstatus( $status );
85 if ( $exitStatus != 0 ) {
86 echo "Worker exited with status $exitStatus, restarting\n";
87 $this->procsToStart++;
89 echo "Worker exited normally\n";
94 if ( $this->procsToStart ) {
99 // Run signal handlers
100 if ( function_exists( 'pcntl_signal_dispatch' ) ) {
101 pcntl_signal_dispatch();
103 declare (ticks=1) { $status = $status; }
105 // Respond to TERM signal
106 if ( $this->termReceived ) {
107 foreach ( $this->children as $childPid => $unused ) {
108 posix_kill( $childPid, SIGTERM );
110 $this->termReceived = false;
112 } while ( count( $this->children ) );
113 pcntl_signal( SIGTERM, SIG_DFL );
117 protected function prepareEnvironment() {
118 global $wgCaches, $wgMemc;
119 // Don't share DB or memcached connections
120 wfGetLBFactory()->destroyInstance();
126 * Fork a number of worker processes.
128 protected function forkWorkers( $numProcs ) {
129 global $wgMemc, $wgCaches, $wgMainCacheType;
131 $this->prepareEnvironment();
133 // Create the child processes
134 for ( $i = 0; $i < $numProcs; $i++ ) {
137 if ( $pid === -1 || $pid === false ) {
138 echo "Error creating child processes\n";
146 // This is the parent process
147 $this->children[$pid] = true;
154 protected function initChild() {
155 global $wgMemc, $wgMainCacheType;
156 $wgMemc = wfGetCache( $wgMainCacheType );
157 $this->children = null;
158 pcntl_signal( SIGTERM, SIG_DFL );
161 protected function handleTermSignal( $signal ) {
162 $this->termReceived = true;