c5d72574683ca30f402f5951ef9de07ef0a6ec54
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 = array();
36 protected function doBatchPush( array $jobs, $flags ) {
37 $unclaimed =& $this->getQueueData( 'unclaimed', array() );
39 /** @var IJobSpecification[] $jobs */
40 foreach ( $jobs as $job ) {
41 if ( $job->ignoreDuplicates() ) {
42 $sha1 = Wikimedia\base_convert
(
43 sha1( serialize( $job->getDeduplicationInfo() ) ),
46 if ( !isset( $unclaimed[$sha1] ) ) {
47 $unclaimed[$sha1] = $job;
55 protected function supportedOrders() {
56 return array( 'random', 'timestamp', 'fifo' );
59 protected function optimalOrder() {
60 return array( 'fifo' );
63 protected function doIsEmpty() {
64 return ( $this->doGetSize() == 0 );
67 protected function doGetSize() {
68 $unclaimed = $this->getQueueData( 'unclaimed' );
70 return $unclaimed ?
count( $unclaimed ) : 0;
73 protected function doGetAcquiredCount() {
74 $claimed = $this->getQueueData( 'claimed' );
76 return $claimed ?
count( $claimed ) : 0;
79 protected function doPop() {
80 if ( $this->doGetSize() == 0 ) {
84 $unclaimed =& $this->getQueueData( 'unclaimed' );
85 $claimed =& $this->getQueueData( 'claimed', array() );
87 if ( $this->order
=== 'random' ) {
88 $key = array_rand( $unclaimed );
91 $key = key( $unclaimed );
94 $spec = $unclaimed[$key];
95 unset( $unclaimed[$key] );
98 $job = $this->jobFromSpecInternal( $spec );
101 $job->metadata
['claimId'] = key( $claimed );
106 protected function doAck( Job
$job ) {
107 if ( $this->getAcquiredCount() == 0 ) {
111 $claimed =& $this->getQueueData( 'claimed' );
112 unset( $claimed[$job->metadata
['claimId']] );
115 protected function doDelete() {
116 if ( isset( self
::$data[$this->type
][$this->wiki
] ) ) {
117 unset( self
::$data[$this->type
][$this->wiki
] );
118 if ( !self
::$data[$this->type
] ) {
119 unset( self
::$data[$this->type
] );
124 public function getAllQueuedJobs() {
125 $unclaimed = $this->getQueueData( 'unclaimed' );
127 return new ArrayIterator( array() );
131 return new MappedIterator(
133 function ( $value ) use ( $that ) {
134 $that->jobFromSpecInternal( $value );
139 public function getAllAcquiredJobs() {
140 $claimed = $this->getQueueData( 'claimed' );
142 return new ArrayIterator( array() );
146 return new MappedIterator(
148 function ( $value ) use ( $that ) {
149 $that->jobFromSpecInternal( $value );
154 public function jobFromSpecInternal( IJobSpecification
$spec ) {
155 return Job
::factory( $spec->getType(), $spec->getTitle(), $spec->getParams() );
158 private function &getQueueData( $field, $init = null ) {
159 if ( !isset( self
::$data[$this->type
][$this->wiki
][$field] ) ) {
160 if ( $init !== null ) {
161 self
::$data[$this->type
][$this->wiki
][$field] = $init;
167 return self
::$data[$this->type
][$this->wiki
][$field];