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