9use InvalidArgumentException;
40 private $localJobClasses;
42 private $jobTypeConfiguration;
44 private $jobTypesExcludedFromDefaultQueue;
46 private $statsFactory;
50 private $globalIdGenerator;
56 private const TYPE_ANY = 2;
60 private const PROC_CACHE_TTL = 15;
77 ?array $localJobClasses,
78 array $jobTypeConfiguration,
79 array $jobTypesExcludedFromDefaultQueue,
87 $this->localJobClasses = $localJobClasses;
88 $this->jobTypeConfiguration = $jobTypeConfiguration;
89 $this->jobTypesExcludedFromDefaultQueue = $jobTypesExcludedFromDefaultQueue;
90 $this->statsFactory = $statsFactory;
91 $this->wanCache = $wanCache;
92 $this->globalIdGenerator = $globalIdGenerator;
101 public function get( $type ) {
103 $conf += $this->jobTypeConfiguration[$type] ?? $this->jobTypeConfiguration[
'default'];
104 if ( !isset( $conf[
'readOnlyReason'] ) ) {
105 $conf[
'readOnlyReason'] = $this->readOnlyMode->getConfiguredReason();
108 $conf[
'stats'] = $this->statsFactory;
109 $conf[
'wanCache'] = $this->wanCache;
110 $conf[
'idGenerator'] = $this->globalIdGenerator;
124 public function push( $jobs ) {
125 $jobs = is_array( $jobs ) ? $jobs : [ $jobs ];
126 if ( $jobs === [] ) {
130 $this->assertValidJobs( $jobs );
133 foreach ( $jobs as
$job ) {
134 $type =
$job->getType();
135 if ( isset( $this->jobTypeConfiguration[$type] ) ) {
136 $jobsByType[$type][] =
$job;
139 isset( $this->jobTypeConfiguration[
'default'][
'typeAgnostic'] ) &&
140 $this->jobTypeConfiguration[
'default'][
'typeAgnostic']
142 $jobsByType[
'default'][] =
$job;
144 $jobsByType[$type][] =
$job;
149 foreach ( $jobsByType as $type => $jobs ) {
150 $this->
get( $type )->
push( $jobs );
153 if ( $this->cache->hasField(
'queues-ready',
'list' ) ) {
154 $list = $this->cache->getField(
'queues-ready',
'list' );
155 if ( count( array_diff( array_keys( $jobsByType ), $list ) ) ) {
156 $this->cache->clear(
'queues-ready' );
162 $cache->makeGlobalKey(
'jobqueue', $this->domain,
'hasjobs', self::TYPE_ANY ),
166 if ( array_diff( array_keys( $jobsByType ), $this->jobTypesExcludedFromDefaultQueue ) ) {
168 $cache->makeGlobalKey(
'jobqueue', $this->domain,
'hasjobs', self::TYPE_DEFAULT ),
183 if ( PHP_SAPI ===
'cli' || PHP_SAPI ===
'phpdbg' ) {
184 $this->
push( $jobs );
188 $jobs = is_array( $jobs ) ? $jobs : [ $jobs ];
191 $this->assertValidJobs( $jobs );
210 public function pop( $qtype = self::TYPE_DEFAULT, $flags = 0, array $ignored = [] ) {
213 if ( !$this->localJobClasses ) {
215 "Cannot pop '{$qtype}' job off foreign '{$this->domain}' wiki queue." );
217 if ( is_string( $qtype ) && !isset( $this->localJobClasses[$qtype] ) ) {
219 throw new JobQueueError(
"Unrecognized job type '$qtype'." );
222 if ( is_string( $qtype ) ) {
223 if ( !in_array( $qtype, $ignored ) ) {
224 $job = $this->
get( $qtype )->
pop();
227 if ( $flags & self::USE_CACHE ) {
228 if ( !$this->cache->hasField(
'queues-ready',
'list', self::PROC_CACHE_TTL ) ) {
231 $types = $this->cache->getField(
'queues-ready',
'list' );
236 if ( $qtype == self::TYPE_DEFAULT ) {
240 $types = array_diff( $types, $ignored );
243 foreach ( $types as $type ) {
244 $job = $this->
get( $type )->
pop();
248 $this->cache->clear(
'queues-ready' );
274 if ( !$this->localJobClasses ) {
275 throw new LogicException(
'Cannot inspect job queue from foreign wiki' );
277 return array_keys( $this->localJobClasses );
288 return array_diff( $this->
getQueueTypes(), $this->jobTypesExcludedFromDefaultQueue );
302 $key =
$cache->makeGlobalKey(
'jobqueue', $this->domain,
'hasjobs', $type );
305 if ( $value ===
false ) {
307 if ( $type == self::TYPE_DEFAULT ) {
310 $value = count( $queues ) ?
'true' :
'false';
311 $cache->add( $key, $value, 15 );
314 return ( $value ===
'true' );
328 $queue = $info[
'queue'];
329 $nonEmpty = $queue->getSiblingQueuesWithJobs( $this->
getQueueTypes() );
330 if ( is_array( $nonEmpty ) ) {
331 $types = array_merge( $types, $nonEmpty );
333 foreach ( $info[
'types'] as $type ) {
334 if ( !$this->
get( $type )->isEmpty() ) {
355 $queue = $info[
'queue'];
356 $sizes = $queue->getSiblingQueueSizes( $this->
getQueueTypes() );
357 if ( is_array( $sizes ) ) {
360 foreach ( $info[
'types'] as $type ) {
361 $sizeMap[$type] = $this->
get( $type )->getSize();
374 if ( $this->coalescedQueues ===
null ) {
375 $this->coalescedQueues = [];
376 foreach ( $this->jobTypeConfiguration as $type => $conf ) {
378 $conf[
'type'] =
'null';
379 $conf[
'stats'] = $this->statsFactory;
380 $conf[
'wanCache'] = $this->wanCache;
381 $conf[
'idGenerator'] = $this->globalIdGenerator;
384 $loc = $queue->getCoalesceLocationInternal() ??
'';
385 if ( !isset( $this->coalescedQueues[$loc] ) ) {
386 $this->coalescedQueues[$loc][
'queue'] = $queue;
387 $this->coalescedQueues[$loc][
'types'] = [];
389 if ( $type ===
'default' ) {
390 $this->coalescedQueues[$loc][
'types'] = array_merge(
391 $this->coalescedQueues[$loc][
'types'],
392 array_diff( $this->
getQueueTypes(), array_keys( $this->jobTypeConfiguration ) )
395 $this->coalescedQueues[$loc][
'types'][] = $type;
403 private function assertValidJobs( array $jobs ) {
404 foreach ( $jobs as
$job ) {
406 $type = get_debug_type(
$job );
407 throw new InvalidArgumentException(
"Expected IJobSpecification objects, got " . $type );
414class_alias( JobQueueGroup::class,
'JobQueueGroup' );
if(count( $args)< 1) $job