JobQueueMemory.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. /**
  34. * @see JobQueue::doBatchPush
  35. *
  36. * @param IJobSpecification[] $jobs
  37. * @param int $flags
  38. */
  39. protected function doBatchPush( array $jobs, $flags ) {
  40. $unclaimed =& $this->getQueueData( 'unclaimed', [] );
  41. foreach ( $jobs as $job ) {
  42. if ( $job->ignoreDuplicates() ) {
  43. $sha1 = Wikimedia\base_convert(
  44. sha1( serialize( $job->getDeduplicationInfo() ) ),
  45. 16, 36, 31
  46. );
  47. if ( !isset( $unclaimed[$sha1] ) ) {
  48. $unclaimed[$sha1] = $job;
  49. }
  50. } else {
  51. $unclaimed[] = $job;
  52. }
  53. }
  54. }
  55. /**
  56. * @see JobQueue::supportedOrders
  57. *
  58. * @return string[]
  59. */
  60. protected function supportedOrders() {
  61. return [ 'random', 'timestamp', 'fifo' ];
  62. }
  63. /**
  64. * @see JobQueue::optimalOrder
  65. *
  66. * @return string
  67. */
  68. protected function optimalOrder() {
  69. return 'fifo';
  70. }
  71. /**
  72. * @see JobQueue::doIsEmpty
  73. *
  74. * @return bool
  75. */
  76. protected function doIsEmpty() {
  77. return ( $this->doGetSize() == 0 );
  78. }
  79. /**
  80. * @see JobQueue::doGetSize
  81. *
  82. * @return int
  83. */
  84. protected function doGetSize() {
  85. $unclaimed = $this->getQueueData( 'unclaimed' );
  86. return $unclaimed ? count( $unclaimed ) : 0;
  87. }
  88. /**
  89. * @see JobQueue::doGetAcquiredCount
  90. *
  91. * @return int
  92. */
  93. protected function doGetAcquiredCount() {
  94. $claimed = $this->getQueueData( 'claimed' );
  95. return $claimed ? count( $claimed ) : 0;
  96. }
  97. /**
  98. * @see JobQueue::doPop
  99. *
  100. * @return Job|bool
  101. */
  102. protected function doPop() {
  103. if ( $this->doGetSize() == 0 ) {
  104. return false;
  105. }
  106. $unclaimed =& $this->getQueueData( 'unclaimed' );
  107. $claimed =& $this->getQueueData( 'claimed', [] );
  108. if ( $this->order === 'random' ) {
  109. $key = array_rand( $unclaimed );
  110. } else {
  111. reset( $unclaimed );
  112. $key = key( $unclaimed );
  113. }
  114. $spec = $unclaimed[$key];
  115. unset( $unclaimed[$key] );
  116. $claimed[] = $spec;
  117. $job = $this->jobFromSpecInternal( $spec );
  118. end( $claimed );
  119. $job->metadata['claimId'] = key( $claimed );
  120. return $job;
  121. }
  122. /**
  123. * @see JobQueue::doAck
  124. *
  125. * @param Job $job
  126. */
  127. protected function doAck( Job $job ) {
  128. if ( $this->getAcquiredCount() == 0 ) {
  129. return;
  130. }
  131. $claimed =& $this->getQueueData( 'claimed' );
  132. unset( $claimed[$job->metadata['claimId']] );
  133. }
  134. /**
  135. * @see JobQueue::doDelete
  136. */
  137. protected function doDelete() {
  138. if ( isset( self::$data[$this->type][$this->wiki] ) ) {
  139. unset( self::$data[$this->type][$this->wiki] );
  140. if ( !self::$data[$this->type] ) {
  141. unset( self::$data[$this->type] );
  142. }
  143. }
  144. }
  145. /**
  146. * @see JobQueue::getAllQueuedJobs
  147. *
  148. * @return Iterator of Job objects.
  149. */
  150. public function getAllQueuedJobs() {
  151. $unclaimed = $this->getQueueData( 'unclaimed' );
  152. if ( !$unclaimed ) {
  153. return new ArrayIterator( [] );
  154. }
  155. return new MappedIterator(
  156. $unclaimed,
  157. function ( $value ) {
  158. return $this->jobFromSpecInternal( $value );
  159. }
  160. );
  161. }
  162. /**
  163. * @see JobQueue::getAllAcquiredJobs
  164. *
  165. * @return Iterator of Job objects.
  166. */
  167. public function getAllAcquiredJobs() {
  168. $claimed = $this->getQueueData( 'claimed' );
  169. if ( !$claimed ) {
  170. return new ArrayIterator( [] );
  171. }
  172. return new MappedIterator(
  173. $claimed,
  174. function ( $value ) {
  175. return $this->jobFromSpecInternal( $value );
  176. }
  177. );
  178. }
  179. /**
  180. * @param IJobSpecification $spec
  181. *
  182. * @return Job
  183. */
  184. public function jobFromSpecInternal( IJobSpecification $spec ) {
  185. return Job::factory( $spec->getType(), $spec->getTitle(), $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->wiki][$field] ) ) {
  195. if ( $init !== null ) {
  196. self::$data[$this->type][$this->wiki][$field] = $init;
  197. } else {
  198. return $init;
  199. }
  200. }
  201. return self::$data[$this->type][$this->wiki][$field];
  202. }
  203. }