MediaWiki  master
runJobs.php
Go to the documentation of this file.
1 <?php
24 require_once __DIR__ . '/Maintenance.php';
25 
27 
33 class RunJobs extends Maintenance {
34  public function __construct() {
35  parent::__construct();
36  $this->addDescription( 'Run pending jobs' );
37  $this->addOption( 'maxjobs', 'Maximum number of jobs to run', false, true );
38  $this->addOption( 'maxtime', 'Maximum amount of wall-clock time', false, true );
39  $this->addOption( 'type', 'Type of job to run', false, true );
40  $this->addOption( 'procs', 'Number of processes to use', false, true );
41  $this->addOption( 'nothrottle', 'Ignore job throttling configuration', false, false );
42  $this->addOption( 'result', 'Set to "json" to print only a JSON response', false, true );
43  $this->addOption( 'wait', 'Wait for new jobs instead of exiting', false, false );
44  }
45 
46  public function finalSetup() {
47  // So extensions (and other code) can check whether they're running in job mode.
48  // This is not defined if this script is included from installer/updater or phpunit.
49  define( 'MEDIAWIKI_JOB_RUNNER', true );
50  parent::finalSetup();
51  }
52 
53  public function memoryLimit() {
54  if ( $this->hasOption( 'memory-limit' ) ) {
55  return parent::memoryLimit();
56  }
57 
58  // Don't eat all memory on the machine if we get a bad job.
59  return "150M";
60  }
61 
62  public function execute() {
63  if ( $this->hasOption( 'procs' ) ) {
64  $procs = intval( $this->getOption( 'procs' ) );
65  if ( $procs < 1 || $procs > 1000 ) {
66  $this->fatalError( "Invalid argument to --procs" );
67  } elseif ( $procs != 1 ) {
68  try {
69  $fc = new ForkController( $procs );
70  if ( $fc->start() != 'child' ) {
71  return;
72  }
73  } catch ( MWException $e ) {
74  $this->fatalError( $e->getMessage() );
75  }
76  }
77  }
78 
79  $outputJSON = ( $this->getOption( 'result' ) === 'json' );
80  $wait = $this->hasOption( 'wait' );
81 
82  $runner = MediaWikiServices::getInstance()->getJobRunner();
83  if ( !$outputJSON ) {
84  $runner->setDebugHandler( [ $this, 'debugInternal' ] );
85  }
86 
87  $type = $this->getOption( 'type', false );
88  $maxJobs = $this->getOption( 'maxjobs', false );
89  $maxTime = $this->getOption( 'maxtime', false );
90  $throttle = !$this->hasOption( 'nothrottle' );
91 
92  while ( true ) {
93  $response = $runner->run( [
94  'type' => $type,
95  'maxJobs' => $maxJobs,
96  'maxTime' => $maxTime,
97  'throttle' => $throttle,
98  ] );
99 
100  if ( $outputJSON ) {
101  $this->output( FormatJson::encode( $response, true ) );
102  }
103 
104  if (
105  !$wait ||
106  $response['reached'] === 'time-limit' ||
107  $response['reached'] === 'job-limit' ||
108  $response['reached'] === 'memory-limit'
109  ) {
110  // If job queue is empty, output it
111  if ( !$outputJSON && $response['jobs'] === [] ) {
112  $this->output( "Job queue is empty.\n" );
113  }
114  break;
115  }
116 
117  if ( $maxJobs !== false ) {
118  $maxJobs -= count( $response['jobs'] );
119  }
120 
121  sleep( 1 );
122  }
123  }
124 
128  public function debugInternal( $s ) {
129  $this->output( $s );
130  }
131 }
132 
133 $maintClass = RunJobs::class;
134 require_once RUN_MAINTENANCE_IF_MAIN;
RunJobs
Maintenance script that runs pending jobs.
Definition: runJobs.php:33
RunJobs\execute
execute()
Do the actual work.
Definition: runJobs.php:62
ForkController
Class for managing forking command line scripts.
Definition: ForkController.php:33
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:191
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:489
RunJobs\memoryLimit
memoryLimit()
Normally we disable the memory_limit when running admin scripts.
Definition: runJobs.php:53
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:329
RunJobs\debugInternal
debugInternal( $s)
Definition: runJobs.php:128
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:59
RunJobs\finalSetup
finalSetup()
Handle some last-minute setup here.
Definition: runJobs.php:46
FormatJson\encode
static encode( $value, $pretty=false, $escaping=0)
Returns the JSON representation of a value.
Definition: FormatJson.php:96
MWException
MediaWiki exception.
Definition: MWException.php:29
RunJobs\__construct
__construct()
Default constructor.
Definition: runJobs.php:34
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:249
$s
foreach( $mmfl['setupFiles'] as $fileName) if( $queue) if(empty( $mmfl['quiet'])) $s
Definition: mergeMessageFileList.php:206
$maintClass
$maintClass
Definition: runJobs.php:133
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:286
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:435
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option was set.
Definition: Maintenance.php:271
$type
$type
Definition: testCompression.php:52