MediaWiki  1.34.0
showJobs.php
Go to the documentation of this file.
1 <?php
28 require_once __DIR__ . '/Maintenance.php';
29 
36 class ShowJobs extends Maintenance {
37  protected static $stateMethods = [
38  'unclaimed' => 'getAllQueuedJobs',
39  'delayed' => 'getAllDelayedJobs',
40  'claimed' => 'getAllAcquiredJobs',
41  'abandoned' => 'getAllAbandonedJobs',
42  ];
43 
44  public function __construct() {
45  parent::__construct();
46  $this->addDescription( 'Show number of jobs waiting in master database' );
47  $this->addOption( 'group', 'Show number of jobs per job type' );
48  $this->addOption( 'list', 'Show a list of all jobs instead of counts' );
49  $this->addOption( 'type', 'Only show/count jobs of a given type', false, true );
50  $this->addOption( 'status', 'Filter list by state (unclaimed,delayed,claimed,abandoned)' );
51  $this->addOption( 'limit', 'Limit of jobs listed' );
52  }
53 
54  public function execute() {
55  $typeFilter = $this->getOption( 'type', '' );
56  $stateFilter = $this->getOption( 'status', '' );
57  $stateLimit = (float)$this->getOption( 'limit', INF );
58 
59  $group = JobQueueGroup::singleton();
60 
61  $filteredTypes = $typeFilter
62  ? [ $typeFilter ]
63  : $group->getQueueTypes();
64  $filteredStates = $stateFilter
65  ? array_intersect_key( self::$stateMethods, [ $stateFilter => 1 ] )
67 
68  if ( $this->hasOption( 'list' ) ) {
69  $count = 0;
70  foreach ( $filteredTypes as $type ) {
71  $queue = $group->get( $type );
72  foreach ( $filteredStates as $state => $method ) {
73  foreach ( $queue->$method() as $job ) {
75  $this->output( $job->toString() . " status=$state\n" );
76  if ( ++$count >= $stateLimit ) {
77  return;
78  }
79  }
80  }
81  }
82  } elseif ( $this->hasOption( 'group' ) ) {
83  foreach ( $filteredTypes as $type ) {
84  $queue = $group->get( $type );
85  $delayed = $queue->getDelayedCount();
86  $pending = $queue->getSize();
87  $claimed = $queue->getAcquiredCount();
88  $abandoned = $queue->getAbandonedCount();
89  $active = max( 0, $claimed - $abandoned );
90  if ( ( $pending + $claimed + $delayed + $abandoned ) > 0 ) {
91  $this->output(
92  "{$type}: $pending queued; " .
93  "$claimed claimed ($active active, $abandoned abandoned); " .
94  "$delayed delayed\n"
95  );
96  }
97  }
98  } else {
99  $count = 0;
100  foreach ( $filteredTypes as $type ) {
101  $count += $group->get( $type )->getSize();
102  }
103  $this->output( "$count\n" );
104  }
105  }
106 }
107 
108 $maintClass = ShowJobs::class;
109 require_once RUN_MAINTENANCE_IF_MAIN;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:39
ShowJobs\__construct
__construct()
Default constructor.
Definition: showJobs.php:44
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:348
$maintClass
$maintClass
Definition: showJobs.php:108
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:82
Job
Class to both describe a background job and handle jobs.
Definition: Job.php:30
ShowJobs\execute
execute()
Do the actual work.
Definition: showJobs.php:54
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:267
$queue
$queue
Definition: mergeMessageFileList.php:157
JobQueueGroup\singleton
static singleton( $domain=false)
Definition: JobQueueGroup.php:70
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:302
$job
if(count( $args)< 1) $job
Definition: recompressTracked.php:50
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:453
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option exists.
Definition: Maintenance.php:288
ShowJobs
Maintenance script that reports the number of jobs currently waiting in master database.
Definition: showJobs.php:36
ShowJobs\$stateMethods
static $stateMethods
Definition: showJobs.php:37
$type
$type
Definition: testCompression.php:48