X-Git-Url: http://git.cyclocoop.org/?a=blobdiff_plain;f=maintenance%2FrunJobs.php;h=d6d2538cafbd911f00752543660e2924130c6010;hb=e5c7ce3aa4e079e2f8f630e22f77bbe238747bb0;hp=6e8e09fa0234a977b3bd162b8ba8a8a2f086e3ff;hpb=6f53fc41af6d71f67cd5434460afcd8ad07f31bc;p=lhc%2Fweb%2Fwiklou.git diff --git a/maintenance/runJobs.php b/maintenance/runJobs.php index 6e8e09fa02..d6d2538caf 100644 --- a/maintenance/runJobs.php +++ b/maintenance/runJobs.php @@ -1,48 +1,125 @@ mDescription = "Run pending jobs"; + $this->addOption( 'maxjobs', 'Maximum number of jobs to run', false, true ); + $this->addOption( 'type', 'Type of job to run', false, true ); + $this->addOption( 'procs', 'Number of processes to use', false, true ); + $this->addOption( 'exclusive', 'Run only one exclusive runJobs script at a time. Timeout is 1800 seconds. Useful for cron scripts.', false ); + } + + public function memoryLimit() { + // Don't eat all memory on the machine if we get a bad job. + return "150M"; + } + + public function execute() { + if ( $this->lock() === false ) { + exit( 0 ); + } + + global $wgTitle; + if ( $this->hasOption( 'procs' ) ) { + $procs = intval( $this->getOption( 'procs' ) ); + if ( $procs < 1 || $procs > 1000 ) { + $this->error( "Invalid argument to --procs", true ); + } + $fc = new ForkController( $procs ); + if ( $fc->start( $procs ) != 'child' ) { + $this->unlock(); + exit( 0 ); + } + } + $maxJobs = $this->getOption( 'maxjobs', 10000 ); + $type = $this->getOption( 'type', false ); + $wgTitle = Title::newFromText( 'RunJobs.php' ); + $dbw = wfGetDB( DB_MASTER ); + $n = 0; + $conds = ''; + if ( $type !== false ) + $conds = "job_cmd = " . $dbw->addQuotes( $type ); + + while ( $dbw->selectField( 'job', 'job_id', $conds, 'runJobs.php' ) ) { + $offset = 0; + for ( ; ; ) { + $job = !$type ? Job::pop( $offset ) : Job::pop_type( $type ); + + if ( !$job ) + break; -$type = false; -if ( isset( $options['type'] ) ) - $type = $options['type']; - -$wgTitle = Title::newFromText( 'RunJobs.php' ); - -$dbw = wfGetDB( DB_MASTER ); -$n = 0; -$conds = array(); -if ($type !== false) - $conds = array('job_cmd' => $type); - -while ( $dbw->selectField( 'job', 'count(*)', $conds, 'runJobs.php' ) ) { - $offset=0; - for (;;) { - $job = ($type == false) ? - Job::pop($offset, $type) - : Job::pop_type($type); - - if ($job == false) - break; - - wfWaitForSlaves( 5 ); - print $job->id . " " . $job->toString() . "\n"; - $offset=$job->id; - if ( !$job->run() ) { - print "Error: {$job->error}\n"; + wfWaitForSlaves( 5 ); + $t = microtime( true ); + $offset = $job->id; + $status = $job->run(); + $t = microtime( true ) - $t; + $timeMs = intval( $t * 1000 ); + if ( !$status ) { + $this->runJobsLog( $job->toString() . " t=$timeMs error={$job->error}" ); + } else { + $this->runJobsLog( $job->toString() . " t=$timeMs good" ); + } + if ( $maxJobs && ++$n > $maxJobs ) { + break 2; + } + } } - if ( $maxJobs && ++$n > $maxJobs ) { - break 2; + if ( !$this->hasOption( 'procs' ) ) { + $this->unlock(); } } + + /** + * Log the job message + * @param $msg String The message to log + */ + private function runJobsLog( $msg ) { + $this->output( wfTimestamp( TS_DB ) . " $msg\n" ); + wfDebugLog( 'runJobs', $msg ); + } + + protected function lock() { + if ( $this->hasOption( 'exclusive' ) ) { + $cache = wfGetCache( CACHE_ANYTHING ); + $running = $cache->get( wfMemcKey( 'runjobs' ) ); + if ( $running ) { + return false; + } else { + $cache->set( wfMemcKey( 'runjobs' ), '1', 1800 ); + return true; + } + } + return true; + } + + protected function unlock() { + wfGetCache( CACHE_ANYTHING )->delete( wfMemcKey( 'runjobs' ) ); + } + } -?> + +$maintClass = "RunJobs"; +require_once( DO_MAINTENANCE );