ParsoidParser: Record ParserOptions watcher on ParserOutput object
[mediawiki.git] / includes / jobqueue / JobQueueMemory.php
blobe25ad3d31a98fd7579691f9cd70e950be57f9c36
1 <?php
2 /**
3 * PHP memory-backed job queue code.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
23 /**
24 * Class to handle job queues stored in PHP memory for testing
26 * JobQueueGroup does not remember every queue instance, so statically track it here
28 * @ingroup JobQueue
29 * @since 1.27
31 class JobQueueMemory extends JobQueue {
32 /** @var array[] */
33 protected static $data = [];
35 public function __construct( array $params ) {
36 $params['wanCache'] = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
38 parent::__construct( $params );
41 /**
42 * @see JobQueue::doBatchPush
44 * @param IJobSpecification[] $jobs
45 * @param int $flags
47 protected function doBatchPush( array $jobs, $flags ) {
48 $unclaimed =& $this->getQueueData( 'unclaimed', [] );
50 foreach ( $jobs as $job ) {
51 if ( $job->ignoreDuplicates() ) {
52 $sha1 = sha1( serialize( $job->getDeduplicationInfo() ) );
53 if ( !isset( $unclaimed[$sha1] ) ) {
54 $unclaimed[$sha1] = $job;
56 } else {
57 $unclaimed[] = $job;
62 /**
63 * @see JobQueue::supportedOrders
65 * @return string[]
67 protected function supportedOrders() {
68 return [ 'random', 'timestamp', 'fifo' ];
71 /**
72 * @see JobQueue::optimalOrder
74 * @return string
76 protected function optimalOrder() {
77 return 'fifo';
80 /**
81 * @see JobQueue::doIsEmpty
83 * @return bool
85 protected function doIsEmpty() {
86 return ( $this->doGetSize() == 0 );
89 /**
90 * @see JobQueue::doGetSize
92 * @return int
94 protected function doGetSize() {
95 $unclaimed = $this->getQueueData( 'unclaimed' );
97 return $unclaimed ? count( $unclaimed ) : 0;
101 * @see JobQueue::doGetAcquiredCount
103 * @return int
105 protected function doGetAcquiredCount() {
106 $claimed = $this->getQueueData( 'claimed' );
108 return $claimed ? count( $claimed ) : 0;
112 * @see JobQueue::doPop
114 * @return RunnableJob|false
116 protected function doPop() {
117 if ( $this->doGetSize() == 0 ) {
118 return false;
121 $unclaimed =& $this->getQueueData( 'unclaimed' );
122 $claimed =& $this->getQueueData( 'claimed', [] );
124 if ( $this->order === 'random' ) {
125 $key = array_rand( $unclaimed );
126 } else {
127 $key = array_key_first( $unclaimed );
130 $spec = $unclaimed[$key];
131 unset( $unclaimed[$key] );
132 $claimed[] = $spec;
134 $job = $this->jobFromSpecInternal( $spec );
136 $job->setMetadata( 'claimId', array_key_last( $claimed ) );
138 return $job;
142 * @see JobQueue::doAck
144 * @param RunnableJob $job
146 protected function doAck( RunnableJob $job ) {
147 if ( $this->getAcquiredCount() == 0 ) {
148 return;
151 $claimed =& $this->getQueueData( 'claimed' );
152 unset( $claimed[$job->getMetadata( 'claimId' )] );
156 * @inheritDoc
158 protected function doDelete() {
159 if ( isset( self::$data[$this->type][$this->domain] ) ) {
160 unset( self::$data[$this->type][$this->domain] );
161 if ( !self::$data[$this->type] ) {
162 unset( self::$data[$this->type] );
168 * @see JobQueue::getAllQueuedJobs
170 * @return Iterator<RunnableJob> of Job objects.
172 public function getAllQueuedJobs() {
173 $unclaimed = $this->getQueueData( 'unclaimed' );
174 if ( !$unclaimed ) {
175 return new ArrayIterator( [] );
178 return new MappedIterator(
179 $unclaimed,
180 function ( $value ) {
181 return $this->jobFromSpecInternal( $value );
187 * @see JobQueue::getAllAcquiredJobs
189 * @return Iterator<RunnableJob> of Job objects.
191 public function getAllAcquiredJobs() {
192 $claimed = $this->getQueueData( 'claimed' );
193 if ( !$claimed ) {
194 return new ArrayIterator( [] );
197 return new MappedIterator(
198 $claimed,
199 function ( $value ) {
200 return $this->jobFromSpecInternal( $value );
206 * @param IJobSpecification $spec
207 * @return RunnableJob
209 public function jobFromSpecInternal( IJobSpecification $spec ) {
210 return $this->factoryJob( $spec->getType(), $spec->getParams() );
214 * @param string $field
215 * @param mixed|null $init
217 * @return mixed
219 private function &getQueueData( $field, $init = null ) {
220 if ( !isset( self::$data[$this->type][$this->domain][$field] ) ) {
221 if ( $init !== null ) {
222 self::$data[$this->type][$this->domain][$field] = $init;
223 } else {
224 return $init;
228 return self::$data[$this->type][$this->domain][$field];