6 * --maxjobs <num> (default 10000)
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
25 * @ingroup Maintenance
28 require_once( __DIR__
. '/Maintenance.php' );
31 * Maintenance script that runs pending jobs.
33 * @ingroup Maintenance
35 class RunJobs
extends Maintenance
{
36 public function __construct() {
37 parent
::__construct();
38 $this->mDescription
= "Run pending jobs";
39 $this->addOption( 'maxjobs', 'Maximum number of jobs to run', false, true );
40 $this->addOption( 'maxtime', 'Maximum amount of wall-clock time', false, true );
41 $this->addOption( 'type', 'Type of job to run', false, true );
42 $this->addOption( 'procs', 'Number of processes to use', false, true );
45 public function memoryLimit() {
46 if ( $this->hasOption( 'memory-limit' ) ) {
47 return parent
::memoryLimit();
49 // Don't eat all memory on the machine if we get a bad job.
53 public function execute() {
57 $this->error( "Unable to run jobs; the wiki is in read-only mode.", 1 ); // die
60 if ( $this->hasOption( 'procs' ) ) {
61 $procs = intval( $this->getOption( 'procs' ) );
62 if ( $procs < 1 ||
$procs > 1000 ) {
63 $this->error( "Invalid argument to --procs", true );
65 $fc = new ForkController( $procs );
66 if ( $fc->start() != 'child' ) {
70 $maxJobs = $this->getOption( 'maxjobs', false );
71 $maxTime = $this->getOption( 'maxtime', false );
73 $type = $this->getOption( 'type', false );
74 $wgTitle = Title
::newFromText( 'RunJobs.php' );
75 $dbw = wfGetDB( DB_MASTER
);
76 $jobsRun = 0; // counter
78 $group = JobQueueGroup
::singleton();
79 // Handle any required periodic queue maintenance
80 $count = $group->executeReadyPeriodicTasks();
82 $this->runJobsLog( "Executed $count periodic queue task(s)." );
85 $flags = JobQueueGroup
::USE_CACHE | JobQueueGroup
::USE_PRIORITY
;
88 $job = ( $type === false )
89 ?
$group->pop( JobQueueGroup
::TYPE_DEFAULT
, $flags )
90 : $group->pop( $type ); // job from a single queue
91 if ( $job ) { // found a job
93 $this->runJobsLog( $job->toString() . " STARTING" );
96 $t = microtime( true );
98 $status = $job->run();
99 $error = $job->getLastError();
100 } catch ( MWException
$e ) {
102 $error = get_class( $e ) . ': ' . $e->getMessage();
104 $timeMs = intval( ( microtime( true ) - $t ) * 1000 );
106 // Mark the job as done on success or when the job cannot be retried
107 if ( $status !== false ||
!$job->allowRetries() ) {
108 $group->ack( $job ); // done
111 if ( $status === false ) {
112 $this->runJobsLog( $job->toString() . " t=$timeMs error={$error}" );
114 $this->runJobsLog( $job->toString() . " t=$timeMs good" );
117 // Break out if we hit the job count or wall time limits...
118 if ( $maxJobs && $jobsRun >= $maxJobs ) {
120 } elseif ( $maxTime && ( time() - $startTime ) > $maxTime ) {
124 // Don't let any of the main DB slaves get backed up
125 $timePassed = time() - $lastTime;
126 if ( $timePassed >= 5 ||
$timePassed < 0 ) {
129 // Don't let any queue slaves/backups fall behind
130 if ( $jobsRun > 0 && ( $jobsRun %
100 ) == 0 ) {
131 $group->waitForBackups();
134 } while ( $job ); // stop when there are no jobs
138 * Log the job message
139 * @param $msg String The message to log
141 private function runJobsLog( $msg ) {
142 $this->output( wfTimestamp( TS_DB
) . " $msg\n" );
143 wfDebugLog( 'runJobs', $msg );
147 $maintClass = "RunJobs";
148 require_once( RUN_MAINTENANCE_IF_MAIN
);