]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/ForkController.php
MediaWiki 1.17.0
[autoinstalls/mediawiki.git] / includes / ForkController.php
1 <?php
2
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.
7  *
8  * This class requires the posix and pcntl extensions.
9  *
10  * @ingroup Maintenance
11  */
12 class ForkController {
13         var $children = array();
14         var $termReceived = false;
15         var $flags = 0, $procsToStart = 0;
16
17         static $restartableSignals = array(
18                 SIGFPE,
19                 SIGILL,
20                 SIGSEGV,
21                 SIGBUS,
22                 SIGABRT,
23                 SIGSYS,
24                 SIGPIPE,
25                 SIGXCPU,
26                 SIGXFSZ,
27         );
28
29         /**
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.
32          */
33         const RESTART_ON_ERROR = 1;
34
35         public function __construct( $numProcs, $flags = 0 ) {
36                 if ( php_sapi_name() != 'cli' ) {
37                         throw new MWException( "MultiProcess cannot be used from the web." );
38                 }
39                 $this->procsToStart = $numProcs;
40                 $this->flags = $flags;
41         }
42
43         /**
44          * Start the child processes.
45          *
46          * This should only be called from the command line. It should be called
47          * as early as possible during execution.
48          *
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'.
52          */
53         public function start() {
54                 // Trap SIGTERM
55                 pcntl_signal( SIGTERM, array( $this, 'handleTermSignal' ), false );
56
57                 do {
58                         // Start child processes
59                         if ( $this->procsToStart ) {
60                                 if ( $this->forkWorkers( $this->procsToStart ) == 'child' ) {
61                                         return 'child';
62                                 }
63                                 $this->procsToStart = 0;
64                         }
65
66                         // Check child status
67                         $status = false;
68                         $deadPid = pcntl_wait( $status );
69
70                         if ( $deadPid > 0 ) {
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++;
81                                                 }
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++;
88                                                 } else {
89                                                         echo "Worker exited normally\n";
90                                                 }
91                                         }
92                                 }
93                                 // Throttle restarts
94                                 if ( $this->procsToStart ) {
95                                         usleep( 500000 );
96                                 }
97                         }
98
99                         // Run signal handlers
100                         if ( function_exists( 'pcntl_signal_dispatch' ) ) {
101                                 pcntl_signal_dispatch();
102                         } else {
103                                 declare (ticks=1) { $status = $status; }
104                         }
105                         // Respond to TERM signal
106                         if ( $this->termReceived ) {
107                                 foreach ( $this->children as $childPid => $unused ) {
108                                         posix_kill( $childPid, SIGTERM );
109                                 }
110                                 $this->termReceived = false;
111                         }
112                 } while ( count( $this->children ) );
113                 pcntl_signal( SIGTERM, SIG_DFL );
114                 return 'done';
115         }
116
117         protected function prepareEnvironment() {
118                 global $wgCaches, $wgMemc;
119                 // Don't share DB or memcached connections
120                 wfGetLBFactory()->destroyInstance();
121                 $wgCaches = array();
122                 unset( $wgMemc );
123         }
124
125         /**
126          * Fork a number of worker processes.
127          */
128         protected function forkWorkers( $numProcs ) {
129                 $this->prepareEnvironment();
130
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 );
138                         }
139
140                         if ( !$pid ) {
141                                 $this->initChild();
142                                 return 'child';
143                         } else {
144                                 // This is the parent process
145                                 $this->children[$pid] = true;
146                         }
147                 }
148
149                 return 'parent';
150         }
151
152         protected function initChild() {
153                 global $wgMemc, $wgMainCacheType;
154                 $wgMemc = wfGetCache( $wgMainCacheType );
155                 $this->children = null;
156                 pcntl_signal( SIGTERM, SIG_DFL );
157         }
158
159         protected function handleTermSignal( $signal ) {
160                 $this->termReceived = true;
161         }
162 }