(bug 18317) Bolded selections in 1 | 3 | etc days on RecentChanges now use <strong...
[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 }
87 }
88 }
89 // Throttle restarts
90 if ( $this->procsToStart ) {
91 usleep( 500000 );
92 }
93 }
94
95 // Run signal handlers
96 if ( function_exists( 'pcntl_signal_dispatch' ) ) {
97 pcntl_signal_dispatch();
98 } else {
99 declare (ticks=1) { $status = $status; }
100 }
101 // Respond to TERM signal
102 if ( $this->termReceived ) {
103 foreach ( $this->children as $childPid => $unused ) {
104 posix_kill( $childPid, SIGTERM );
105 }
106 $this->termReceived = false;
107 }
108 } while ( count( $this->children ) );
109 pcntl_signal( SIGTERM, SIG_DFL );
110 return 'done';
111 }
112
113 protected function prepareEnvironment() {
114 global $wgCaches, $wgMemc;
115 // Don't share DB or memcached connections
116 wfGetLBFactory()->destroyInstance();
117 $wgCaches = array();
118 unset( $wgMemc );
119 }
120
121 /**
122 * Fork a number of worker processes.
123 */
124 protected function forkWorkers( $numProcs ) {
125 global $wgMemc, $wgCaches, $wgMainCacheType;
126
127 $this->prepareEnvironment();
128
129 // Create the child processes
130 for ( $i = 0; $i < $numProcs; $i++ ) {
131 // Do the fork
132 $pid = pcntl_fork();
133 if ( $pid === -1 || $pid === false ) {
134 echo "Error creating child processes\n";
135 exit( 1 );
136 }
137
138 if ( !$pid ) {
139 $this->initChild();
140 return 'child';
141 } else {
142 // This is the parent process
143 $this->children[$pid] = true;
144 }
145 }
146
147 return 'parent';
148 }
149
150 protected function initChild() {
151 global $wgMemc, $wgMainCacheType;
152 $wgMemc = wfGetCache( $wgMainCacheType );
153 $this->children = null;
154 pcntl_signal( SIGTERM, SIG_DFL );
155 }
156
157 protected function handleTermSignal( $signal ) {
158 $this->termReceived = true;
159 }
160 }