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 class ForkController
{
11 var $children = array();
12 var $termReceived = false;
13 var $flags = 0, $procsToStart = 0;
15 static $restartableSignals = array(
28 * Pass this flag to __construct() to cause the class to automatically restart
29 * workers that exit with non-zero exit status or a signal such as SIGSEGV.
31 const RESTART_ON_ERROR
= 1;
33 public function __construct( $numProcs, $flags = 0 ) {
34 if ( php_sapi_name() != 'cli' ) {
35 throw new MWException( "MultiProcess cannot be used from the web." );
37 $this->procsToStart
= $numProcs;
38 $this->flags
= $flags;
42 * Start the child processes.
44 * This should only be called from the command line. It should be called
45 * as early as possible during execution.
47 * This will return 'child' in the child processes. In the parent process,
48 * it will run until all the child processes exit or a TERM signal is
49 * received. It will then return 'done'.
51 public function start() {
53 pcntl_signal( SIGTERM
, array( $this, 'handleTermSignal' ), false );
56 // Start child processes
57 if ( $this->procsToStart
) {
58 if ( $this->forkWorkers( $this->procsToStart
) == 'child' ) {
61 $this->procsToStart
= 0;
66 $deadPid = pcntl_wait( $status );
69 // Respond to child process termination
70 unset( $this->children
[$deadPid] );
71 if ( $this->flags
& self
::RESTART_ON_ERROR
) {
72 if ( pcntl_wifsignaled( $status ) ) {
73 // Restart if the signal was abnormal termination
74 // Don't restart if it was deliberately killed
75 $signal = pcntl_wtermsig( $status );
76 if ( in_array( $signal, self
::$restartableSignals ) ) {
77 echo "Worker exited with signal $signal, restarting\n";
78 $this->procsToStart++
;
80 } elseif ( pcntl_wifexited( $status ) ) {
81 // Restart on non-zero exit status
82 $exitStatus = pcntl_wexitstatus( $status );
83 if ( $exitStatus != 0 ) {
84 echo "Worker exited with status $exitStatus, restarting\n";
85 $this->procsToStart++
;
87 echo "Worker exited normally\n";
92 if ( $this->procsToStart
) {
97 // Run signal handlers
98 if ( function_exists( 'pcntl_signal_dispatch' ) ) {
99 pcntl_signal_dispatch();
101 declare (ticks
=1) { $status = $status; }
103 // Respond to TERM signal
104 if ( $this->termReceived
) {
105 foreach ( $this->children
as $childPid => $unused ) {
106 posix_kill( $childPid, SIGTERM
);
108 $this->termReceived
= false;
110 } while ( count( $this->children
) );
111 pcntl_signal( SIGTERM
, SIG_DFL
);
115 protected function prepareEnvironment() {
116 global $wgCaches, $wgMemc;
117 // Don't share DB or memcached connections
118 wfGetLBFactory()->destroyInstance();
124 * Fork a number of worker processes.
126 protected function forkWorkers( $numProcs ) {
127 global $wgMemc, $wgCaches, $wgMainCacheType;
129 $this->prepareEnvironment();
131 // Create the child processes
132 for ( $i = 0; $i < $numProcs; $i++
) {
135 if ( $pid === -1 ||
$pid === false ) {
136 echo "Error creating child processes\n";
144 // This is the parent process
145 $this->children
[$pid] = true;
152 protected function initChild() {
153 global $wgMemc, $wgMainCacheType;
154 $wgMemc = wfGetCache( $wgMainCacheType );
155 $this->children
= null;
156 pcntl_signal( SIGTERM
, SIG_DFL
);
159 protected function handleTermSignal( $signal ) {
160 $this->termReceived
= true;