ShowJobs::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 9
rs 9.6666
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 36 and the first side effect is on line 28.

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
 * Report number of jobs currently waiting in master database.
4
 *
5
 * Based on runJobs.php
6
 *
7
 * This program is free software; you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 2 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License along
18
 * with this program; if not, write to the Free Software Foundation, Inc.,
19
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20
 * http://www.gnu.org/copyleft/gpl.html
21
 *
22
 * @file
23
 * @ingroup Maintenance
24
 * @author Tim Starling
25
 * @author Antoine Musso
26
 */
27
28
require_once __DIR__ . '/Maintenance.php';
29
30
/**
31
 * Maintenance script that reports the number of jobs currently waiting
32
 * in master database.
33
 *
34
 * @ingroup Maintenance
35
 */
36
class ShowJobs extends Maintenance {
37
	protected static $stateMethods = [
38
		'unclaimed' => 'getAllQueuedJobs',
39
		'delayed'   => 'getAllDelayedJobs',
40
		'claimed'   => 'getAllAcquiredJobs',
41
		'abandoned' => 'getAllAbandonedJobs',
42
	];
43
44
	public function __construct() {
45
		parent::__construct();
46
		$this->addDescription( 'Show number of jobs waiting in master database' );
47
		$this->addOption( 'group', 'Show number of jobs per job type' );
48
		$this->addOption( 'list', 'Show a list of all jobs instead of counts' );
49
		$this->addOption( 'type', 'Only show/count jobs of a given type', false, true );
50
		$this->addOption( 'status', 'Filter list by state (unclaimed,delayed,claimed,abandoned)' );
51
		$this->addOption( 'limit', 'Limit of jobs listed' );
52
	}
53
54
	public function execute() {
55
		$typeFilter = $this->getOption( 'type', '' );
56
		$stateFilter = $this->getOption( 'status', '' );
57
		$stateLimit = (float)$this->getOption( 'limit', INF );
58
59
		$group = JobQueueGroup::singleton();
60
61
		$filteredTypes = $typeFilter
62
			? [ $typeFilter ]
63
			: $group->getQueueTypes();
64
		$filteredStates = $stateFilter
65
			? array_intersect_key( self::$stateMethods, [ $stateFilter => 1 ] )
66
			: self::$stateMethods;
67
68
		if ( $this->hasOption( 'list' ) ) {
69
			$count = 0;
70
			foreach ( $filteredTypes as $type ) {
71
				$queue = $group->get( $type );
72
				foreach ( $filteredStates as $state => $method ) {
73
					foreach ( $queue->$method() as $job ) {
74
						/** @var Job $job */
75
						$this->output( $job->toString() . " status=$state\n" );
76
						if ( ++$count >= $stateLimit ) {
77
							return;
78
						}
79
					}
80
				}
81
			}
82
		} elseif ( $this->hasOption( 'group' ) ) {
83
			foreach ( $filteredTypes as $type ) {
84
				$queue = $group->get( $type );
85
				$delayed = $queue->getDelayedCount();
86
				$pending = $queue->getSize();
87
				$claimed = $queue->getAcquiredCount();
88
				$abandoned = $queue->getAbandonedCount();
89
				$active = max( 0, $claimed - $abandoned );
90
				if ( ( $pending + $claimed + $delayed + $abandoned ) > 0 ) {
91
					$this->output(
92
						"{$type}: $pending queued; " .
93
						"$claimed claimed ($active active, $abandoned abandoned); " .
94
						"$delayed delayed\n"
95
					);
96
				}
97
			}
98
		} else {
99
			$count = 0;
100
			foreach ( $filteredTypes as $type ) {
101
				$count += $group->get( $type )->getSize();
102
			}
103
			$this->output( "$count\n" );
104
		}
105
	}
106
}
107
108
$maintClass = "ShowJobs";
109
require_once RUN_MAINTENANCE_IF_MAIN;
110