MediaWiki master
showJobs.php
Go to the documentation of this file.
1<?php
22
23// @codeCoverageIgnoreStart
24require_once __DIR__ . '/Maintenance.php';
25// @codeCoverageIgnoreEnd
26
39class ShowJobs extends Maintenance {
40 private const STATE_METHODS = [
41 'unclaimed' => 'getAllQueuedJobs',
42 'delayed' => 'getAllDelayedJobs',
43 'claimed' => 'getAllAcquiredJobs',
44 'abandoned' => 'getAllAbandonedJobs',
45 ];
46
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' );
55 }
56
57 public function execute() {
58 $typeFilter = $this->getOption( 'type', '' );
59 $stateFilter = $this->getOption( 'status', '' );
60 $stateLimit = (float)$this->getOption( 'limit', INF );
61
62 $group = $this->getServiceContainer()->getJobQueueGroup();
63
64 $filteredTypes = $typeFilter
65 ? [ $typeFilter ]
66 : $group->getQueueTypes();
67 $filteredStates = $stateFilter
68 ? array_intersect_key( self::STATE_METHODS, [ $stateFilter => 1 ] )
69 : self::STATE_METHODS;
70
71 if ( $this->hasOption( 'list' ) ) {
72 $count = 0;
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 ) {
80 return;
81 }
82 }
83 }
84 }
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 ) {
94 $this->output(
95 "{$type}: $pending queued; " .
96 "$claimed claimed ($active active, $abandoned abandoned); " .
97 "$delayed delayed\n"
98 );
99 }
100 }
101 } else {
102 $count = 0;
103 foreach ( $filteredTypes as $type ) {
104 $count += $group->get( $type )->getSize();
105 }
106 $this->output( "$count\n" );
107 }
108 }
109}
110
111// @codeCoverageIgnoreStart
112$maintClass = ShowJobs::class;
113require_once RUN_MAINTENANCE_IF_MAIN;
114// @codeCoverageIgnoreEnd
Describe and execute a background job.
Definition Job.php:38
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:39
execute()
Do the actual work.
Definition showJobs.php:57
__construct()
Default constructor.
Definition showJobs.php:47
if(count( $args)< 1) $job
$maintClass
Definition showJobs.php:112