Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
66.67% |
36 / 54 |
|
20.00% |
1 / 5 |
CRAP | |
0.00% |
0 / 1 |
RunJobs | |
66.67% |
36 / 54 |
|
20.00% |
1 / 5 |
42.59 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
1 | |||
finalSetup | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
memoryLimit | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
execute | |
69.23% |
27 / 39 |
|
0.00% |
0 / 1 |
27.44 | |||
debugInternal | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | /** |
3 | * Run pending jobs. |
4 | * |
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. |
9 | * |
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. |
14 | * |
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 |
19 | * |
20 | * @file |
21 | * @ingroup Maintenance |
22 | */ |
23 | |
24 | // @codeCoverageIgnoreStart |
25 | require_once __DIR__ . '/Maintenance.php'; |
26 | // @codeCoverageIgnoreEnd |
27 | |
28 | use MediaWiki\Json\FormatJson; |
29 | use MediaWiki\Maintenance\ForkController; |
30 | use MediaWiki\Settings\SettingsBuilder; |
31 | |
32 | /** |
33 | * Maintenance script that runs pending jobs. |
34 | * |
35 | * @ingroup Maintenance |
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 | // So extensions (and other code) can check whether they're running in job mode. |
52 | // This is not defined if this script is included from installer/updater or phpunit. |
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 | // Don't eat all memory on the machine if we get a bad job. |
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 | // If job queue is empty, output it |
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 | * @param string $s |
132 | */ |
133 | public function debugInternal( $s ) { |
134 | $this->output( $s ); |
135 | } |
136 | } |
137 | |
138 | // @codeCoverageIgnoreStart |
139 | $maintClass = RunJobs::class; |
140 | require_once RUN_MAINTENANCE_IF_MAIN; |
141 | // @codeCoverageIgnoreEnd |