(bug 19441) Updated date formatting for Lithuanian
[mediawiki.git] / includes / ForkController.php
blob6a386995c3e0218619ce03efce41ecf35ccf72d1
1 <?php
3 /**
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.
9 */
10 class ForkController {
11 var $children = array();
12 var $termReceived = false;
13 var $flags = 0, $procsToStart = 0;
15 static $restartableSignals = array(
16 SIGFPE,
17 SIGILL,
18 SIGSEGV,
19 SIGBUS,
20 SIGABRT,
21 SIGSYS,
22 SIGPIPE,
23 SIGXCPU,
24 SIGXFSZ,
27 /**
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;
41 /**
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() {
52 // Trap SIGTERM
53 pcntl_signal( SIGTERM, array( $this, 'handleTermSignal' ), false );
55 do {
56 // Start child processes
57 if ( $this->procsToStart ) {
58 if ( $this->forkWorkers( $this->procsToStart ) == 'child' ) {
59 return 'child';
61 $this->procsToStart = 0;
64 // Check child status
65 $status = false;
66 $deadPid = pcntl_wait( $status );
68 if ( $deadPid > 0 ) {
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++;
86 } else {
87 echo "Worker exited normally\n";
91 // Throttle restarts
92 if ( $this->procsToStart ) {
93 usleep( 500000 );
97 // Run signal handlers
98 if ( function_exists( 'pcntl_signal_dispatch' ) ) {
99 pcntl_signal_dispatch();
100 } else {
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 );
112 return 'done';
115 protected function prepareEnvironment() {
116 global $wgCaches, $wgMemc;
117 // Don't share DB or memcached connections
118 wfGetLBFactory()->destroyInstance();
119 $wgCaches = array();
120 unset( $wgMemc );
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++ ) {
133 // Do the fork
134 $pid = pcntl_fork();
135 if ( $pid === -1 || $pid === false ) {
136 echo "Error creating child processes\n";
137 exit( 1 );
140 if ( !$pid ) {
141 $this->initChild();
142 return 'child';
143 } else {
144 // This is the parent process
145 $this->children[$pid] = true;
149 return 'parent';
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;