3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
21 use Wikimedia\ObjectCache\HashBagOStuff
;
22 use Wikimedia\ObjectCache\WANObjectCache
;
25 * PHP memory-backed job queue storage, for testing.
27 * JobQueueGroup does not remember every queue instance, so statically track it here.
32 class JobQueueMemory
extends JobQueue
{
34 protected static $data = [];
36 public function __construct( array $params ) {
37 $params['wanCache'] = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
39 parent
::__construct( $params );
43 * @see JobQueue::doBatchPush
45 * @param IJobSpecification[] $jobs
48 protected function doBatchPush( array $jobs, $flags ) {
49 $unclaimed =& $this->getQueueData( 'unclaimed', [] );
51 foreach ( $jobs as $job ) {
52 if ( $job->ignoreDuplicates() ) {
53 $sha1 = sha1( serialize( $job->getDeduplicationInfo() ) );
54 if ( !isset( $unclaimed[$sha1] ) ) {
55 $unclaimed[$sha1] = $job;
64 * @see JobQueue::supportedOrders
68 protected function supportedOrders() {
69 return [ 'random', 'timestamp', 'fifo' ];
73 * @see JobQueue::optimalOrder
77 protected function optimalOrder() {
82 * @see JobQueue::doIsEmpty
86 protected function doIsEmpty() {
87 return ( $this->doGetSize() == 0 );
91 * @see JobQueue::doGetSize
95 protected function doGetSize() {
96 $unclaimed = $this->getQueueData( 'unclaimed' );
98 return $unclaimed ?
count( $unclaimed ) : 0;
102 * @see JobQueue::doGetAcquiredCount
106 protected function doGetAcquiredCount() {
107 $claimed = $this->getQueueData( 'claimed' );
109 return $claimed ?
count( $claimed ) : 0;
113 * @see JobQueue::doPop
115 * @return RunnableJob|false
117 protected function doPop() {
118 if ( $this->doGetSize() == 0 ) {
122 $unclaimed =& $this->getQueueData( 'unclaimed' );
123 $claimed =& $this->getQueueData( 'claimed', [] );
125 if ( $this->order
=== 'random' ) {
126 $key = array_rand( $unclaimed );
128 $key = array_key_first( $unclaimed );
131 $spec = $unclaimed[$key];
132 unset( $unclaimed[$key] );
135 $job = $this->jobFromSpecInternal( $spec );
137 $job->setMetadata( 'claimId', array_key_last( $claimed ) );
143 * @see JobQueue::doAck
145 * @param RunnableJob $job
147 protected function doAck( RunnableJob
$job ) {
148 if ( $this->getAcquiredCount() == 0 ) {
152 $claimed =& $this->getQueueData( 'claimed' );
153 unset( $claimed[$job->getMetadata( 'claimId' )] );
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
] );
169 * @see JobQueue::getAllQueuedJobs
171 * @return Iterator<RunnableJob> of Job objects.
173 public function getAllQueuedJobs() {
174 $unclaimed = $this->getQueueData( 'unclaimed' );
176 return new ArrayIterator( [] );
179 return new MappedIterator(
181 function ( $value ) {
182 return $this->jobFromSpecInternal( $value );
188 * @see JobQueue::getAllAcquiredJobs
190 * @return Iterator<RunnableJob> of Job objects.
192 public function getAllAcquiredJobs() {
193 $claimed = $this->getQueueData( 'claimed' );
195 return new ArrayIterator( [] );
198 return new MappedIterator(
200 function ( $value ) {
201 return $this->jobFromSpecInternal( $value );
207 * @param IJobSpecification $spec
208 * @return RunnableJob
210 public function jobFromSpecInternal( IJobSpecification
$spec ) {
211 return $this->factoryJob( $spec->getType(), $spec->getParams() );
215 * @param string $field
216 * @param mixed|null $init
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;
229 return self
::$data[$this->type
][$this->domain
][$field];