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