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.
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.
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
21 use MediaWiki\Maintenance\Maintenance
;
23 // @codeCoverageIgnoreStart
24 require_once __DIR__
. '/Maintenance.php';
25 // @codeCoverageIgnoreEnd
28 * Report number of jobs currently waiting in primary database.
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.
35 * @ingroup Maintenance
36 * @author Tim Starling
37 * @author Antoine Musso
39 class ShowJobs
extends Maintenance
{
40 private const STATE_METHODS
= [
41 'unclaimed' => 'getAllQueuedJobs',
42 'delayed' => 'getAllDelayedJobs',
43 'claimed' => 'getAllAcquiredJobs',
44 'abandoned' => 'getAllAbandonedJobs',
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' );
57 public function execute() {
58 $typeFilter = $this->getOption( 'type', '' );
59 $stateFilter = $this->getOption( 'status', '' );
60 $stateLimit = (float)$this->getOption( 'limit', INF
);
62 $group = $this->getServiceContainer()->getJobQueueGroup();
64 $filteredTypes = $typeFilter
66 : $group->getQueueTypes();
67 $filteredStates = $stateFilter
68 ?
array_intersect_key( self
::STATE_METHODS
, [ $stateFilter => 1 ] )
69 : self
::STATE_METHODS
;
71 if ( $this->hasOption( 'list' ) ) {
73 foreach ( $filteredTypes as $type ) {
74 $queue = $group->get( $type );
75 foreach ( $filteredStates as $state => $method ) {
76 foreach ( $queue->$method() as $job ) {
78 $this->output( $job->toString() . " status=$state\n" );
79 if ( ++
$count >= $stateLimit ) {
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 ) {
95 "{$type}: $pending queued; " .
96 "$claimed claimed ($active active, $abandoned abandoned); " .
103 foreach ( $filteredTypes as $type ) {
104 $count +
= $group->get( $type )->getSize();
106 $this->output( "$count\n" );
111 // @codeCoverageIgnoreStart
112 $maintClass = ShowJobs
::class;
113 require_once RUN_MAINTENANCE_IF_MAIN
;
114 // @codeCoverageIgnoreEnd