MediaWiki master
JobQueueMemory.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\JobQueue;
8
9use ArrayIterator;
13
22class JobQueueMemory extends JobQueue {
24 protected static $data = [];
25
26 public function __construct( array $params ) {
27 $params['wanCache'] = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
28
29 parent::__construct( $params );
30 }
31
38 protected function doBatchPush( array $jobs, $flags ) {
39 $unclaimed =& $this->getQueueData( 'unclaimed', [] );
40
41 foreach ( $jobs as $job ) {
42 if ( $job->ignoreDuplicates() ) {
43 $sha1 = sha1( serialize( $job->getDeduplicationInfo() ) );
44 if ( !isset( $unclaimed[$sha1] ) ) {
45 $unclaimed[$sha1] = $job;
46 }
47 } else {
48 $unclaimed[] = $job;
49 }
50 }
51 }
52
58 protected function supportedOrders() {
59 return [ 'random', 'timestamp', 'fifo' ];
60 }
61
67 protected function optimalOrder() {
68 return 'fifo';
69 }
70
76 protected function doIsEmpty() {
77 return ( $this->doGetSize() == 0 );
78 }
79
85 protected function doGetSize() {
86 $unclaimed = $this->getQueueData( 'unclaimed' );
87
88 return $unclaimed ? count( $unclaimed ) : 0;
89 }
90
96 protected function doGetAcquiredCount() {
97 $claimed = $this->getQueueData( 'claimed' );
98
99 return $claimed ? count( $claimed ) : 0;
100 }
101
107 protected function doPop() {
108 if ( $this->doGetSize() == 0 ) {
109 return false;
110 }
111
112 $unclaimed =& $this->getQueueData( 'unclaimed' );
113 $claimed =& $this->getQueueData( 'claimed', [] );
114
115 if ( $this->order === 'random' ) {
116 $key = array_rand( $unclaimed );
117 } else {
118 $key = array_key_first( $unclaimed );
119 }
120
121 $spec = $unclaimed[$key];
122 unset( $unclaimed[$key] );
123 $claimed[] = $spec;
124
125 $job = $this->jobFromSpecInternal( $spec );
126
127 $job->setMetadata( 'claimId', array_key_last( $claimed ) );
128
129 return $job;
130 }
131
137 protected function doAck( RunnableJob $job ) {
138 if ( $this->getAcquiredCount() == 0 ) {
139 return;
140 }
141
142 $claimed =& $this->getQueueData( 'claimed' );
143 unset( $claimed[$job->getMetadata( 'claimId' )] );
144 }
145
149 protected function doDelete() {
150 if ( isset( self::$data[$this->type][$this->domain] ) ) {
151 unset( self::$data[$this->type][$this->domain] );
152 if ( !self::$data[$this->type] ) {
153 unset( self::$data[$this->type] );
154 }
155 }
156 }
157
163 public function getAllQueuedJobs() {
164 $unclaimed = $this->getQueueData( 'unclaimed' );
165 return $unclaimed ?
166 new MappedIterator( $unclaimed, $this->jobFromSpecInternal( ... ) ) :
167 new ArrayIterator( [] );
168 }
169
175 public function getAllAcquiredJobs() {
176 $claimed = $this->getQueueData( 'claimed' );
177 return $claimed ?
178 new MappedIterator( $claimed, $this->jobFromSpecInternal( ... ) ) :
179 new ArrayIterator( [] );
180 }
181
186 public function jobFromSpecInternal( IJobSpecification $spec ) {
187 return $this->factoryJob( $spec->getType(), $spec->getParams() );
188 }
189
196 private function &getQueueData( $field, $init = null ) {
197 if ( !isset( self::$data[$this->type][$this->domain][$field] ) ) {
198 if ( $init !== null ) {
199 self::$data[$this->type][$this->domain][$field] = $init;
200 } else {
201 return $init;
202 }
203 }
204
205 return self::$data[$this->type][$this->domain][$field];
206 }
207}
208
210class_alias( JobQueueMemory::class, 'JobQueueMemory' );
Convenience class for generating iterators from iterators.
PHP memory-backed job queue storage, for testing.
doDelete()
to override JobQueue::delete()
jobFromSpecInternal(IJobSpecification $spec)
Base class for queueing and running background jobs from a storage backend.
Definition JobQueue.php:36
string $domain
DB domain ID.
Definition JobQueue.php:38
string $type
Job type.
Definition JobQueue.php:40
getAcquiredCount()
Get the number of acquired jobs (these are temporarily out of the queue).
Definition JobQueue.php:271
factoryJob( $command, $params)
Definition JobQueue.php:736
Store data in a memory for the current request/process only.
Multi-datacenter aware caching interface.
Interface for serializable objects that describe a job queue task.
Job that has a run() method and metadata accessors for JobQueue::pop() and JobQueue::ack().
if(count( $args)< 1) $job