| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
require_once __DIR__ . '/Maintenance.php'; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
class 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 ) { |
| 75 |
|
| 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 |
|
| 110 |
$maintClass = ShowJobs::class; |
| 111 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 112 |
|