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