5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup Maintenance
24 if ( !defined( 'MEDIAWIKI' ) ) {
25 // So extensions (and other code) can check whether they're running in job mode.
26 // This is not defined if this script is included from installer/updater or phpunit.
27 define( 'MEDIAWIKI_JOB_RUNNER', true );
30 require_once __DIR__
. '/Maintenance.php';
32 use MediaWiki\Logger\LoggerFactory
;
35 * Maintenance script that runs pending jobs.
37 * @ingroup Maintenance
39 class RunJobs
extends Maintenance
{
40 public function __construct() {
41 parent
::__construct();
42 $this->addDescription( 'Run pending jobs' );
43 $this->addOption( 'maxjobs', 'Maximum number of jobs to run', false, true );
44 $this->addOption( 'maxtime', 'Maximum amount of wall-clock time', false, true );
45 $this->addOption( 'type', 'Type of job to run', false, true );
46 $this->addOption( 'procs', 'Number of processes to use', false, true );
47 $this->addOption( 'nothrottle', 'Ignore job throttling configuration', false, false );
48 $this->addOption( 'result', 'Set to "json" to print only a JSON response', false, true );
49 $this->addOption( 'wait', 'Wait for new jobs instead of exiting', false, false );
52 public function memoryLimit() {
53 if ( $this->hasOption( 'memory-limit' ) ) {
54 return parent
::memoryLimit();
57 // Don't eat all memory on the machine if we get a bad job.
61 public function execute() {
62 if ( $this->hasOption( 'procs' ) ) {
63 $procs = intval( $this->getOption( 'procs' ) );
64 if ( $procs < 1 ||
$procs > 1000 ) {
65 $this->fatalError( "Invalid argument to --procs" );
66 } elseif ( $procs != 1 ) {
67 $fc = new ForkController( $procs );
68 if ( $fc->start() != 'child' ) {
74 $outputJSON = ( $this->getOption( 'result' ) === 'json' );
75 $wait = $this->hasOption( 'wait' );
77 $runner = new JobRunner( LoggerFactory
::getInstance( 'runJobs' ) );
79 $runner->setDebugHandler( [ $this, 'debugInternal' ] );
82 $type = $this->getOption( 'type', false );
83 $maxJobs = $this->getOption( 'maxjobs', false );
84 $maxTime = $this->getOption( 'maxtime', false );
85 $throttle = !$this->hasOption( 'nothrottle' );
88 $response = $runner->run( [
90 'maxJobs' => $maxJobs,
91 'maxTime' => $maxTime,
92 'throttle' => $throttle,
96 $this->output( FormatJson
::encode( $response, true ) );
101 $response['reached'] === 'time-limit' ||
102 $response['reached'] === 'job-limit' ||
103 $response['reached'] === 'memory-limit'
105 // If job queue is empty, output it
106 if ( !$outputJSON && $response['jobs'] === [] ) {
107 $this->output( "Job queue is empty.\n" );
112 if ( $maxJobs !== false ) {
113 $maxJobs -= count( $response['jobs'] );
123 public function debugInternal( $s ) {
128 $maintClass = RunJobs
::class;
129 require_once RUN_MAINTENANCE_IF_MAIN
;