MediaWiki  REL1_19
runJobs.php
Go to the documentation of this file.
00001 <?php
00027 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
00028 
00029 class RunJobs extends Maintenance {
00030         public function __construct() {
00031                 parent::__construct();
00032                 $this->mDescription = "Run pending jobs";
00033                 $this->addOption( 'maxjobs', 'Maximum number of jobs to run', false, true );
00034                 $this->addOption( 'maxtime', 'Maximum amount of wall-clock time', false, true );
00035                 $this->addOption( 'type', 'Type of job to run', false, true );
00036                 $this->addOption( 'procs', 'Number of processes to use', false, true );
00037         }
00038 
00039         public function memoryLimit() {
00040                 // Don't eat all memory on the machine if we get a bad job.
00041                 return "150M";
00042         }
00043 
00044         public function execute() {
00045                 global $wgTitle;
00046                 if ( $this->hasOption( 'procs' ) ) {
00047                         $procs = intval( $this->getOption( 'procs' ) );
00048                         if ( $procs < 1 || $procs > 1000 ) {
00049                                 $this->error( "Invalid argument to --procs", true );
00050                         }
00051                         $fc = new ForkController( $procs );
00052                         if ( $fc->start() != 'child' ) {
00053                                 exit( 0 );
00054                         }
00055                 }
00056                 $maxJobs = $this->getOption( 'maxjobs', false );
00057                 $maxTime = $this->getOption( 'maxtime', false );
00058                 $startTime = time();
00059                 $type = $this->getOption( 'type', false );
00060                 $wgTitle = Title::newFromText( 'RunJobs.php' );
00061                 $dbw = wfGetDB( DB_MASTER );
00062                 $n = 0;
00063                 $conds = '';
00064                 if ( $type !== false ) {
00065                         $conds = "job_cmd = " . $dbw->addQuotes( $type );
00066                 }
00067 
00068                 while ( $dbw->selectField( 'job', 'job_id', $conds, 'runJobs.php' ) ) {
00069                         $offset = 0;
00070                         for ( ; ; ) {
00071                                 $job = !$type ? Job::pop( $offset ) : Job::pop_type( $type );
00072 
00073                                 if ( !$job ) {
00074                                         break;
00075                                 }
00076 
00077                                 wfWaitForSlaves();
00078                                 $t = microtime( true );
00079                                 $offset = $job->id;
00080                                 $status = $job->run();
00081                                 $t = microtime( true ) - $t;
00082                                 $timeMs = intval( $t * 1000 );
00083                                 if ( !$status ) {
00084                                         $this->runJobsLog( $job->toString() . " t=$timeMs error={$job->error}" );
00085                                 } else {
00086                                         $this->runJobsLog( $job->toString() . " t=$timeMs good" );
00087                                 }
00088 
00089                                 if ( $maxJobs && ++$n > $maxJobs ) {
00090                                         break 2;
00091                                 }
00092                                 if ( $maxTime && time() - $startTime > $maxTime ) {
00093                                         break 2;
00094                                 }
00095                         }
00096                 }
00097         }
00098 
00103         private function runJobsLog( $msg ) {
00104                 $this->output( wfTimestamp( TS_DB ) . " $msg\n" );
00105                 wfDebugLog( 'runJobs', $msg );
00106         }
00107 }
00108 
00109 $maintClass = "RunJobs";
00110 require_once( RUN_MAINTENANCE_IF_MAIN );