Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 57 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
RunJobs | |
0.00% |
0 / 54 |
|
0.00% |
0 / 5 |
552 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
2 | |||
finalSetup | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
memoryLimit | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
execute | |
0.00% |
0 / 39 |
|
0.00% |
0 / 1 |
342 | |||
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 | require_once __DIR__ . '/Maintenance.php'; |
25 | |
26 | use MediaWiki\Maintenance\ForkController; |
27 | use MediaWiki\Settings\SettingsBuilder; |
28 | |
29 | /** |
30 | * Maintenance script that runs pending jobs. |
31 | * |
32 | * @ingroup Maintenance |
33 | */ |
34 | class 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 ) { |
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 | |
127 | /** |
128 | * @param string $s |
129 | */ |
130 | public function debugInternal( $s ) { |
131 | $this->output( $s ); |
132 | } |
133 | } |
134 | |
135 | $maintClass = RunJobs::class; |
136 | require_once RUN_MAINTENANCE_IF_MAIN; |