MediaWiki master
JobQueueMemory.php
Go to the documentation of this file.
1<?php
29class JobQueueMemory extends JobQueue {
31 protected static $data = [];
32
33 public function __construct( array $params ) {
34 $params['wanCache'] = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
35
36 parent::__construct( $params );
37 }
38
45 protected function doBatchPush( array $jobs, $flags ) {
46 $unclaimed =& $this->getQueueData( 'unclaimed', [] );
47
48 foreach ( $jobs as $job ) {
49 if ( $job->ignoreDuplicates() ) {
50 $sha1 = sha1( serialize( $job->getDeduplicationInfo() ) );
51 if ( !isset( $unclaimed[$sha1] ) ) {
52 $unclaimed[$sha1] = $job;
53 }
54 } else {
55 $unclaimed[] = $job;
56 }
57 }
58 }
59
65 protected function supportedOrders() {
66 return [ 'random', 'timestamp', 'fifo' ];
67 }
68
74 protected function optimalOrder() {
75 return 'fifo';
76 }
77
83 protected function doIsEmpty() {
84 return ( $this->doGetSize() == 0 );
85 }
86
92 protected function doGetSize() {
93 $unclaimed = $this->getQueueData( 'unclaimed' );
94
95 return $unclaimed ? count( $unclaimed ) : 0;
96 }
97
103 protected function doGetAcquiredCount() {
104 $claimed = $this->getQueueData( 'claimed' );
105
106 return $claimed ? count( $claimed ) : 0;
107 }
108
114 protected function doPop() {
115 if ( $this->doGetSize() == 0 ) {
116 return false;
117 }
118
119 $unclaimed =& $this->getQueueData( 'unclaimed' );
120 $claimed =& $this->getQueueData( 'claimed', [] );
121
122 if ( $this->order === 'random' ) {
123 $key = array_rand( $unclaimed );
124 } else {
125 $key = array_key_first( $unclaimed );
126 }
127
128 $spec = $unclaimed[$key];
129 unset( $unclaimed[$key] );
130 $claimed[] = $spec;
131
132 $job = $this->jobFromSpecInternal( $spec );
133
134 $job->setMetadata( 'claimId', array_key_last( $claimed ) );
135
136 return $job;
137 }
138
144 protected function doAck( RunnableJob $job ) {
145 if ( $this->getAcquiredCount() == 0 ) {
146 return;
147 }
148
149 $claimed =& $this->getQueueData( 'claimed' );
150 unset( $claimed[$job->getMetadata( 'claimId' )] );
151 }
152
156 protected function doDelete() {
157 if ( isset( self::$data[$this->type][$this->domain] ) ) {
158 unset( self::$data[$this->type][$this->domain] );
159 if ( !self::$data[$this->type] ) {
160 unset( self::$data[$this->type] );
161 }
162 }
163 }
164
170 public function getAllQueuedJobs() {
171 $unclaimed = $this->getQueueData( 'unclaimed' );
172 if ( !$unclaimed ) {
173 return new ArrayIterator( [] );
174 }
175
176 return new MappedIterator(
177 $unclaimed,
178 function ( $value ) {
179 return $this->jobFromSpecInternal( $value );
180 }
181 );
182 }
183
189 public function getAllAcquiredJobs() {
190 $claimed = $this->getQueueData( 'claimed' );
191 if ( !$claimed ) {
192 return new ArrayIterator( [] );
193 }
194
195 return new MappedIterator(
196 $claimed,
197 function ( $value ) {
198 return $this->jobFromSpecInternal( $value );
199 }
200 );
201 }
202
207 public function jobFromSpecInternal( IJobSpecification $spec ) {
208 return $this->factoryJob( $spec->getType(), $spec->getParams() );
209 }
210
217 private function &getQueueData( $field, $init = null ) {
218 if ( !isset( self::$data[$this->type][$this->domain][$field] ) ) {
219 if ( $init !== null ) {
220 self::$data[$this->type][$this->domain][$field] = $init;
221 } else {
222 return $init;
223 }
224 }
225
226 return self::$data[$this->type][$this->domain][$field];
227 }
228}
array $params
The job parameters.
Simple store for keeping values in an associative array for the current process.
PHP memory-backed job queue storage, for testing.
doDelete()
to override JobQueue::delete()
__construct(array $params)
doBatchPush(array $jobs, $flags)
static array[] $data
doAck(RunnableJob $job)
jobFromSpecInternal(IJobSpecification $spec)
Base class for queueing and running background jobs from a storage backend.
Definition JobQueue.php:43
string $type
Job type.
Definition JobQueue.php:47
factoryJob( $command, $params)
Definition JobQueue.php:743
getAcquiredCount()
Get the number of acquired jobs (these are temporarily out of the queue).
Definition JobQueue.php:279
string $domain
DB domain ID.
Definition JobQueue.php:45
Convenience class for generating iterators from iterators.
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