MediaWiki master
showJobs.php
Go to the documentation of this file.
1<?php
9
10// @codeCoverageIgnoreStart
11require_once __DIR__ . '/Maintenance.php';
12// @codeCoverageIgnoreEnd
13
26class ShowJobs extends Maintenance {
27 private const STATE_METHODS = [
28 'unclaimed' => 'getAllQueuedJobs',
29 'delayed' => 'getAllDelayedJobs',
30 'claimed' => 'getAllAcquiredJobs',
31 'abandoned' => 'getAllAbandonedJobs',
32 ];
33
34 public function __construct() {
35 parent::__construct();
36 $this->addDescription( 'Show number of jobs waiting in primary database' );
37 $this->addOption( 'group', 'Show number of jobs per job type' );
38 $this->addOption( 'list', 'Show a list of all jobs instead of counts' );
39 $this->addOption( 'type', 'Only show/count jobs of a given type', false, true );
40 $this->addOption( 'status', 'Filter list by state (unclaimed,delayed,claimed,abandoned)' );
41 $this->addOption( 'limit', 'Limit of jobs listed' );
42 }
43
44 public function execute() {
45 $typeFilter = $this->getOption( 'type', '' );
46 $stateFilter = $this->getOption( 'status', '' );
47 $stateLimit = (float)$this->getOption( 'limit', INF );
48
49 $group = $this->getServiceContainer()->getJobQueueGroup();
50
51 $filteredTypes = $typeFilter
52 ? [ $typeFilter ]
53 : $group->getQueueTypes();
54 $filteredStates = $stateFilter
55 ? array_intersect_key( self::STATE_METHODS, [ $stateFilter => 1 ] )
56 : self::STATE_METHODS;
57
58 if ( $this->hasOption( 'list' ) ) {
59 $count = 0;
60 foreach ( $filteredTypes as $type ) {
61 $queue = $group->get( $type );
62 foreach ( $filteredStates as $state => $method ) {
63 foreach ( $queue->$method() as $job ) {
65 $this->output( $job->toString() . " status=$state\n" );
66 if ( ++$count >= $stateLimit ) {
67 return;
68 }
69 }
70 }
71 }
72 } elseif ( $this->hasOption( 'group' ) ) {
73 foreach ( $filteredTypes as $type ) {
74 $queue = $group->get( $type );
75 $delayed = $queue->getDelayedCount();
76 $pending = $queue->getSize();
77 $claimed = $queue->getAcquiredCount();
78 $abandoned = $queue->getAbandonedCount();
79 $active = max( 0, $claimed - $abandoned );
80 if ( ( $pending + $claimed + $delayed + $abandoned ) > 0 ) {
81 $this->output(
82 "{$type}: $pending queued; " .
83 "$claimed claimed ($active active, $abandoned abandoned); " .
84 "$delayed delayed\n"
85 );
86 }
87 }
88 } else {
89 $count = 0;
90 foreach ( $filteredTypes as $type ) {
91 $count += $group->get( $type )->getSize();
92 }
93 $this->output( "$count\n" );
94 }
95 }
96}
97
98// @codeCoverageIgnoreStart
99$maintClass = ShowJobs::class;
100require_once RUN_MAINTENANCE_IF_MAIN;
101// @codeCoverageIgnoreEnd
Describe and execute a background job.
Definition Job.php:28
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
hasOption( $name)
Checks to see if a particular option was set.
getOption( $name, $default=null)
Get an option, or return the default.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
Report number of jobs currently waiting in primary database.
Definition showJobs.php:26
execute()
Do the actual work.
Definition showJobs.php:44
__construct()
Default constructor.
Definition showJobs.php:34
if(count( $args)< 1) $job
$maintClass
Definition showJobs.php:99