MediaWiki  1.32.0
runJobs.php
Go to the documentation of this file.
1 <?php
24 require_once __DIR__ . '/Maintenance.php';
25 
27 
28 // So extensions (and other code) can check whether they're running in job mode
29 define( 'MEDIAWIKI_JOB_RUNNER', true );
30 
36 class RunJobs extends Maintenance {
37  public function __construct() {
38  parent::__construct();
39  $this->addDescription( 'Run pending jobs' );
40  $this->addOption( 'maxjobs', 'Maximum number of jobs to run', false, true );
41  $this->addOption( 'maxtime', 'Maximum amount of wall-clock time', false, true );
42  $this->addOption( 'type', 'Type of job to run', false, true );
43  $this->addOption( 'procs', 'Number of processes to use', false, true );
44  $this->addOption( 'nothrottle', 'Ignore job throttling configuration', false, false );
45  $this->addOption( 'result', 'Set to "json" to print only a JSON response', false, true );
46  $this->addOption( 'wait', 'Wait for new jobs instead of exiting', false, false );
47  }
48 
49  public function memoryLimit() {
50  if ( $this->hasOption( 'memory-limit' ) ) {
51  return parent::memoryLimit();
52  }
53 
54  // Don't eat all memory on the machine if we get a bad job.
55  return "150M";
56  }
57 
58  public function execute() {
59  if ( $this->hasOption( 'procs' ) ) {
60  $procs = intval( $this->getOption( 'procs' ) );
61  if ( $procs < 1 || $procs > 1000 ) {
62  $this->fatalError( "Invalid argument to --procs" );
63  } elseif ( $procs != 1 ) {
64  $fc = new ForkController( $procs );
65  if ( $fc->start() != 'child' ) {
66  exit( 0 );
67  }
68  }
69  }
70 
71  $outputJSON = ( $this->getOption( 'result' ) === 'json' );
72  $wait = $this->hasOption( 'wait' );
73 
74  $runner = new JobRunner( LoggerFactory::getInstance( 'runJobs' ) );
75  if ( !$outputJSON ) {
76  $runner->setDebugHandler( [ $this, 'debugInternal' ] );
77  }
78 
79  $type = $this->getOption( 'type', false );
80  $maxJobs = $this->getOption( 'maxjobs', false );
81  $maxTime = $this->getOption( 'maxtime', false );
82  $throttle = !$this->hasOption( 'nothrottle' );
83 
84  while ( true ) {
85  $response = $runner->run( [
86  'type' => $type,
87  'maxJobs' => $maxJobs,
88  'maxTime' => $maxTime,
89  'throttle' => $throttle,
90  ] );
91 
92  if ( $outputJSON ) {
93  $this->output( FormatJson::encode( $response, true ) );
94  }
95 
96  if (
97  !$wait ||
98  $response['reached'] === 'time-limit' ||
99  $response['reached'] === 'job-limit' ||
100  $response['reached'] === 'memory-limit'
101  ) {
102  break;
103  }
104 
105  if ( $maxJobs !== false ) {
106  $maxJobs -= count( $response['jobs'] );
107  }
108 
109  sleep( 1 );
110  }
111  }
112 
116  public function debugInternal( $s ) {
117  $this->output( $s );
118  }
119 }
120 
122 require_once RUN_MAINTENANCE_IF_MAIN;
RunJobs
Maintenance script that runs pending jobs.
Definition: runJobs.php:36
RunJobs\execute
execute()
Do the actual work.
Definition: runJobs.php:58
ForkController
Class for managing forking command line scripts.
Definition: ForkController.php:33
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:465
RunJobs\memoryLimit
memoryLimit()
Normally we disable the memory_limit when running admin scripts.
Definition: runJobs.php:49
captcha-old.count
count
Definition: captcha-old.py:249
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:317
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
RunJobs\debugInternal
debugInternal( $s)
Definition: runJobs.php:116
$s
$s
Definition: mergeMessageFileList.php:187
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
FormatJson\encode
static encode( $value, $pretty=false, $escaping=0)
Returns the JSON representation of a value.
Definition: FormatJson.php:115
RunJobs\__construct
__construct()
Default constructor.
Definition: runJobs.php:37
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:236
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
$response
this hook is for auditing only $response
Definition: hooks.txt:813
$maintClass
$maintClass
Definition: runJobs.php:121
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:271
LoggerFactory
MediaWiki Logger LoggerFactory implements a PSR[0] compatible message logging system Named Psr Log LoggerInterface instances can be obtained from the MediaWiki Logger LoggerFactory::getInstance() static method. MediaWiki\Logger\LoggerFactory expects a class implementing the MediaWiki\Logger\Spi interface to act as a factory for new Psr\Log\LoggerInterface instances. The "Spi" in MediaWiki\Logger\Spi stands for "service provider interface". An SPI is an API intended to be implemented or extended by a third party. This software design pattern is intended to enable framework extension and replaceable components. It is specifically used in the MediaWiki\Logger\LoggerFactory service to allow alternate PSR-3 logging implementations to be easily integrated with MediaWiki. The service provider interface allows the backend logging library to be implemented in multiple ways. The $wgMWLoggerDefaultSpi global provides the classname of the default MediaWiki\Logger\Spi implementation to be loaded at runtime. This can either be the name of a class implementing the MediaWiki\Logger\Spi with a zero argument const ructor or a callable that will return an MediaWiki\Logger\Spi instance. Alternately the MediaWiki\Logger\LoggerFactory MediaWiki Logger LoggerFactory
Definition: logger.txt:5
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:414
$wait
$wait
Definition: styleTest.css.php:50
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option exists.
Definition: Maintenance.php:257
JobRunner
Job queue runner utility methods.
Definition: JobRunner.php:39
$type
$type
Definition: testCompression.php:48