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() {
55 if ( $this->hasOption( 'procs' ) ) {
56 $procs = intval( $this->getOption( 'procs' ) );
57 if ( $procs < 1 ||
$procs > 1000 ) {
58 $this->error( "Invalid argument to --procs", true );
60 $fc = new ForkController( $procs );
61 if ( $fc->start() != 'child' ) {
65 $maxJobs = $this->getOption( 'maxjobs', false );
66 $maxTime = $this->getOption( 'maxtime', false );
68 $type = $this->getOption( 'type', false );
69 $wgTitle = Title
::newFromText( 'RunJobs.php' );
70 $dbw = wfGetDB( DB_MASTER
);
73 if ( $type === false ) {
74 $conds = Job
::defaultQueueConditions( );
76 $conds = array( 'job_cmd' => $type );
79 while ( $dbw->selectField( 'job', 'job_id', $conds, 'runJobs.php' ) ) {
82 $job = !$type ? Job
::pop( $offset ) : Job
::pop_type( $type );
89 $t = microtime( true );
91 $this->runJobsLog( $job->toString() . " STARTING" );
92 $status = $job->run();
93 $t = microtime( true ) - $t;
94 $timeMs = intval( $t * 1000 );
96 $this->runJobsLog( $job->toString() . " t=$timeMs error={$job->error}" );
98 $this->runJobsLog( $job->toString() . " t=$timeMs good" );
101 if ( $maxJobs && ++
$n > $maxJobs ) {
104 if ( $maxTime && time() - $startTime > $maxTime ) {
112 * Log the job message
113 * @param $msg String The message to log
115 private function runJobsLog( $msg ) {
116 $this->output( wfTimestamp( TS_DB
) . " $msg\n" );
117 wfDebugLog( 'runJobs', $msg );
121 $maintClass = "RunJobs";
122 require_once( RUN_MAINTENANCE_IF_MAIN
);