]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - maintenance/runJobs.php
MediaWiki 1.16.0
[autoinstallsdev/mediawiki.git] / maintenance / runJobs.php
1 <?php
2 /**
3  * This script starts pending jobs.
4  *
5  * Usage:
6  *  --maxjobs <num> (default 10000)
7  *  --type <job_cmd>
8  *
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.
13  *
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.
18  *
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
23  *
24  * @ingroup Maintenance
25  */
26
27 require_once( dirname(__FILE__) . '/Maintenance.php' );
28
29 class RunJobs extends Maintenance {
30         public function __construct() {
31                 parent::__construct();
32                 $this->mDescription = "Run pending jobs";
33                 $this->addOption( 'maxjobs', 'Maximum number of jobs to run', false, true );
34                 $this->addOption( 'type', 'Type of job to run', false, true );
35                 $this->addOption( 'procs', 'Number of processes to use', false, true );
36         }
37         
38         public function memoryLimit() {
39                 // Don't eat all memory on the machine if we get a bad job.
40                 return "150M";
41         }
42
43         public function execute() {
44                 global $wgTitle;
45                 if ( $this->hasOption( 'procs' ) ) {
46                         $procs = intval( $this->getOption('procs') );
47                         if ( $procs < 1 || $procs > 1000 ) {
48                                 $this->error( "Invalid argument to --procs", true );
49                         }
50                         $fc = new ForkController( $procs );
51                         if ( $fc->start( $procs ) != 'child' ) {
52                                 exit( 0 );
53                         }
54                 }
55                 $maxJobs = $this->getOption( 'maxjobs', 10000 );
56                 $type = $this->getOption( 'type', false );
57                 $wgTitle = Title::newFromText( 'RunJobs.php' );
58                 $dbw = wfGetDB( DB_MASTER );
59                 $n = 0;
60                 $conds = '';
61                 if ($type !== false)
62                         $conds = "job_cmd = " . $dbw->addQuotes($type);
63
64                 while ( $dbw->selectField( 'job', 'job_id', $conds, 'runJobs.php' ) ) {
65                         $offset=0;
66                         for (;;) {
67                                 $job = ($type == false) ?
68                                                 Job::pop($offset)
69                                                 : Job::pop_type($type);
70         
71                                 if ($job == false)
72                                         break;
73         
74                                 wfWaitForSlaves( 5 );
75                                 $t = microtime( true );
76                                 $offset=$job->id;
77                                 $status = $job->run();
78                                 $t = microtime( true ) - $t;
79                                 $timeMs = intval( $t * 1000 );
80                                 if ( !$status ) {
81                                         $this->runJobsLog( $job->toString() . " t=$timeMs error={$job->error}" );
82                                 } else {
83                                         $this->runJobsLog( $job->toString() . " t=$timeMs good" );
84                                 }
85                                 if ( $maxJobs && ++$n > $maxJobs ) {
86                                         break 2;
87                                 }
88                         }
89                 }
90         }
91
92         /**
93          * Log the job message
94          * @param $msg String The message to log
95          */
96         private function runJobsLog( $msg ) {
97                 $this->output( wfTimestamp( TS_DB ) . " $msg\n" );
98                 wfDebugLog( 'runJobs', $msg );
99         }
100 }
101
102 $maintClass = "RunJobs";
103 require_once( DO_MAINTENANCE );