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