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