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( 'type', 'Type of job to run', false, true );
00035 $this->addOption( 'procs', 'Number of processes to use', false, true );
00036 }
00037
00038 public function memoryLimit() {
00039
00040 return "150M";
00041 }
00042
00043 public function execute() {
00044 global $wgTitle;
00045 if ( $this->hasOption( 'procs' ) ) {
00046 $procs = intval( $this->getOption('procs') );
00047 if ( $procs < 1 || $procs > 1000 ) {
00048 $this->error( "Invalid argument to --procs", true );
00049 }
00050 $fc = new ForkController( $procs );
00051 if ( $fc->start( $procs ) != 'child' ) {
00052 exit( 0 );
00053 }
00054 }
00055 $maxJobs = $this->getOption( 'maxjobs', 10000 );
00056 $type = $this->getOption( 'type', false );
00057 $wgTitle = Title::newFromText( 'RunJobs.php' );
00058 $dbw = wfGetDB( DB_MASTER );
00059 $n = 0;
00060 $conds = '';
00061 if ($type !== false)
00062 $conds = "job_cmd = " . $dbw->addQuotes($type);
00063
00064 while ( $dbw->selectField( 'job', 'job_id', $conds, 'runJobs.php' ) ) {
00065 $offset=0;
00066 for (;;) {
00067 $job = ($type == false) ?
00068 Job::pop($offset)
00069 : Job::pop_type($type);
00070
00071 if ($job == false)
00072 break;
00073
00074 wfWaitForSlaves( 5 );
00075 $t = microtime( true );
00076 $offset=$job->id;
00077 $status = $job->run();
00078 $t = microtime( true ) - $t;
00079 $timeMs = intval( $t * 1000 );
00080 if ( !$status ) {
00081 $this->runJobsLog( $job->toString() . " t=$timeMs error={$job->error}" );
00082 } else {
00083 $this->runJobsLog( $job->toString() . " t=$timeMs good" );
00084 }
00085 if ( $maxJobs && ++$n > $maxJobs ) {
00086 break 2;
00087 }
00088 }
00089 }
00090 }
00091
00096 private function runJobsLog( $msg ) {
00097 $this->output( wfTimestamp( TS_DB ) . " $msg\n" );
00098 wfDebugLog( 'runJobs', $msg );
00099 }
00100 }
00101
00102 $maintClass = "RunJobs";
00103 require_once( DO_MAINTENANCE );