MediaWiki REL1_37
runJobs.php
Go to the documentation of this file.
1<?php
24require_once __DIR__ . '/Maintenance.php';
25
27
33class RunJobs extends Maintenance {
34 public function __construct() {
35 parent::__construct();
36 $this->addDescription( 'Run pending jobs' );
37 $this->addOption( 'maxjobs', 'Maximum number of jobs to run', false, true );
38 $this->addOption( 'maxtime', 'Maximum amount of wall-clock time', false, true );
39 $this->addOption( 'type', 'Type of job to run', false, true );
40 $this->addOption( 'procs', 'Number of processes to use', false, true );
41 $this->addOption( 'nothrottle', 'Ignore job throttling configuration', false, false );
42 $this->addOption( 'result', 'Set to "json" to print only a JSON response', false, true );
43 $this->addOption( 'wait', 'Wait for new jobs instead of exiting', false, false );
44 }
45
46 public function finalSetup() {
47 // So extensions (and other code) can check whether they're running in job mode.
48 // This is not defined if this script is included from installer/updater or phpunit.
49 define( 'MEDIAWIKI_JOB_RUNNER', true );
50 parent::finalSetup();
51 }
52
53 public function memoryLimit() {
54 if ( $this->hasOption( 'memory-limit' ) ) {
55 return parent::memoryLimit();
56 }
57
58 // Don't eat all memory on the machine if we get a bad job.
59 return "150M";
60 }
61
62 public function execute() {
63 if ( $this->hasOption( 'procs' ) ) {
64 $procs = intval( $this->getOption( 'procs' ) );
65 if ( $procs < 1 || $procs > 1000 ) {
66 $this->fatalError( "Invalid argument to --procs" );
67 } elseif ( $procs != 1 ) {
68 try {
69 $fc = new ForkController( $procs );
70 if ( $fc->start() != 'child' ) {
71 return;
72 }
73 } catch ( MWException $e ) {
74 $this->fatalError( $e->getMessage() );
75 }
76 }
77 }
78
79 $outputJSON = ( $this->getOption( 'result' ) === 'json' );
80 $wait = $this->hasOption( 'wait' );
81
82 $runner = MediaWikiServices::getInstance()->getJobRunner();
83 if ( !$outputJSON ) {
84 $runner->setDebugHandler( [ $this, 'debugInternal' ] );
85 }
86
87 $type = $this->getOption( 'type', false );
88 $maxJobs = $this->getOption( 'maxjobs', false );
89 $maxTime = $this->getOption( 'maxtime', false );
90 $throttle = !$this->hasOption( 'nothrottle' );
91
92 while ( true ) {
93 $response = $runner->run( [
94 'type' => $type,
95 'maxJobs' => $maxJobs,
96 'maxTime' => $maxTime,
97 'throttle' => $throttle,
98 ] );
99
100 if ( $outputJSON ) {
101 $this->output( FormatJson::encode( $response, true ) );
102 }
103
104 if (
105 !$wait ||
106 $response['reached'] === 'time-limit' ||
107 $response['reached'] === 'job-limit' ||
108 $response['reached'] === 'memory-limit'
109 ) {
110 // If job queue is empty, output it
111 if ( !$outputJSON && $response['jobs'] === [] ) {
112 $this->output( "Job queue is empty.\n" );
113 }
114 break;
115 }
116
117 if ( $maxJobs !== false ) {
118 $maxJobs -= count( $response['jobs'] );
119 }
120
121 sleep( 1 );
122 }
123 }
124
128 public function debugInternal( $s ) {
129 $this->output( $s );
130 }
131}
132
133$maintClass = RunJobs::class;
134require_once RUN_MAINTENANCE_IF_MAIN;
Class for managing forking command line scripts.
MediaWiki exception.
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.
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.
MediaWikiServices is the service locator for the application scope of MediaWiki.
Maintenance script that runs pending jobs.
Definition runJobs.php:33
execute()
Do the actual work.
Definition runJobs.php:62
debugInternal( $s)
Definition runJobs.php:128
memoryLimit()
Normally we disable the memory_limit when running admin scripts.
Definition runJobs.php:53
__construct()
Default constructor.
Definition runJobs.php:34
finalSetup()
Handle some last-minute setup here.
Definition runJobs.php:46
foreach( $mmfl['setupFiles'] as $fileName) if($queue) if(empty( $mmfl['quiet'])) $s
$maintClass
Definition runJobs.php:133