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