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