| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Maintenance; |
| 22 |
|
| 23 |
use MediaWiki\MediaWikiServices; |
| 24 |
use RuntimeException; |
| 25 |
use Wikimedia\ObjectCache\RedisConnectionPool; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
class ForkController { |
| 38 |
|
| 39 |
protected $children = []; |
| 40 |
|
| 41 |
|
| 42 |
protected $childNumber = 0; |
| 43 |
|
| 44 |
|
| 45 |
protected $termReceived = false; |
| 46 |
|
| 47 |
|
| 48 |
protected $flags = 0; |
| 49 |
|
| 50 |
|
| 51 |
protected $procsToStart = 0; |
| 52 |
|
| 53 |
|
| 54 |
protected static $RESTARTABLE_SIGNALS = []; |
| 55 |
|
| 56 |
|
| 57 |
protected $exitStatuses = []; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
private const RESTART_ON_ERROR = 1; |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public function __construct( $numProcs, $flags = 0 ) { |
| 70 |
if ( !wfIsCLI() ) { |
| 71 |
throw new RuntimeException( "MediaWiki\Maintenance\ForkController cannot be used from the web." ); |
| 72 |
} elseif ( !extension_loaded( 'pcntl' ) ) { |
| 73 |
throw new RuntimeException( |
| 74 |
'MediaWiki\Maintenance\ForkController requires pcntl extension to be installed.' |
| 75 |
); |
| 76 |
} elseif ( !extension_loaded( 'posix' ) ) { |
| 77 |
throw new RuntimeException( |
| 78 |
'MediaWiki\Maintenance\ForkController requires posix extension to be installed.' |
| 79 |
); |
| 80 |
} |
| 81 |
$this->procsToStart = $numProcs; |
| 82 |
$this->flags = $flags; |
| 83 |
|
| 84 |
|
| 85 |
self::$RESTARTABLE_SIGNALS = [ |
| 86 |
SIGFPE, SIGILL, SIGSEGV, SIGBUS, |
| 87 |
SIGABRT, SIGSYS, SIGPIPE, SIGXCPU, SIGXFSZ, |
| 88 |
]; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
public function start() { |
| 104 |
|
| 105 |
pcntl_signal( SIGTERM, [ $this, 'handleTermSignal' ], false ); |
| 106 |
|
| 107 |
do { |
| 108 |
|
| 109 |
if ( $this->procsToStart ) { |
| 110 |
if ( $this->forkWorkers( $this->procsToStart ) == 'child' ) { |
| 111 |
return 'child'; |
| 112 |
} |
| 113 |
$this->procsToStart = 0; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
$status = false; |
| 118 |
$deadPid = pcntl_wait( $status ); |
| 119 |
|
| 120 |
if ( $deadPid > 0 ) { |
| 121 |
|
| 122 |
unset( $this->children[$deadPid] ); |
| 123 |
if ( $this->flags & self::RESTART_ON_ERROR ) { |
| 124 |
if ( pcntl_wifsignaled( $status ) ) { |
| 125 |
|
| 126 |
|
| 127 |
$signal = pcntl_wtermsig( $status ); |
| 128 |
if ( in_array( $signal, self::$RESTARTABLE_SIGNALS ) ) { |
| 129 |
echo "Worker exited with signal $signal, restarting\n"; |
| 130 |
$this->procsToStart++; |
| 131 |
} |
| 132 |
} elseif ( pcntl_wifexited( $status ) ) { |
| 133 |
|
| 134 |
$exitStatus = pcntl_wexitstatus( $status ); |
| 135 |
if ( $exitStatus != 0 ) { |
| 136 |
echo "Worker exited with status $exitStatus, restarting\n"; |
| 137 |
$this->procsToStart++; |
| 138 |
} else { |
| 139 |
echo "Worker exited normally\n"; |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
if ( pcntl_wifexited( $status ) ) { |
| 145 |
$exitStatus = pcntl_wexitstatus( $status ); |
| 146 |
echo "Worker exited with status $exitStatus\n"; |
| 147 |
$this->exitStatuses[] = $exitStatus; |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
if ( $this->procsToStart ) { |
| 152 |
usleep( 500_000 ); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
if ( function_exists( 'pcntl_signal_dispatch' ) ) { |
| 158 |
pcntl_signal_dispatch(); |
| 159 |
} else { |
| 160 |
declare( ticks=1 ) { |
| 161 |
|
| 162 |
$status = $status; |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
if ( $this->termReceived ) { |
| 167 |
foreach ( $this->children as $childPid => $unused ) { |
| 168 |
posix_kill( $childPid, SIGTERM ); |
| 169 |
} |
| 170 |
$this->termReceived = false; |
| 171 |
} |
| 172 |
} while ( count( $this->children ) ); |
| 173 |
pcntl_signal( SIGTERM, SIG_DFL ); |
| 174 |
return 'done'; |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
public function allSuccessful(): bool { |
| 184 |
return array_reduce( |
| 185 |
$this->exitStatuses, |
| 186 |
static fn ( $acc, $status ) => $acc && ( $status === 0 ), |
| 187 |
true |
| 188 |
); |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
public function getChildNumber() { |
| 198 |
return $this->childNumber; |
| 199 |
} |
| 200 |
|
| 201 |
protected function prepareEnvironment() { |
| 202 |
|
| 203 |
MediaWikiServices::resetChildProcessServices(); |
| 204 |
MediaWikiServices::getInstance()->getObjectCacheFactory()->clear(); |
| 205 |
RedisConnectionPool::destroySingletons(); |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
protected function forkWorkers( $numProcs ) { |
| 215 |
$this->prepareEnvironment(); |
| 216 |
|
| 217 |
|
| 218 |
for ( $i = 0; $i < $numProcs; $i++ ) { |
| 219 |
|
| 220 |
$pid = pcntl_fork(); |
| 221 |
if ( $pid === -1 ) { |
| 222 |
echo "Error creating child processes\n"; |
| 223 |
exit( 1 ); |
| 224 |
} |
| 225 |
|
| 226 |
if ( !$pid ) { |
| 227 |
$this->initChild(); |
| 228 |
$this->childNumber = $i; |
| 229 |
return 'child'; |
| 230 |
} else { |
| 231 |
|
| 232 |
$this->children[$pid] = true; |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
return 'parent'; |
| 237 |
} |
| 238 |
|
| 239 |
protected function initChild() { |
| 240 |
$this->children = null; |
| 241 |
pcntl_signal( SIGTERM, SIG_DFL ); |
| 242 |
} |
| 243 |
|
| 244 |
protected function handleTermSignal( $signal ) { |
| 245 |
$this->termReceived = true; |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
class_alias( ForkController::class, 'ForkController' ); |