| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
require_once __DIR__ . '/Maintenance.php'; |
| 26 |
|
| 27 |
|
| 28 |
use MediaWiki\Json\FormatJson; |
| 29 |
use MediaWiki\Maintenance\ForkController; |
| 30 |
use MediaWiki\Settings\SettingsBuilder; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
class 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 |
|
| 52 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
public function debugInternal( $s ) { |
| 134 |
$this->output( $s ); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
$maintClass = RunJobs::class; |
| 140 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 141 |
|