7dad748798531bb68016c5410b882dc10c6d674d
[lhc/web/wiklou.git] / includes / jobqueue / JobQueueMemory.php
1 <?php
2 /**
3 * PHP memory-backed job queue code.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @author Aaron Schulz
22 */
23
24 /**
25 * Class to handle job queues stored in PHP memory for testing
26 *
27 * JobQueueGroup does not remember every queue instance, so statically track it here
28 *
29 * @ingroup JobQueue
30 * @since 1.27
31 */
32 class JobQueueMemory extends JobQueue {
33 /** @var array[] */
34 protected static $data = array();
35
36 /**
37 * @see JobQueue::doBatchPush
38 *
39 * @param IJobSpecification[] $jobs
40 * @param int $flags
41 */
42 protected function doBatchPush( array $jobs, $flags ) {
43 $unclaimed =& $this->getQueueData( 'unclaimed', array() );
44
45 foreach ( $jobs as $job ) {
46 if ( $job->ignoreDuplicates() ) {
47 $sha1 = Wikimedia\base_convert(
48 sha1( serialize( $job->getDeduplicationInfo() ) ),
49 16, 36, 31
50 );
51 if ( !isset( $unclaimed[$sha1] ) ) {
52 $unclaimed[$sha1] = $job;
53 }
54 } else {
55 $unclaimed[] = $job;
56 }
57 }
58 }
59
60 /**
61 * @see JobQueue::supportedOrders
62 *
63 * @return string[]
64 */
65 protected function supportedOrders() {
66 return array( 'random', 'timestamp', 'fifo' );
67 }
68
69 /**
70 * @see JobQueue::optimalOrder
71 *
72 * @return string
73 */
74 protected function optimalOrder() {
75 return 'fifo';
76 }
77
78 /**
79 * @see JobQueue::doIsEmpty
80 *
81 * @return bool
82 */
83 protected function doIsEmpty() {
84 return ( $this->doGetSize() == 0 );
85 }
86
87 /**
88 * @see JobQueue::doGetSize
89 *
90 * @return int
91 */
92 protected function doGetSize() {
93 $unclaimed = $this->getQueueData( 'unclaimed' );
94
95 return $unclaimed ? count( $unclaimed ) : 0;
96 }
97
98 /**
99 * @see JobQueue::doGetAcquiredCount
100 *
101 * @return int
102 */
103 protected function doGetAcquiredCount() {
104 $claimed = $this->getQueueData( 'claimed' );
105
106 return $claimed ? count( $claimed ) : 0;
107 }
108
109 /**
110 * @see JobQueue::doPop
111 *
112 * @return Job|bool
113 */
114 protected function doPop() {
115 if ( $this->doGetSize() == 0 ) {
116 return false;
117 }
118
119 $unclaimed =& $this->getQueueData( 'unclaimed' );
120 $claimed =& $this->getQueueData( 'claimed', array() );
121
122 if ( $this->order === 'random' ) {
123 $key = array_rand( $unclaimed );
124 } else {
125 reset( $unclaimed );
126 $key = key( $unclaimed );
127 }
128
129 $spec = $unclaimed[$key];
130 unset( $unclaimed[$key] );
131 $claimed[] = $spec;
132
133 $job = $this->jobFromSpecInternal( $spec );
134
135 end( $claimed );
136 $job->metadata['claimId'] = key( $claimed );
137
138 return $job;
139 }
140
141 /**
142 * @see JobQueue::doAck
143 *
144 * @param Job $job
145 */
146 protected function doAck( Job $job ) {
147 if ( $this->getAcquiredCount() == 0 ) {
148 return;
149 }
150
151 $claimed =& $this->getQueueData( 'claimed' );
152 unset( $claimed[$job->metadata['claimId']] );
153 }
154
155 /**
156 * @see JobQueue::doDelete
157 */
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] );
163 }
164 }
165 }
166
167 /**
168 * @see JobQueue::getAllQueuedJobs
169 *
170 * @return Iterator of Job objects.
171 */
172 public function getAllQueuedJobs() {
173 $unclaimed = $this->getQueueData( 'unclaimed' );
174 if ( !$unclaimed ) {
175 return new ArrayIterator( array() );
176 }
177
178 $that = $this;
179 return new MappedIterator(
180 $unclaimed,
181 function ( $value ) use ( $that ) {
182 $that->jobFromSpecInternal( $value );
183 }
184 );
185 }
186
187 /**
188 * @see JobQueue::getAllAcquiredJobs
189 *
190 * @return Iterator of Job objects.
191 */
192 public function getAllAcquiredJobs() {
193 $claimed = $this->getQueueData( 'claimed' );
194 if ( !$claimed ) {
195 return new ArrayIterator( array() );
196 }
197
198 $that = $this;
199 return new MappedIterator(
200 $claimed,
201 function ( $value ) use ( $that ) {
202 $that->jobFromSpecInternal( $value );
203 }
204 );
205 }
206
207 /**
208 * @param IJobSpecification $spec
209 *
210 * @return Job
211 */
212 public function jobFromSpecInternal( IJobSpecification $spec ) {
213 return Job::factory( $spec->getType(), $spec->getTitle(), $spec->getParams() );
214 }
215
216 /**
217 * @param string $field
218 * @param mixed $init
219 *
220 * @return mixed
221 */
222 private function &getQueueData( $field, $init = null ) {
223 if ( !isset( self::$data[$this->type][$this->wiki][$field] ) ) {
224 if ( $init !== null ) {
225 self::$data[$this->type][$this->wiki][$field] = $init;
226 } else {
227 return $init;
228 }
229 }
230
231 return self::$data[$this->type][$this->wiki][$field];
232 }
233 }