MediaWiki master
showJobs.php
Go to the documentation of this file.
1<?php
23
24// @codeCoverageIgnoreStart
25require_once __DIR__ . '/Maintenance.php';
26// @codeCoverageIgnoreEnd
27
40class ShowJobs extends Maintenance {
41 private const STATE_METHODS = [
42 'unclaimed' => 'getAllQueuedJobs',
43 'delayed' => 'getAllDelayedJobs',
44 'claimed' => 'getAllAcquiredJobs',
45 'abandoned' => 'getAllAbandonedJobs',
46 ];
47
48 public function __construct() {
49 parent::__construct();
50 $this->addDescription( 'Show number of jobs waiting in primary database' );
51 $this->addOption( 'group', 'Show number of jobs per job type' );
52 $this->addOption( 'list', 'Show a list of all jobs instead of counts' );
53 $this->addOption( 'type', 'Only show/count jobs of a given type', false, true );
54 $this->addOption( 'status', 'Filter list by state (unclaimed,delayed,claimed,abandoned)' );
55 $this->addOption( 'limit', 'Limit of jobs listed' );
56 }
57
58 public function execute() {
59 $typeFilter = $this->getOption( 'type', '' );
60 $stateFilter = $this->getOption( 'status', '' );
61 $stateLimit = (float)$this->getOption( 'limit', INF );
62
63 $group = $this->getServiceContainer()->getJobQueueGroup();
64
65 $filteredTypes = $typeFilter
66 ? [ $typeFilter ]
67 : $group->getQueueTypes();
68 $filteredStates = $stateFilter
69 ? array_intersect_key( self::STATE_METHODS, [ $stateFilter => 1 ] )
70 : self::STATE_METHODS;
71
72 if ( $this->hasOption( 'list' ) ) {
73 $count = 0;
74 foreach ( $filteredTypes as $type ) {
75 $queue = $group->get( $type );
76 foreach ( $filteredStates as $state => $method ) {
77 foreach ( $queue->$method() as $job ) {
79 $this->output( $job->toString() . " status=$state\n" );
80 if ( ++$count >= $stateLimit ) {
81 return;
82 }
83 }
84 }
85 }
86 } elseif ( $this->hasOption( 'group' ) ) {
87 foreach ( $filteredTypes as $type ) {
88 $queue = $group->get( $type );
89 $delayed = $queue->getDelayedCount();
90 $pending = $queue->getSize();
91 $claimed = $queue->getAcquiredCount();
92 $abandoned = $queue->getAbandonedCount();
93 $active = max( 0, $claimed - $abandoned );
94 if ( ( $pending + $claimed + $delayed + $abandoned ) > 0 ) {
95 $this->output(
96 "{$type}: $pending queued; " .
97 "$claimed claimed ($active active, $abandoned abandoned); " .
98 "$delayed delayed\n"
99 );
100 }
101 }
102 } else {
103 $count = 0;
104 foreach ( $filteredTypes as $type ) {
105 $count += $group->get( $type )->getSize();
106 }
107 $this->output( "$count\n" );
108 }
109 }
110}
111
112// @codeCoverageIgnoreStart
113$maintClass = ShowJobs::class;
114require_once RUN_MAINTENANCE_IF_MAIN;
115// @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.
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:40
execute()
Do the actual work.
Definition showJobs.php:58
__construct()
Default constructor.
Definition showJobs.php:48
if(count( $args)< 1) $job
$maintClass
Definition showJobs.php:113