Debian Codegraph @

maintenance/showJobs.php

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