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
21 * @author Aaron Schulz
25 * Class to handle job queues stored in PHP memory for testing
27 * JobQueueGroup does not remember every queue instance, so statically track it here
32 class JobQueueMemory
extends JobQueue
{
34 protected static $data = [];
37 * @see JobQueue::doBatchPush
39 * @param IJobSpecification[] $jobs
42 protected function doBatchPush( array $jobs, $flags ) {
43 $unclaimed =& $this->getQueueData( 'unclaimed', [] );
45 foreach ( $jobs as $job ) {
46 if ( $job->ignoreDuplicates() ) {
47 $sha1 = Wikimedia\base_convert
(
48 sha1( serialize( $job->getDeduplicationInfo() ) ),
51 if ( !isset( $unclaimed[$sha1] ) ) {
52 $unclaimed[$sha1] = $job;
61 * @see JobQueue::supportedOrders
65 protected function supportedOrders() {
66 return [ 'random', 'timestamp', 'fifo' ];
70 * @see JobQueue::optimalOrder
74 protected function optimalOrder() {
79 * @see JobQueue::doIsEmpty
83 protected function doIsEmpty() {
84 return ( $this->doGetSize() == 0 );
88 * @see JobQueue::doGetSize
92 protected function doGetSize() {
93 $unclaimed = $this->getQueueData( 'unclaimed' );
95 return $unclaimed ?
count( $unclaimed ) : 0;
99 * @see JobQueue::doGetAcquiredCount
103 protected function doGetAcquiredCount() {
104 $claimed = $this->getQueueData( 'claimed' );
106 return $claimed ?
count( $claimed ) : 0;
110 * @see JobQueue::doPop
114 protected function doPop() {
115 if ( $this->doGetSize() == 0 ) {
119 $unclaimed =& $this->getQueueData( 'unclaimed' );
120 $claimed =& $this->getQueueData( 'claimed', [] );
122 if ( $this->order
=== 'random' ) {
123 $key = array_rand( $unclaimed );
126 $key = key( $unclaimed );
129 $spec = $unclaimed[$key];
130 unset( $unclaimed[$key] );
133 $job = $this->jobFromSpecInternal( $spec );
136 $job->metadata
['claimId'] = key( $claimed );
142 * @see JobQueue::doAck
146 protected function doAck( Job
$job ) {
147 if ( $this->getAcquiredCount() == 0 ) {
151 $claimed =& $this->getQueueData( 'claimed' );
152 unset( $claimed[$job->metadata
['claimId']] );
156 * @see JobQueue::doDelete
158 protected function doDelete() {
159 if ( isset( self
::$data[$this->type
][$this->wiki
] ) ) {
160 unset( self
::$data[$this->type
][$this->wiki
] );
161 if ( !self
::$data[$this->type
] ) {
162 unset( self
::$data[$this->type
] );
168 * @see JobQueue::getAllQueuedJobs
170 * @return Iterator of Job objects.
172 public function getAllQueuedJobs() {
173 $unclaimed = $this->getQueueData( 'unclaimed' );
175 return new ArrayIterator( [] );
178 return new MappedIterator(
180 function ( $value ) {
181 $this->jobFromSpecInternal( $value );
187 * @see JobQueue::getAllAcquiredJobs
189 * @return Iterator of Job objects.
191 public function getAllAcquiredJobs() {
192 $claimed = $this->getQueueData( 'claimed' );
194 return new ArrayIterator( [] );
197 return new MappedIterator(
199 function ( $value ) {
200 $this->jobFromSpecInternal( $value );
206 * @param IJobSpecification $spec
210 public function jobFromSpecInternal( IJobSpecification
$spec ) {
211 return Job
::factory( $spec->getType(), $spec->getTitle(), $spec->getParams() );
215 * @param string $field
220 private function &getQueueData( $field, $init = null ) {
221 if ( !isset( self
::$data[$this->type
][$this->wiki
][$field] ) ) {
222 if ( $init !== null ) {
223 self
::$data[$this->type
][$this->wiki
][$field] = $init;
229 return self
::$data[$this->type
][$this->wiki
][$field];