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
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
31 class JobQueueMemory
extends JobQueue
{
33 protected static $data = [];
36 * @see JobQueue::doBatchPush
38 * @param IJobSpecification[] $jobs
41 protected function doBatchPush( array $jobs, $flags ) {
42 $unclaimed =& $this->getQueueData( 'unclaimed', [] );
44 foreach ( $jobs as $job ) {
45 if ( $job->ignoreDuplicates() ) {
46 $sha1 = Wikimedia\base_convert
(
47 sha1( serialize( $job->getDeduplicationInfo() ) ),
50 if ( !isset( $unclaimed[$sha1] ) ) {
51 $unclaimed[$sha1] = $job;
60 * @see JobQueue::supportedOrders
64 protected function supportedOrders() {
65 return [ 'random', 'timestamp', 'fifo' ];
69 * @see JobQueue::optimalOrder
73 protected function optimalOrder() {
78 * @see JobQueue::doIsEmpty
82 protected function doIsEmpty() {
83 return ( $this->doGetSize() == 0 );
87 * @see JobQueue::doGetSize
91 protected function doGetSize() {
92 $unclaimed = $this->getQueueData( 'unclaimed' );
94 return $unclaimed ?
count( $unclaimed ) : 0;
98 * @see JobQueue::doGetAcquiredCount
102 protected function doGetAcquiredCount() {
103 $claimed = $this->getQueueData( 'claimed' );
105 return $claimed ?
count( $claimed ) : 0;
109 * @see JobQueue::doPop
113 protected function doPop() {
114 if ( $this->doGetSize() == 0 ) {
118 $unclaimed =& $this->getQueueData( 'unclaimed' );
119 $claimed =& $this->getQueueData( 'claimed', [] );
121 if ( $this->order
=== 'random' ) {
122 $key = array_rand( $unclaimed );
125 $key = key( $unclaimed );
128 $spec = $unclaimed[$key];
129 unset( $unclaimed[$key] );
132 $job = $this->jobFromSpecInternal( $spec );
135 $job->metadata
['claimId'] = key( $claimed );
141 * @see JobQueue::doAck
145 protected function doAck( Job
$job ) {
146 if ( $this->getAcquiredCount() == 0 ) {
150 $claimed =& $this->getQueueData( 'claimed' );
151 unset( $claimed[$job->metadata
['claimId']] );
155 * @see JobQueue::doDelete
157 protected function doDelete() {
158 if ( isset( self
::$data[$this->type
][$this->wiki
] ) ) {
159 unset( self
::$data[$this->type
][$this->wiki
] );
160 if ( !self
::$data[$this->type
] ) {
161 unset( self
::$data[$this->type
] );
167 * @see JobQueue::getAllQueuedJobs
169 * @return Iterator of Job objects.
171 public function getAllQueuedJobs() {
172 $unclaimed = $this->getQueueData( 'unclaimed' );
174 return new ArrayIterator( [] );
177 return new MappedIterator(
179 function ( $value ) {
180 $this->jobFromSpecInternal( $value );
186 * @see JobQueue::getAllAcquiredJobs
188 * @return Iterator of Job objects.
190 public function getAllAcquiredJobs() {
191 $claimed = $this->getQueueData( 'claimed' );
193 return new ArrayIterator( [] );
196 return new MappedIterator(
198 function ( $value ) {
199 $this->jobFromSpecInternal( $value );
205 * @param IJobSpecification $spec
209 public function jobFromSpecInternal( IJobSpecification
$spec ) {
210 return Job
::factory( $spec->getType(), $spec->getTitle(), $spec->getParams() );
214 * @param string $field
219 private function &getQueueData( $field, $init = null ) {
220 if ( !isset( self
::$data[$this->type
][$this->wiki
][$field] ) ) {
221 if ( $init !== null ) {
222 self
::$data[$this->type
][$this->wiki
][$field] = $init;
228 return self
::$data[$this->type
][$this->wiki
][$field];