RunJobs::memoryLimit()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 33 and the first side effect is on line 24.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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\Logger\LoggerFactory;
27
28
/**
29
 * Maintenance script that runs pending jobs.
30
 *
31
 * @ingroup Maintenance
32
 */
33
class RunJobs extends Maintenance {
34
	public function __construct() {
35
		parent::__construct();
36
		$this->addDescription( 'Run pending jobs' );
37
		$this->addOption( 'maxjobs', 'Maximum number of jobs to run', false, true );
38
		$this->addOption( 'maxtime', 'Maximum amount of wall-clock time', false, true );
39
		$this->addOption( 'type', 'Type of job to run', false, true );
40
		$this->addOption( 'procs', 'Number of processes to use', false, true );
41
		$this->addOption( 'nothrottle', 'Ignore job throttling configuration', false, false );
42
		$this->addOption( 'result', 'Set to JSON to print only a JSON response', false, true );
43
		$this->addOption( 'wait', 'Wait for new jobs instead of exiting', false, false );
44
	}
45
46
	public function memoryLimit() {
47
		if ( $this->hasOption( 'memory-limit' ) ) {
48
			return parent::memoryLimit();
49
		}
50
51
		// Don't eat all memory on the machine if we get a bad job.
52
		return "150M";
53
	}
54
55
	public function execute() {
56
		if ( $this->hasOption( 'procs' ) ) {
57
			$procs = intval( $this->getOption( 'procs' ) );
58
			if ( $procs < 1 || $procs > 1000 ) {
59
				$this->error( "Invalid argument to --procs", true );
60
			} elseif ( $procs != 1 ) {
61
				$fc = new ForkController( $procs );
62
				if ( $fc->start() != 'child' ) {
63
					exit( 0 );
0 ignored issues
show
Coding Style Compatibility introduced by
The method execute() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
64
				}
65
			}
66
		}
67
68
		$outputJSON = ( $this->getOption( 'result' ) === 'json' );
69
		$wait = $this->hasOption( 'wait' );
70
71
		$runner = new JobRunner( LoggerFactory::getInstance( 'runJobs' ) );
72
		if ( !$outputJSON ) {
73
			$runner->setDebugHandler( [ $this, 'debugInternal' ] );
74
		}
75
76
		$type = $this->getOption( 'type', false );
77
		$maxJobs = $this->getOption( 'maxjobs', false );
78
		$maxTime = $this->getOption( 'maxtime', false );
79
		$throttle = !$this->hasOption( 'nothrottle' );
80
81
		while ( true ) {
82
			$response = $runner->run( [
83
				'type'     => $type,
84
				'maxJobs'  => $maxJobs,
85
				'maxTime'  => $maxTime,
86
				'throttle' => $throttle,
87
			] );
88
89
			if ( $outputJSON ) {
90
				$this->output( FormatJson::encode( $response, true ) );
0 ignored issues
show
Security Bug introduced by
It seems like \FormatJson::encode($response, true) targeting FormatJson::encode() can also be of type false; however, Maintenance::output() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
91
			}
92
93
			if (
94
				!$wait ||
95
				$response['reached'] === 'time-limit' ||
96
				$response['reached'] === 'job-limit' ||
97
				$response['reached'] === 'memory-limit'
98
			) {
99
				break;
100
			}
101
102
			if ( $maxJobs !== false ) {
103
				$maxJobs -= count( $response['jobs'] );
104
			}
105
106
			sleep( 1 );
107
		}
108
	}
109
110
	/**
111
	 * @param string $s
112
	 */
113
	public function debugInternal( $s ) {
114
		$this->output( $s );
115
	}
116
}
117
118
$maintClass = "RunJobs";
119
require_once RUN_MAINTENANCE_IF_MAIN;
120