* Document a bit
[lhc/web/wiklou.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 class ForkController {
11 var $children = array();
12 var $termReceived = false;
13 var $flags = 0, $procsToStart = 0;
14
15 static $restartableSignals = array(
16 SIGFPE,
17 SIGILL,
18 SIGSEGV,
19 SIGBUS,
20 SIGABRT,
21 SIGSYS,
22 SIGPIPE,
23 SIGXCPU,
24 SIGXFSZ,
25 );
26
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.
30 */
31 const RESTART_ON_ERROR = 1;
32
33 public function __construct( $numProcs, $flags = 0 ) {
34 if ( php_sapi_name() != 'cli' ) {
35 throw new MWException( "MultiProcess cannot be used from the web." );
36 }
37 $this->procsToStart = $numProcs;
38 $this->flags = $flags;
39 }
40
41 /**
42 * Start the child processes.
43 *
44 * This should only be called from the command line. It should be called
45 * as early as possible during execution.
46 *
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'.
50 */
51 public function start() {
52 // Trap SIGTERM
53 pcntl_signal( SIGTERM, array( $this, 'handleTermSignal' ), false );
54
55 do {
56 // Start child processes
57 if ( $this->procsToStart ) {
58 if ( $this->forkWorkers( $this->procsToStart ) == 'child' ) {
59 return 'child';
60 }
61 $this->procsToStart = 0;
62 }
63
64 // Check child status
65 $status = false;
66 $deadPid = pcntl_wait( $status );
67
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++;
79 }
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";
88 }
89 }
90 }
91 // Throttle restarts
92 if ( $this->procsToStart ) {
93 usleep( 500000 );
94 }
95 }
96
97 // Run signal handlers
98 if ( function_exists( 'pcntl_signal_dispatch' ) ) {
99 pcntl_signal_dispatch();
100 } else {
101 declare (ticks=1) { $status = $status; }
102 }
103 // Respond to TERM signal
104 if ( $this->termReceived ) {
105 foreach ( $this->children as $childPid => $unused ) {
106 posix_kill( $childPid, SIGTERM );
107 }
108 $this->termReceived = false;
109 }
110 } while ( count( $this->children ) );
111 pcntl_signal( SIGTERM, SIG_DFL );
112 return 'done';
113 }
114
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 );
121 }
122
123 /**
124 * Fork a number of worker processes.
125 */
126 protected function forkWorkers( $numProcs ) {
127 global $wgMemc, $wgCaches, $wgMainCacheType;
128
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 }