3 * Class for managing forking command line scripts.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
24 * Class for managing forking command line scripts.
25 * Currently just does forking and process control, but it could easily be extended
26 * to provide IPC and job dispatch.
28 * This class requires the posix and pcntl extensions.
30 * @ingroup Maintenance
32 class ForkController
{
33 protected $children = array(), $childNumber = 0;
34 protected $termReceived = false;
35 protected $flags = 0, $procsToStart = 0;
37 protected static $restartableSignals = array(
50 * Pass this flag to __construct() to cause the class to automatically restart
51 * workers that exit with non-zero exit status or a signal such as SIGSEGV.
53 const RESTART_ON_ERROR
= 1;
55 public function __construct( $numProcs, $flags = 0 ) {
56 if ( PHP_SAPI
!= 'cli' ) {
57 throw new MWException( "ForkController cannot be used from the web." );
59 $this->procsToStart
= $numProcs;
60 $this->flags
= $flags;
64 * Start the child processes.
66 * This should only be called from the command line. It should be called
67 * as early as possible during execution.
69 * This will return 'child' in the child processes. In the parent process,
70 * it will run until all the child processes exit or a TERM signal is
71 * received. It will then return 'done'.
74 public function start() {
76 pcntl_signal( SIGTERM
, array( $this, 'handleTermSignal' ), false );
79 // Start child processes
80 if ( $this->procsToStart
) {
81 if ( $this->forkWorkers( $this->procsToStart
) == 'child' ) {
84 $this->procsToStart
= 0;
89 $deadPid = pcntl_wait( $status );
92 // Respond to child process termination
93 unset( $this->children
[$deadPid] );
94 if ( $this->flags
& self
::RESTART_ON_ERROR
) {
95 if ( pcntl_wifsignaled( $status ) ) {
96 // Restart if the signal was abnormal termination
97 // Don't restart if it was deliberately killed
98 $signal = pcntl_wtermsig( $status );
99 if ( in_array( $signal, self
::$restartableSignals ) ) {
100 echo "Worker exited with signal $signal, restarting\n";
101 $this->procsToStart++
;
103 } elseif ( pcntl_wifexited( $status ) ) {
104 // Restart on non-zero exit status
105 $exitStatus = pcntl_wexitstatus( $status );
106 if ( $exitStatus != 0 ) {
107 echo "Worker exited with status $exitStatus, restarting\n";
108 $this->procsToStart++
;
110 echo "Worker exited normally\n";
115 if ( $this->procsToStart
) {
120 // Run signal handlers
121 if ( function_exists( 'pcntl_signal_dispatch' ) ) {
122 pcntl_signal_dispatch();
124 declare( ticks
= 1 ) {
128 // Respond to TERM signal
129 if ( $this->termReceived
) {
130 foreach ( $this->children
as $childPid => $unused ) {
131 posix_kill( $childPid, SIGTERM
);
133 $this->termReceived
= false;
135 } while ( count( $this->children
) );
136 pcntl_signal( SIGTERM
, SIG_DFL
);
141 * Get the number of the child currently running. Note, this
142 * is not the pid, but rather which of the total number of children
146 public function getChildNumber() {
147 return $this->childNumber
;
150 protected function prepareEnvironment() {
152 // Don't share DB, storage, or memcached connections
153 wfGetLBFactory()->destroyInstance();
154 FileBackendGroup
::destroySingleton();
155 LockManagerGroup
::destroySingletons();
156 JobQueueGroup
::destroySingletons();
157 ObjectCache
::clear();
158 RedisConnectionPool
::destroySingletons();
163 * Fork a number of worker processes.
165 * @param int $numProcs
168 protected function forkWorkers( $numProcs ) {
169 $this->prepareEnvironment();
171 // Create the child processes
172 for ( $i = 0; $i < $numProcs; $i++
) {
175 if ( $pid === -1 ||
$pid === false ) {
176 echo "Error creating child processes\n";
182 $this->childNumber
= $i;
185 // This is the parent process
186 $this->children
[$pid] = true;
193 protected function initChild() {
194 global $wgMemc, $wgMainCacheType;
195 $wgMemc = wfGetCache( $wgMainCacheType );
196 $this->children
= null;
197 pcntl_signal( SIGTERM
, SIG_DFL
);
200 protected function handleTermSignal( $signal ) {
201 $this->termReceived
= true;