MediaWiki master
JobQueueMemory.php
Go to the documentation of this file.
1<?php
23
32class JobQueueMemory extends JobQueue {
34 protected static $data = [];
35
36 public function __construct( array $params ) {
37 $params['wanCache'] = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
38
39 parent::__construct( $params );
40 }
41
48 protected function doBatchPush( array $jobs, $flags ) {
49 $unclaimed =& $this->getQueueData( 'unclaimed', [] );
50
51 foreach ( $jobs as $job ) {
52 if ( $job->ignoreDuplicates() ) {
53 $sha1 = sha1( serialize( $job->getDeduplicationInfo() ) );
54 if ( !isset( $unclaimed[$sha1] ) ) {
55 $unclaimed[$sha1] = $job;
56 }
57 } else {
58 $unclaimed[] = $job;
59 }
60 }
61 }
62
68 protected function supportedOrders() {
69 return [ 'random', 'timestamp', 'fifo' ];
70 }
71
77 protected function optimalOrder() {
78 return 'fifo';
79 }
80
86 protected function doIsEmpty() {
87 return ( $this->doGetSize() == 0 );
88 }
89
95 protected function doGetSize() {
96 $unclaimed = $this->getQueueData( 'unclaimed' );
97
98 return $unclaimed ? count( $unclaimed ) : 0;
99 }
100
106 protected function doGetAcquiredCount() {
107 $claimed = $this->getQueueData( 'claimed' );
108
109 return $claimed ? count( $claimed ) : 0;
110 }
111
117 protected function doPop() {
118 if ( $this->doGetSize() == 0 ) {
119 return false;
120 }
121
122 $unclaimed =& $this->getQueueData( 'unclaimed' );
123 $claimed =& $this->getQueueData( 'claimed', [] );
124
125 if ( $this->order === 'random' ) {
126 $key = array_rand( $unclaimed );
127 } else {
128 $key = array_key_first( $unclaimed );
129 }
130
131 $spec = $unclaimed[$key];
132 unset( $unclaimed[$key] );
133 $claimed[] = $spec;
134
135 $job = $this->jobFromSpecInternal( $spec );
136
137 $job->setMetadata( 'claimId', array_key_last( $claimed ) );
138
139 return $job;
140 }
141
147 protected function doAck( RunnableJob $job ) {
148 if ( $this->getAcquiredCount() == 0 ) {
149 return;
150 }
151
152 $claimed =& $this->getQueueData( 'claimed' );
153 unset( $claimed[$job->getMetadata( 'claimId' )] );
154 }
155
159 protected function doDelete() {
160 if ( isset( self::$data[$this->type][$this->domain] ) ) {
161 unset( self::$data[$this->type][$this->domain] );
162 if ( !self::$data[$this->type] ) {
163 unset( self::$data[$this->type] );
164 }
165 }
166 }
167
173 public function getAllQueuedJobs() {
174 $unclaimed = $this->getQueueData( 'unclaimed' );
175 if ( !$unclaimed ) {
176 return new ArrayIterator( [] );
177 }
178
179 return new MappedIterator(
180 $unclaimed,
181 function ( $value ) {
182 return $this->jobFromSpecInternal( $value );
183 }
184 );
185 }
186
192 public function getAllAcquiredJobs() {
193 $claimed = $this->getQueueData( 'claimed' );
194 if ( !$claimed ) {
195 return new ArrayIterator( [] );
196 }
197
198 return new MappedIterator(
199 $claimed,
200 function ( $value ) {
201 return $this->jobFromSpecInternal( $value );
202 }
203 );
204 }
205
210 public function jobFromSpecInternal( IJobSpecification $spec ) {
211 return $this->factoryJob( $spec->getType(), $spec->getParams() );
212 }
213
220 private function &getQueueData( $field, $init = null ) {
221 if ( !isset( self::$data[$this->type][$this->domain][$field] ) ) {
222 if ( $init !== null ) {
223 self::$data[$this->type][$this->domain][$field] = $init;
224 } else {
225 return $init;
226 }
227 }
228
229 return self::$data[$this->type][$this->domain][$field];
230 }
231}
array $params
The job parameters.
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:45
string $type
Job type.
Definition JobQueue.php:49
factoryJob( $command, $params)
Definition JobQueue.php:745
getAcquiredCount()
Get the number of acquired jobs (these are temporarily out of the queue).
Definition JobQueue.php:281
string $domain
DB domain ID.
Definition JobQueue.php:47
Convenience class for generating iterators from iterators.
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