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
21 * @ingroup Maintenance
24 require_once __DIR__
. '/Maintenance.php';
27 * Maintenance script that runs pending jobs.
29 * @ingroup Maintenance
31 class RunJobs
extends Maintenance
{
32 public function __construct() {
33 parent
::__construct();
34 $this->mDescription
= "Run pending jobs";
35 $this->addOption( 'maxjobs', 'Maximum number of jobs to run', false, true );
36 $this->addOption( 'maxtime', 'Maximum amount of wall-clock time', false, true );
37 $this->addOption( 'type', 'Type of job to run', false, true );
38 $this->addOption( 'procs', 'Number of processes to use', false, true );
41 public function memoryLimit() {
42 if ( $this->hasOption( 'memory-limit' ) ) {
43 return parent
::memoryLimit();
45 // Don't eat all memory on the machine if we get a bad job.
49 public function execute() {
53 $this->error( "Unable to run jobs; the wiki is in read-only mode.", 1 ); // die
56 if ( $this->hasOption( 'procs' ) ) {
57 $procs = intval( $this->getOption( 'procs' ) );
58 if ( $procs < 1 ||
$procs > 1000 ) {
59 $this->error( "Invalid argument to --procs", true );
60 } elseif ( $procs != 1 ) {
61 $fc = new ForkController( $procs );
62 if ( $fc->start() != 'child' ) {
67 $maxJobs = $this->getOption( 'maxjobs', false );
68 $maxTime = $this->getOption( 'maxtime', false );
70 $type = $this->getOption( 'type', false );
71 $wgTitle = Title
::newFromText( 'RunJobs.php' );
72 $jobsRun = 0; // counter
74 $group = JobQueueGroup
::singleton();
75 // Handle any required periodic queue maintenance
76 $count = $group->executeReadyPeriodicTasks();
78 $this->runJobsLog( "Executed $count periodic queue task(s)." );
81 $flags = JobQueueGroup
::USE_CACHE | JobQueueGroup
::USE_PRIORITY
;
82 $lastTime = time(); // time since last slave check
84 $job = ( $type === false )
85 ?
$group->pop( JobQueueGroup
::TYPE_DEFAULT
, $flags )
86 : $group->pop( $type ); // job from a single queue
87 if ( $job ) { // found a job
89 $this->runJobsLog( $job->toString() . " STARTING" );
91 // Set timer to stop the job if too much CPU time is used
92 set_time_limit( $maxTime ?
: 0 );
94 wfProfileIn( __METHOD__
. '-' . get_class( $job ) );
95 $t = microtime( true );
97 $status = $job->run();
98 $error = $job->getLastError();
99 } catch ( MWException
$e ) {
101 $error = get_class( $e ) . ': ' . $e->getMessage();
102 $e->report(); // write error to STDERR and the log
104 $timeMs = intval( ( microtime( true ) - $t ) * 1000 );
105 wfProfileOut( __METHOD__
. '-' . get_class( $job ) );
109 // Mark the job as done on success or when the job cannot be retried
110 if ( $status !== false ||
!$job->allowRetries() ) {
111 $group->ack( $job ); // done
114 if ( $status === false ) {
115 $this->runJobsLog( $job->toString() . " t=$timeMs error={$error}" );
117 $this->runJobsLog( $job->toString() . " t=$timeMs good" );
120 // Break out if we hit the job count or wall time limits...
121 if ( $maxJobs && $jobsRun >= $maxJobs ) {
123 } elseif ( $maxTime && ( time() - $startTime ) > $maxTime ) {
127 // Don't let any of the main DB slaves get backed up
128 $timePassed = time() - $lastTime;
129 if ( $timePassed >= 5 ||
$timePassed < 0 ) {
133 // Don't let any queue slaves/backups fall behind
134 if ( $jobsRun > 0 && ( $jobsRun %
100 ) == 0 ) {
135 $group->waitForBackups();
138 // Bail if near-OOM instead of in a job
139 $this->assertMemoryOK();
141 } while ( $job ); // stop when there are no jobs
145 * Make sure that this script is not too close to the memory usage limit
146 * @throws MWException
148 private function assertMemoryOK() {
149 static $maxBytes = null;
150 if ( $maxBytes === null ) {
152 if ( preg_match( '!^(\d+)(k|m|g|)$!i', ini_get( 'memory_limit' ), $m ) ) {
153 list( , $num, $unit ) = $m;
154 $conv = array( 'g' => 1024 * 1024 * 1024, 'm' => 1024 * 1024, 'k' => 1024, '' => 1 );
155 $maxBytes = $num * $conv[strtolower( $unit )];
160 $usedBytes = memory_get_usage();
161 if ( $maxBytes && $usedBytes >= 0.95 * $maxBytes ) {
162 throw new MWException( "Detected excessive memory usage ($usedBytes/$maxBytes)." );
167 * Log the job message
168 * @param $msg String The message to log
170 private function runJobsLog( $msg ) {
171 $this->output( wfTimestamp( TS_DB
) . " $msg\n" );
172 wfDebugLog( 'runJobs', $msg );
176 $maintClass = "RunJobs";
177 require_once RUN_MAINTENANCE_IF_MAIN
;