MediaWiki master
showJobs.php
Go to the documentation of this file.
1<?php
21// @codeCoverageIgnoreStart
22require_once __DIR__ . '/Maintenance.php';
23// @codeCoverageIgnoreEnd
24
37class 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 ) {
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;
111require_once RUN_MAINTENANCE_IF_MAIN;
112// @codeCoverageIgnoreEnd
Describe and execute a background job.
Definition Job.php:41
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
hasOption( $name)
Checks to see if a particular option was set.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
Report number of jobs currently waiting in primary database.
Definition showJobs.php:37
execute()
Do the actual work.
Definition showJobs.php:55
__construct()
Default constructor.
Definition showJobs.php:45
if(count( $args)< 1) $job
$maintClass
Definition showJobs.php:110