]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blobdiff - maintenance/runJobs.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / maintenance / runJobs.php
index 79ea7bfe3e2c1077a71d1200456779543739d2a2..2e011fecde54b2f356e9fdb1aed7b43f2192a5c8 100644 (file)
@@ -1,10 +1,6 @@
 <?php
 /**
- * This script starts pending jobs.
- *
- * Usage:
- *  --maxjobs <num> (default 10000)
- *  --type <job_cmd>
+ * Run pending jobs.
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  * http://www.gnu.org/copyleft/gpl.html
  *
+ * @file
  * @ingroup Maintenance
  */
 
-require_once( dirname( __FILE__ ) . '/Maintenance.php' );
+require_once __DIR__ . '/Maintenance.php';
+
+use MediaWiki\Logger\LoggerFactory;
 
+/**
+ * Maintenance script that runs pending jobs.
+ *
+ * @ingroup Maintenance
+ */
 class RunJobs extends Maintenance {
        public function __construct() {
                parent::__construct();
-               $this->mDescription = "Run pending jobs";
+               $this->addDescription( 'Run pending jobs' );
                $this->addOption( 'maxjobs', 'Maximum number of jobs to run', false, true );
+               $this->addOption( 'maxtime', 'Maximum amount of wall-clock time', false, true );
                $this->addOption( 'type', 'Type of job to run', false, true );
                $this->addOption( 'procs', 'Number of processes to use', false, true );
+               $this->addOption( 'nothrottle', 'Ignore job throttling configuration', false, false );
+               $this->addOption( 'result', 'Set to JSON to print only a JSON response', false, true );
+               $this->addOption( 'wait', 'Wait for new jobs instead of exiting', false, false );
        }
 
        public function memoryLimit() {
+               if ( $this->hasOption( 'memory-limit' ) ) {
+                       return parent::memoryLimit();
+               }
+
                // Don't eat all memory on the machine if we get a bad job.
                return "150M";
        }
 
        public function execute() {
-               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' ) {
-                               exit( 0 );
+                       } elseif ( $procs != 1 ) {
+                               $fc = new ForkController( $procs );
+                               if ( $fc->start() != 'child' ) {
+                                       exit( 0 );
+                               }
                        }
                }
-               $maxJobs = $this->getOption( 'maxjobs', 10000 );
+
+               $outputJSON = ( $this->getOption( 'result' ) === 'json' );
+               $wait = $this->hasOption( 'wait' );
+
+               $runner = new JobRunner( LoggerFactory::getInstance( 'runJobs' ) );
+               if ( !$outputJSON ) {
+                       $runner->setDebugHandler( [ $this, 'debugInternal' ] );
+               }
+
                $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;
-
-                               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;
-                               }
+               $maxJobs = $this->getOption( 'maxjobs', false );
+               $maxTime = $this->getOption( 'maxtime', false );
+               $throttle = !$this->hasOption( 'nothrottle' );
+
+               while ( true ) {
+                       $response = $runner->run( [
+                               'type'     => $type,
+                               'maxJobs'  => $maxJobs,
+                               'maxTime'  => $maxTime,
+                               'throttle' => $throttle,
+                       ] );
+
+                       if ( $outputJSON ) {
+                               $this->output( FormatJson::encode( $response, true ) );
                        }
+
+                       if (
+                               !$wait ||
+                               $response['reached'] === 'time-limit' ||
+                               $response['reached'] === 'job-limit' ||
+                               $response['reached'] === 'memory-limit'
+                       ) {
+                               break;
+                       }
+
+                       if ( $maxJobs !== false ) {
+                               $maxJobs -= count( $response['jobs'] );
+                       }
+
+                       sleep( 1 );
                }
        }
 
        /**
-        * Log the job message
-        * @param $msg String The message to log
+        * @param string $s
         */
-       private function runJobsLog( $msg ) {
-               $this->output( wfTimestamp( TS_DB ) . " $msg\n" );
-               wfDebugLog( 'runJobs', $msg );
+       public function debugInternal( $s ) {
+               $this->output( $s );
        }
 }
 
 $maintClass = "RunJobs";
-require_once( RUN_MAINTENANCE_IF_MAIN );
+require_once RUN_MAINTENANCE_IF_MAIN;