Merge "Add $wgRateLimits types ip-all and subnet-all"
[lhc/web/wiklou.git] / maintenance / runJobs.php
index 3e6fa52..86cade2 100644 (file)
@@ -23,6 +23,8 @@
 
 require_once __DIR__ . '/Maintenance.php';
 
+use MediaWiki\Logger\LoggerFactory;
+
 /**
  * Maintenance script that runs pending jobs.
  *
@@ -31,12 +33,13 @@ require_once __DIR__ . '/Maintenance.php';
 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 );
        }
 
        public function memoryLimit() {
@@ -49,9 +52,7 @@ class RunJobs extends Maintenance {
        }
 
        public function execute() {
-               if ( wfReadOnly() ) {
-                       $this->error( "Unable to run jobs; the wiki is in read-only mode.", 1 ); // die
-               }
+               global $wgCommandLineMode;
 
                if ( $this->hasOption( 'procs' ) ) {
                        $procs = intval( $this->getOption( 'procs' ) );
@@ -65,14 +66,29 @@ class RunJobs extends Maintenance {
                        }
                }
 
-               $runner = new JobRunner();
-               $runner->setDebugHandler( array( $this, 'debugInternal' ) );
-               $runner->run( array(
+               $outputJSON = ( $this->getOption( 'result' ) === 'json' );
+
+               // Enable DBO_TRX for atomicity; JobRunner manages transactions
+               // and works well in web server mode already (@TODO: this is a hack)
+               $wgCommandLineMode = false;
+
+               $runner = new JobRunner( LoggerFactory::getInstance( 'runJobs' ) );
+               if ( !$outputJSON ) {
+                       $runner->setDebugHandler( array( $this, 'debugInternal' ) );
+               }
+
+               $response = $runner->run( array(
                        'type'     => $this->getOption( 'type', false ),
                        'maxJobs'  => $this->getOption( 'maxjobs', false ),
                        'maxTime'  => $this->getOption( 'maxtime', false ),
                        'throttle' => $this->hasOption( 'nothrottle' ) ? false : true,
                ) );
+
+               if ( $outputJSON ) {
+                       $this->output( FormatJson::encode( $response, true ) );
+               }
+
+               $wgCommandLineMode = true;
        }
 
        /**