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\JobQueue\Job;
22use MediaWiki\Maintenance\Maintenance;
23
24// @codeCoverageIgnoreStart
25require_once __DIR__ . '/Maintenance.php';
26// @codeCoverageIgnoreEnd
27
28/**
29 * Report number of jobs currently waiting in primary database.
30 *
31 * Based on runJobs.php. Note that this only works for JobQueue backends
32 * that implement JobQueue::doGetSize. Implementations based on Kafka,
33 * for example, might not have a way to obtain this. In that case,
34 * telemetry should be provided externally, e.g. with Grafana/Prometheus.
35 *
36 * @ingroup Maintenance
37 * @author Tim Starling
38 * @author Antoine Musso
39 */
40class ShowJobs extends Maintenance {
41    private const STATE_METHODS = [
42        'unclaimed' => 'getAllQueuedJobs',
43        'delayed'   => 'getAllDelayedJobs',
44        'claimed'   => 'getAllAcquiredJobs',
45        'abandoned' => 'getAllAbandonedJobs',
46    ];
47
48    public function __construct() {
49        parent::__construct();
50        $this->addDescription( 'Show number of jobs waiting in primary database' );
51        $this->addOption( 'group', 'Show number of jobs per job type' );
52        $this->addOption( 'list', 'Show a list of all jobs instead of counts' );
53        $this->addOption( 'type', 'Only show/count jobs of a given type', false, true );
54        $this->addOption( 'status', 'Filter list by state (unclaimed,delayed,claimed,abandoned)' );
55        $this->addOption( 'limit', 'Limit of jobs listed' );
56    }
57
58    public function execute() {
59        $typeFilter = $this->getOption( 'type', '' );
60        $stateFilter = $this->getOption( 'status', '' );
61        $stateLimit = (float)$this->getOption( 'limit', INF );
62
63        $group = $this->getServiceContainer()->getJobQueueGroup();
64
65        $filteredTypes = $typeFilter
66            ? [ $typeFilter ]
67            : $group->getQueueTypes();
68        $filteredStates = $stateFilter
69            ? array_intersect_key( self::STATE_METHODS, [ $stateFilter => 1 ] )
70            : self::STATE_METHODS;
71
72        if ( $this->hasOption( 'list' ) ) {
73            $count = 0;
74            foreach ( $filteredTypes as $type ) {
75                $queue = $group->get( $type );
76                foreach ( $filteredStates as $state => $method ) {
77                    foreach ( $queue->$method() as $job ) {
78                        /** @var Job $job */
79                        $this->output( $job->toString() . " status=$state\n" );
80                        if ( ++$count >= $stateLimit ) {
81                            return;
82                        }
83                    }
84                }
85            }
86        } elseif ( $this->hasOption( 'group' ) ) {
87            foreach ( $filteredTypes as $type ) {
88                $queue = $group->get( $type );
89                $delayed = $queue->getDelayedCount();
90                $pending = $queue->getSize();
91                $claimed = $queue->getAcquiredCount();
92                $abandoned = $queue->getAbandonedCount();
93                $active = max( 0, $claimed - $abandoned );
94                if ( ( $pending + $claimed + $delayed + $abandoned ) > 0 ) {
95                    $this->output(
96                        "{$type}$pending queued; " .
97                        "$claimed claimed ($active active, $abandoned abandoned); " .
98                        "$delayed delayed\n"
99                    );
100                }
101            }
102        } else {
103            $count = 0;
104            foreach ( $filteredTypes as $type ) {
105                $count += $group->get( $type )->getSize();
106            }
107            $this->output( "$count\n" );
108        }
109    }
110}
111
112// @codeCoverageIgnoreStart
113$maintClass = ShowJobs::class;
114require_once RUN_MAINTENANCE_IF_MAIN;
115// @codeCoverageIgnoreEnd