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