| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
use Wikimedia\ObjectCache\HashBagOStuff; |
| 22 |
use Wikimedia\ObjectCache\WANObjectCache; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
class JobQueueMemory extends JobQueue { |
| 33 |
|
| 34 |
protected static $data = []; |
| 35 |
|
| 36 |
public function __construct( array $params ) { |
| 37 |
$params['wanCache'] = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] ); |
| 38 |
|
| 39 |
parent::__construct( $params ); |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
protected function doBatchPush( array $jobs, $flags ) { |
| 49 |
$unclaimed =& $this->getQueueData( 'unclaimed', [] ); |
| 50 |
|
| 51 |
foreach ( $jobs as $job ) { |
| 52 |
if ( $job->ignoreDuplicates() ) { |
| 53 |
$sha1 = sha1( serialize( $job->getDeduplicationInfo() ) ); |
| 54 |
if ( !isset( $unclaimed[$sha1] ) ) { |
| 55 |
$unclaimed[$sha1] = $job; |
| 56 |
} |
| 57 |
} else { |
| 58 |
$unclaimed[] = $job; |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
protected function supportedOrders() { |
| 69 |
return [ 'random', 'timestamp', 'fifo' ]; |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
protected function optimalOrder() { |
| 78 |
return 'fifo'; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
protected function doIsEmpty() { |
| 87 |
return ( $this->doGetSize() == 0 ); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
protected function doGetSize() { |
| 96 |
$unclaimed = $this->getQueueData( 'unclaimed' ); |
| 97 |
|
| 98 |
return $unclaimed ? count( $unclaimed ) : 0; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
protected function doGetAcquiredCount() { |
| 107 |
$claimed = $this->getQueueData( 'claimed' ); |
| 108 |
|
| 109 |
return $claimed ? count( $claimed ) : 0; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
protected function doPop() { |
| 118 |
if ( $this->doGetSize() == 0 ) { |
| 119 |
return false; |
| 120 |
} |
| 121 |
|
| 122 |
$unclaimed =& $this->getQueueData( 'unclaimed' ); |
| 123 |
$claimed =& $this->getQueueData( 'claimed', [] ); |
| 124 |
|
| 125 |
if ( $this->order === 'random' ) { |
| 126 |
$key = array_rand( $unclaimed ); |
| 127 |
} else { |
| 128 |
$key = array_key_first( $unclaimed ); |
| 129 |
} |
| 130 |
|
| 131 |
$spec = $unclaimed[$key]; |
| 132 |
unset( $unclaimed[$key] ); |
| 133 |
$claimed[] = $spec; |
| 134 |
|
| 135 |
$job = $this->jobFromSpecInternal( $spec ); |
| 136 |
|
| 137 |
$job->setMetadata( 'claimId', array_key_last( $claimed ) ); |
| 138 |
|
| 139 |
return $job; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
protected function doAck( RunnableJob $job ) { |
| 148 |
if ( $this->getAcquiredCount() == 0 ) { |
| 149 |
return; |
| 150 |
} |
| 151 |
|
| 152 |
$claimed =& $this->getQueueData( 'claimed' ); |
| 153 |
unset( $claimed[$job->getMetadata( 'claimId' )] ); |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 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] ); |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
public function getAllQueuedJobs() { |
| 174 |
$unclaimed = $this->getQueueData( 'unclaimed' ); |
| 175 |
if ( !$unclaimed ) { |
| 176 |
return new ArrayIterator( [] ); |
| 177 |
} |
| 178 |
|
| 179 |
return new MappedIterator( |
| 180 |
$unclaimed, |
| 181 |
function ( $value ) { |
| 182 |
return $this->jobFromSpecInternal( $value ); |
| 183 |
} |
| 184 |
); |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
public function getAllAcquiredJobs() { |
| 193 |
$claimed = $this->getQueueData( 'claimed' ); |
| 194 |
if ( !$claimed ) { |
| 195 |
return new ArrayIterator( [] ); |
| 196 |
} |
| 197 |
|
| 198 |
return new MappedIterator( |
| 199 |
$claimed, |
| 200 |
function ( $value ) { |
| 201 |
return $this->jobFromSpecInternal( $value ); |
| 202 |
} |
| 203 |
); |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
public function jobFromSpecInternal( IJobSpecification $spec ) { |
| 211 |
return $this->factoryJob( $spec->getType(), $spec->getParams() ); |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 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; |
| 224 |
} else { |
| 225 |
return $init; |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
return self::$data[$this->type][$this->domain][$field]; |
| 230 |
} |
| 231 |
} |