MediaWiki master
JobQueueMemory.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\JobQueue;
8
9use ArrayIterator;
12
21class JobQueueMemory extends JobQueue {
23 protected static $data = [];
24
25 public function __construct( array $params ) {
26 $params['localClusterCache'] = new HashBagOStuff();
27
28 parent::__construct( $params );
29 }
30
37 protected function doBatchPush( array $jobs, $flags ) {
38 $unclaimed =& $this->getQueueData( 'unclaimed', [] );
39
40 foreach ( $jobs as $job ) {
41 if ( $job->ignoreDuplicates() ) {
42 $sha1 = sha1( serialize( $job->getDeduplicationInfo() ) );
43 if ( !isset( $unclaimed[$sha1] ) ) {
44 $unclaimed[$sha1] = $job;
45 }
46 } else {
47 $unclaimed[] = $job;
48 }
49 }
50 }
51
57 protected function supportedOrders() {
58 return [ 'random', 'timestamp', 'fifo' ];
59 }
60
66 protected function optimalOrder() {
67 return 'fifo';
68 }
69
75 protected function doIsEmpty() {
76 return ( $this->doGetSize() == 0 );
77 }
78
84 protected function doGetSize() {
85 $unclaimed = $this->getQueueData( 'unclaimed' );
86
87 return $unclaimed ? count( $unclaimed ) : 0;
88 }
89
95 protected function doGetAcquiredCount() {
96 $claimed = $this->getQueueData( 'claimed' );
97
98 return $claimed ? count( $claimed ) : 0;
99 }
100
106 protected function doPop() {
107 if ( $this->doGetSize() == 0 ) {
108 return false;
109 }
110
111 $unclaimed =& $this->getQueueData( 'unclaimed' );
112 $claimed =& $this->getQueueData( 'claimed', [] );
113
114 if ( $this->order === 'random' ) {
115 $key = array_rand( $unclaimed );
116 } else {
117 $key = array_key_first( $unclaimed );
118 }
119
120 $spec = $unclaimed[$key];
121 unset( $unclaimed[$key] );
122 $claimed[] = $spec;
123
124 $job = $this->jobFromSpecInternal( $spec );
125
126 $job->setMetadata( 'claimId', array_key_last( $claimed ) );
127
128 return $job;
129 }
130
136 protected function doAck( RunnableJob $job ) {
137 if ( $this->getAcquiredCount() == 0 ) {
138 return;
139 }
140
141 $claimed =& $this->getQueueData( 'claimed' );
142 unset( $claimed[$job->getMetadata( 'claimId' )] );
143 }
144
148 protected function doDelete() {
149 if ( isset( self::$data[$this->type][$this->domain] ) ) {
150 unset( self::$data[$this->type][$this->domain] );
151 if ( !self::$data[$this->type] ) {
152 unset( self::$data[$this->type] );
153 }
154 }
155 }
156
162 public function getAllQueuedJobs() {
163 $unclaimed = $this->getQueueData( 'unclaimed' );
164 return $unclaimed ?
165 new MappedIterator( $unclaimed, $this->jobFromSpecInternal( ... ) ) :
166 new ArrayIterator( [] );
167 }
168
174 public function getAllAcquiredJobs() {
175 $claimed = $this->getQueueData( 'claimed' );
176 return $claimed ?
177 new MappedIterator( $claimed, $this->jobFromSpecInternal( ... ) ) :
178 new ArrayIterator( [] );
179 }
180
185 public function jobFromSpecInternal( IJobSpecification $spec ) {
186 return $this->factoryJob( $spec->getType(), $spec->getParams() );
187 }
188
195 private function &getQueueData( $field, $init = null ) {
196 if ( !isset( self::$data[$this->type][$this->domain][$field] ) ) {
197 if ( $init !== null ) {
198 self::$data[$this->type][$this->domain][$field] = $init;
199 } else {
200 return $init;
201 }
202 }
203
204 return self::$data[$this->type][$this->domain][$field];
205 }
206}
207
209class_alias( JobQueueMemory::class, 'JobQueueMemory' );
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:273
factoryJob( $command, $params)
Definition JobQueue.php:737
Convenience class for generating iterators from iterators.
Store data in a memory for the current request/process only.
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