MediaWiki  1.34.0
JobQueueMemory.php
Go to the documentation of this file.
1 <?php
31 class JobQueueMemory extends JobQueue {
33  protected static $data = [];
34 
35  public function __construct( array $params ) {
36  $params['wanCache'] = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
37 
38  parent::__construct( $params );
39  }
40 
47  protected function doBatchPush( array $jobs, $flags ) {
48  $unclaimed =& $this->getQueueData( 'unclaimed', [] );
49 
50  foreach ( $jobs as $job ) {
51  if ( $job->ignoreDuplicates() ) {
52  $sha1 = sha1( serialize( $job->getDeduplicationInfo() ) );
53  if ( !isset( $unclaimed[$sha1] ) ) {
54  $unclaimed[$sha1] = $job;
55  }
56  } else {
57  $unclaimed[] = $job;
58  }
59  }
60  }
61 
67  protected function supportedOrders() {
68  return [ 'random', 'timestamp', 'fifo' ];
69  }
70 
76  protected function optimalOrder() {
77  return 'fifo';
78  }
79 
85  protected function doIsEmpty() {
86  return ( $this->doGetSize() == 0 );
87  }
88 
94  protected function doGetSize() {
95  $unclaimed = $this->getQueueData( 'unclaimed' );
96 
97  return $unclaimed ? count( $unclaimed ) : 0;
98  }
99 
105  protected function doGetAcquiredCount() {
106  $claimed = $this->getQueueData( 'claimed' );
107 
108  return $claimed ? count( $claimed ) : 0;
109  }
110 
116  protected function doPop() {
117  if ( $this->doGetSize() == 0 ) {
118  return false;
119  }
120 
121  $unclaimed =& $this->getQueueData( 'unclaimed' );
122  $claimed =& $this->getQueueData( 'claimed', [] );
123 
124  if ( $this->order === 'random' ) {
125  $key = array_rand( $unclaimed );
126  } else {
127  reset( $unclaimed );
128  $key = key( $unclaimed );
129  }
130 
131  $spec = $unclaimed[$key];
132  unset( $unclaimed[$key] );
133  $claimed[] = $spec;
134 
135  $job = $this->jobFromSpecInternal( $spec );
136 
137  end( $claimed );
138  $job->setMetadata( 'claimId', key( $claimed ) );
139 
140  return $job;
141  }
142 
148  protected function doAck( RunnableJob $job ) {
149  if ( $this->getAcquiredCount() == 0 ) {
150  return;
151  }
152 
153  $claimed =& $this->getQueueData( 'claimed' );
154  unset( $claimed[$job->getMetadata( 'claimId' )] );
155  }
156 
160  protected function doDelete() {
161  if ( isset( self::$data[$this->type][$this->domain] ) ) {
162  unset( self::$data[$this->type][$this->domain] );
163  if ( !self::$data[$this->type] ) {
164  unset( self::$data[$this->type] );
165  }
166  }
167  }
168 
174  public function getAllQueuedJobs() {
175  $unclaimed = $this->getQueueData( 'unclaimed' );
176  if ( !$unclaimed ) {
177  return new ArrayIterator( [] );
178  }
179 
180  return new MappedIterator(
181  $unclaimed,
182  function ( $value ) {
183  return $this->jobFromSpecInternal( $value );
184  }
185  );
186  }
187 
193  public function getAllAcquiredJobs() {
194  $claimed = $this->getQueueData( 'claimed' );
195  if ( !$claimed ) {
196  return new ArrayIterator( [] );
197  }
198 
199  return new MappedIterator(
200  $claimed,
201  function ( $value ) {
202  return $this->jobFromSpecInternal( $value );
203  }
204  );
205  }
206 
211  public function jobFromSpecInternal( IJobSpecification $spec ) {
212  return $this->factoryJob( $spec->getType(), $spec->getParams() );
213  }
214 
221  private function &getQueueData( $field, $init = null ) {
222  if ( !isset( self::$data[$this->type][$this->domain][$field] ) ) {
223  if ( $init !== null ) {
224  self::$data[$this->type][$this->domain][$field] = $init;
225  } else {
226  return $init;
227  }
228  }
229 
230  return self::$data[$this->type][$this->domain][$field];
231  }
232 }
MappedIterator
Convenience class for generating iterators from iterators.
Definition: MappedIterator.php:28
JobQueueMemory\optimalOrder
optimalOrder()
Definition: JobQueueMemory.php:76
JobQueueMemory\doAck
doAck(RunnableJob $job)
Definition: JobQueueMemory.php:148
JobQueueMemory\getAllQueuedJobs
getAllQueuedJobs()
Definition: JobQueueMemory.php:174
HashBagOStuff
Simple store for keeping values in an associative array for the current process.
Definition: HashBagOStuff.php:31
RunnableJob
Job that has a run() method and metadata accessors for JobQueue::pop() and JobQueue::ack()
Definition: RunnableJob.php:35
JobQueueMemory\$data
static array[] $data
Definition: JobQueueMemory.php:33
serialize
serialize()
Definition: ApiMessageTrait.php:138
IJobSpecification\getType
getType()
JobQueueMemory\jobFromSpecInternal
jobFromSpecInternal(IJobSpecification $spec)
Definition: JobQueueMemory.php:211
JobQueue\getAcquiredCount
getAcquiredCount()
Get the number of acquired jobs (these are temporarily out of the queue).
Definition: JobQueue.php:249
JobQueueMemory\doIsEmpty
doIsEmpty()
Definition: JobQueueMemory.php:85
JobQueue\$type
string $type
Job type.
Definition: JobQueue.php:35
JobQueueMemory
Class to handle job queues stored in PHP memory for testing.
Definition: JobQueueMemory.php:31
IJobSpecification\getParams
getParams()
JobQueueMemory\getQueueData
& getQueueData( $field, $init=null)
Definition: JobQueueMemory.php:221
JobQueueMemory\doBatchPush
doBatchPush(array $jobs, $flags)
Definition: JobQueueMemory.php:47
WANObjectCache
Multi-datacenter aware caching interface.
Definition: WANObjectCache.php:116
JobQueueMemory\doPop
doPop()
Definition: JobQueueMemory.php:116
JobQueueMemory\doGetAcquiredCount
doGetAcquiredCount()
Definition: JobQueueMemory.php:105
JobQueueMemory\getAllAcquiredJobs
getAllAcquiredJobs()
Definition: JobQueueMemory.php:193
JobQueueMemory\__construct
__construct(array $params)
Definition: JobQueueMemory.php:35
$job
if(count( $args)< 1) $job
Definition: recompressTracked.php:50
JobQueue
Class to handle enqueueing and running of background jobs.
Definition: JobQueue.php:31
JobQueue\factoryJob
factoryJob( $command, $params)
Definition: JobQueue.php:704
JobQueueMemory\doGetSize
doGetSize()
Definition: JobQueueMemory.php:94
JobQueue\$domain
string $domain
DB domain ID.
Definition: JobQueue.php:33
IJobSpecification
Interface for serializable objects that describe a job queue task.
Definition: IJobSpecification.php:35
JobQueueMemory\supportedOrders
supportedOrders()
Definition: JobQueueMemory.php:67
JobQueueMemory\doDelete
doDelete()
Definition: JobQueueMemory.php:160