JobQueueMemory.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. */
  22. /**
  23. * Class to handle job queues stored in PHP memory for testing
  24. *
  25. * JobQueueGroup does not remember every queue instance, so statically track it here
  26. *
  27. * @ingroup JobQueue
  28. * @since 1.27
  29. */
  30. class JobQueueMemory extends JobQueue {
  31. /** @var array[] */
  32. protected static $data = [];
  33. public function __construct( array $params ) {
  34. $params['wanCache'] = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
  35. parent::__construct( $params );
  36. }
  37. /**
  38. * @see JobQueue::doBatchPush
  39. *
  40. * @param IJobSpecification[] $jobs
  41. * @param int $flags
  42. */
  43. protected function doBatchPush( array $jobs, $flags ) {
  44. $unclaimed =& $this->getQueueData( 'unclaimed', [] );
  45. foreach ( $jobs as $job ) {
  46. if ( $job->ignoreDuplicates() ) {
  47. $sha1 = sha1( serialize( $job->getDeduplicationInfo() ) );
  48. if ( !isset( $unclaimed[$sha1] ) ) {
  49. $unclaimed[$sha1] = $job;
  50. }
  51. } else {
  52. $unclaimed[] = $job;
  53. }
  54. }
  55. }
  56. /**
  57. * @see JobQueue::supportedOrders
  58. *
  59. * @return string[]
  60. */
  61. protected function supportedOrders() {
  62. return [ 'random', 'timestamp', 'fifo' ];
  63. }
  64. /**
  65. * @see JobQueue::optimalOrder
  66. *
  67. * @return string
  68. */
  69. protected function optimalOrder() {
  70. return 'fifo';
  71. }
  72. /**
  73. * @see JobQueue::doIsEmpty
  74. *
  75. * @return bool
  76. */
  77. protected function doIsEmpty() {
  78. return ( $this->doGetSize() == 0 );
  79. }
  80. /**
  81. * @see JobQueue::doGetSize
  82. *
  83. * @return int
  84. */
  85. protected function doGetSize() {
  86. $unclaimed = $this->getQueueData( 'unclaimed' );
  87. return $unclaimed ? count( $unclaimed ) : 0;
  88. }
  89. /**
  90. * @see JobQueue::doGetAcquiredCount
  91. *
  92. * @return int
  93. */
  94. protected function doGetAcquiredCount() {
  95. $claimed = $this->getQueueData( 'claimed' );
  96. return $claimed ? count( $claimed ) : 0;
  97. }
  98. /**
  99. * @see JobQueue::doPop
  100. *
  101. * @return RunnableJob|bool
  102. */
  103. protected function doPop() {
  104. if ( $this->doGetSize() == 0 ) {
  105. return false;
  106. }
  107. $unclaimed =& $this->getQueueData( 'unclaimed' );
  108. $claimed =& $this->getQueueData( 'claimed', [] );
  109. if ( $this->order === 'random' ) {
  110. $key = array_rand( $unclaimed );
  111. } else {
  112. reset( $unclaimed );
  113. $key = key( $unclaimed );
  114. }
  115. $spec = $unclaimed[$key];
  116. unset( $unclaimed[$key] );
  117. $claimed[] = $spec;
  118. $job = $this->jobFromSpecInternal( $spec );
  119. end( $claimed );
  120. $job->setMetadata( 'claimId', key( $claimed ) );
  121. return $job;
  122. }
  123. /**
  124. * @see JobQueue::doAck
  125. *
  126. * @param RunnableJob $job
  127. */
  128. protected function doAck( RunnableJob $job ) {
  129. if ( $this->getAcquiredCount() == 0 ) {
  130. return;
  131. }
  132. $claimed =& $this->getQueueData( 'claimed' );
  133. unset( $claimed[$job->getMetadata( 'claimId' )] );
  134. }
  135. /**
  136. * @see JobQueue::doDelete
  137. */
  138. protected function doDelete() {
  139. if ( isset( self::$data[$this->type][$this->domain] ) ) {
  140. unset( self::$data[$this->type][$this->domain] );
  141. if ( !self::$data[$this->type] ) {
  142. unset( self::$data[$this->type] );
  143. }
  144. }
  145. }
  146. /**
  147. * @see JobQueue::getAllQueuedJobs
  148. *
  149. * @return Iterator of Job objects.
  150. */
  151. public function getAllQueuedJobs() {
  152. $unclaimed = $this->getQueueData( 'unclaimed' );
  153. if ( !$unclaimed ) {
  154. return new ArrayIterator( [] );
  155. }
  156. return new MappedIterator(
  157. $unclaimed,
  158. function ( $value ) {
  159. return $this->jobFromSpecInternal( $value );
  160. }
  161. );
  162. }
  163. /**
  164. * @see JobQueue::getAllAcquiredJobs
  165. *
  166. * @return Iterator of Job objects.
  167. */
  168. public function getAllAcquiredJobs() {
  169. $claimed = $this->getQueueData( 'claimed' );
  170. if ( !$claimed ) {
  171. return new ArrayIterator( [] );
  172. }
  173. return new MappedIterator(
  174. $claimed,
  175. function ( $value ) {
  176. return $this->jobFromSpecInternal( $value );
  177. }
  178. );
  179. }
  180. /**
  181. * @param IJobSpecification $spec
  182. * @return RunnableJob
  183. */
  184. public function jobFromSpecInternal( IJobSpecification $spec ) {
  185. return $this->factoryJob( $spec->getType(), $spec->getParams() );
  186. }
  187. /**
  188. * @param string $field
  189. * @param mixed $init
  190. *
  191. * @return mixed
  192. */
  193. private function &getQueueData( $field, $init = null ) {
  194. if ( !isset( self::$data[$this->type][$this->domain][$field] ) ) {
  195. if ( $init !== null ) {
  196. self::$data[$this->type][$this->domain][$field] = $init;
  197. } else {
  198. return $init;
  199. }
  200. }
  201. return self::$data[$this->type][$this->domain][$field];
  202. }
  203. }