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