Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
65.45% covered (warning)
65.45%
36 / 55
20.00% covered (danger)
20.00%
1 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
RunJobs
65.45% covered (warning)
65.45%
36 / 55
20.00% covered (danger)
20.00%
1 / 5
47.75
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 finalSetup
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 memoryLimit
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
 execute
69.23% covered (warning)
69.23%
27 / 39
0.00% covered (danger)
0.00%
0 / 1
27.44
 debugInternal
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Run pending jobs.
4 *
5 * @license GPL-2.0-or-later
6 * @file
7 * @ingroup Maintenance
8 */
9
10// @codeCoverageIgnoreStart
11require_once __DIR__ . '/Maintenance.php';
12// @codeCoverageIgnoreEnd
13
14use MediaWiki\Json\FormatJson;
15use MediaWiki\Maintenance\ForkController;
16use MediaWiki\Maintenance\Maintenance;
17use MediaWiki\Settings\SettingsBuilder;
18
19/**
20 * Maintenance script that runs pending jobs.
21 *
22 * @ingroup Maintenance
23 */
24class RunJobs extends Maintenance {
25    public function __construct() {
26        parent::__construct();
27        $this->addDescription( 'Run pending jobs' );
28        $this->addOption( 'maxjobs', 'Maximum number of jobs to run', false, true );
29        $this->addOption( 'maxtime', 'Maximum amount of wall-clock time', false, true );
30        $this->addOption( 'type', 'Type of job to run', false, true );
31        $this->addOption( 'procs', 'Number of processes to use', false, true );
32        $this->addOption( 'nothrottle', 'Ignore job throttling configuration', false, false );
33        $this->addOption( 'result', 'Set to "json" to print only a JSON response', false, true );
34        $this->addOption( 'wait', 'Wait for new jobs instead of exiting', false, false );
35    }
36
37    public function finalSetup( SettingsBuilder $settingsBuilder ) {
38        // So extensions (and other code) can check whether they're running in job mode.
39        // This is not defined if this script is included from installer/updater or phpunit.
40        define( 'MEDIAWIKI_JOB_RUNNER', true );
41        parent::finalSetup( $settingsBuilder );
42    }
43
44    /** @inheritDoc */
45    public function memoryLimit() {
46        if ( $this->hasOption( 'memory-limit' ) ) {
47            return parent::memoryLimit();
48        }
49
50        // Don't eat all memory on the machine if we get a bad job.
51        //
52        // The default memory_limit for PHP-CLI is -1 (unlimited).
53        // This is fine for most maintenance scripts, but runJobs.php is unusually likely
54        // to leak memory (e.g. some badly-managed in-process cache array in some class)
55        // because it can run for long periods doing different tasks.
56        // Let's use 3x the limit for a web request.
57        global $wgMemoryLimit;
58        $limit = wfShorthandToInteger( (string)$wgMemoryLimit );
59        return $limit === -1 ? $limit : ( $limit * 3 );
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                } catch ( Throwable $e ) {
71                    $this->fatalError( $e->getMessage() );
72                }
73                if ( $fc->start() != 'child' ) {
74                    return;
75                }
76            }
77        }
78
79        $outputJSON = ( $this->getOption( 'result' ) === 'json' );
80        $wait = $this->hasOption( 'wait' );
81
82        $runner = $this->getServiceContainer()->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                $response['reached'] === 'exception'
110            ) {
111                // If job queue is empty, output it
112                if ( !$outputJSON && $response['jobs'] === [] ) {
113                    $this->output( "Job queue is empty.\n" );
114                }
115                break;
116            }
117
118            if ( $maxJobs !== false ) {
119                $maxJobs -= count( $response['jobs'] );
120            }
121
122            sleep( 1 );
123        }
124    }
125
126    /**
127     * @param string $s
128     */
129    private function debugInternal( $s ) {
130        $this->output( $s );
131    }
132}
133
134// @codeCoverageIgnoreStart
135$maintClass = RunJobs::class;
136require_once RUN_MAINTENANCE_IF_MAIN;
137// @codeCoverageIgnoreEnd