JobQueue.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. <?php
  2. /**
  3. * Job queue base 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. * @defgroup JobQueue JobQueue
  22. */
  23. use Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface;
  24. /**
  25. * Class to handle enqueueing and running of background jobs
  26. *
  27. * @ingroup JobQueue
  28. * @since 1.21
  29. */
  30. abstract class JobQueue {
  31. /** @var string DB domain ID */
  32. protected $domain;
  33. /** @var string Job type */
  34. protected $type;
  35. /** @var string Job priority for pop() */
  36. protected $order;
  37. /** @var int Time to live in seconds */
  38. protected $claimTTL;
  39. /** @var int Maximum number of times to try a job */
  40. protected $maxTries;
  41. /** @var string|bool Read only rationale (or false if r/w) */
  42. protected $readOnlyReason;
  43. /** @var StatsdDataFactoryInterface */
  44. protected $stats;
  45. /** @var WANObjectCache */
  46. protected $wanCache;
  47. const QOS_ATOMIC = 1; // integer; "all-or-nothing" job insertions
  48. const ROOTJOB_TTL = 2419200; // integer; seconds to remember root jobs (28 days)
  49. /**
  50. * @param array $params
  51. * - type : A job type
  52. * - domain : A DB domain ID
  53. * - wanCache : An instance of WANObjectCache to use for caching [default: none]
  54. * - stats : An instance of StatsdDataFactoryInterface [default: none]
  55. * - claimTTL : Seconds a job can be claimed for exclusive execution [default: forever]
  56. * - maxTries : Total times a job can be tried, assuming claims expire [default: 3]
  57. * - order : Queue order, one of ("fifo", "timestamp", "random") [default: variable]
  58. * - readOnlyReason : Mark the queue as read-only with this reason [default: false]
  59. * @throws JobQueueError
  60. */
  61. protected function __construct( array $params ) {
  62. $this->domain = $params['domain'] ?? $params['wiki']; // b/c
  63. $this->type = $params['type'];
  64. $this->claimTTL = $params['claimTTL'] ?? 0;
  65. $this->maxTries = $params['maxTries'] ?? 3;
  66. if ( isset( $params['order'] ) && $params['order'] !== 'any' ) {
  67. $this->order = $params['order'];
  68. } else {
  69. $this->order = $this->optimalOrder();
  70. }
  71. if ( !in_array( $this->order, $this->supportedOrders() ) ) {
  72. throw new JobQueueError( __CLASS__ . " does not support '{$this->order}' order." );
  73. }
  74. $this->readOnlyReason = $params['readOnlyReason'] ?? false;
  75. $this->stats = $params['stats'] ?? new NullStatsdDataFactory();
  76. $this->wanCache = $params['wanCache'] ?? WANObjectCache::newEmpty();
  77. }
  78. /**
  79. * Get a job queue object of the specified type.
  80. * $params includes:
  81. * - class : What job class to use (determines job type)
  82. * - domain : Database domain ID of the wiki the jobs are for (defaults to current wiki)
  83. * - type : The name of the job types this queue handles
  84. * - order : Order that pop() selects jobs, one of "fifo", "timestamp" or "random".
  85. * If "fifo" is used, the queue will effectively be FIFO. Note that job
  86. * completion will not appear to be exactly FIFO if there are multiple
  87. * job runners since jobs can take different times to finish once popped.
  88. * If "timestamp" is used, the queue will at least be loosely ordered
  89. * by timestamp, allowing for some jobs to be popped off out of order.
  90. * If "random" is used, pop() will pick jobs in random order.
  91. * Note that it may only be weakly random (e.g. a lottery of the oldest X).
  92. * If "any" is choosen, the queue will use whatever order is the fastest.
  93. * This might be useful for improving concurrency for job acquisition.
  94. * - claimTTL : If supported, the queue will recycle jobs that have been popped
  95. * but not acknowledged as completed after this many seconds. Recycling
  96. * of jobs simply means re-inserting them into the queue. Jobs can be
  97. * attempted up to three times before being discarded.
  98. * - readOnlyReason : Set this to a string to make the queue read-only.
  99. * - stash : A BagOStuff instance that can be used for root job deduplication
  100. * - stats : A StatsdDataFactoryInterface [optional]
  101. *
  102. * Queue classes should throw an exception if they do not support the options given.
  103. *
  104. * @param array $params
  105. * @return JobQueue
  106. * @throws JobQueueError
  107. */
  108. final public static function factory( array $params ) {
  109. $class = $params['class'];
  110. if ( !class_exists( $class ) ) {
  111. throw new JobQueueError( "Invalid job queue class '$class'." );
  112. }
  113. $obj = new $class( $params );
  114. if ( !( $obj instanceof self ) ) {
  115. throw new JobQueueError( "Class '$class' is not a " . __CLASS__ . " class." );
  116. }
  117. return $obj;
  118. }
  119. /**
  120. * @return string Database domain ID
  121. */
  122. final public function getDomain() {
  123. return $this->domain;
  124. }
  125. /**
  126. * @return string Wiki ID
  127. * @deprecated 1.33
  128. */
  129. final public function getWiki() {
  130. return WikiMap::getWikiIdFromDbDomain( $this->domain );
  131. }
  132. /**
  133. * @return string Job type that this queue handles
  134. */
  135. final public function getType() {
  136. return $this->type;
  137. }
  138. /**
  139. * @return string One of (random, timestamp, fifo, undefined)
  140. */
  141. final public function getOrder() {
  142. return $this->order;
  143. }
  144. /**
  145. * Get the allowed queue orders for configuration validation
  146. *
  147. * @return array Subset of (random, timestamp, fifo, undefined)
  148. */
  149. abstract protected function supportedOrders();
  150. /**
  151. * Get the default queue order to use if configuration does not specify one
  152. *
  153. * @return string One of (random, timestamp, fifo, undefined)
  154. */
  155. abstract protected function optimalOrder();
  156. /**
  157. * Find out if delayed jobs are supported for configuration validation
  158. *
  159. * @return bool Whether delayed jobs are supported
  160. */
  161. protected function supportsDelayedJobs() {
  162. return false; // not implemented
  163. }
  164. /**
  165. * @return bool Whether delayed jobs are enabled
  166. * @since 1.22
  167. */
  168. final public function delayedJobsEnabled() {
  169. return $this->supportsDelayedJobs();
  170. }
  171. /**
  172. * @return string|bool Read-only rational or false if r/w
  173. * @since 1.27
  174. */
  175. public function getReadOnlyReason() {
  176. return $this->readOnlyReason;
  177. }
  178. /**
  179. * Quickly check if the queue has no available (unacquired, non-delayed) jobs.
  180. * Queue classes should use caching if they are any slower without memcached.
  181. *
  182. * If caching is used, this might return false when there are actually no jobs.
  183. * If pop() is called and returns false then it should correct the cache. Also,
  184. * calling flushCaches() first prevents this. However, this affect is typically
  185. * not distinguishable from the race condition between isEmpty() and pop().
  186. *
  187. * @return bool
  188. * @throws JobQueueError
  189. */
  190. final public function isEmpty() {
  191. $res = $this->doIsEmpty();
  192. return $res;
  193. }
  194. /**
  195. * @see JobQueue::isEmpty()
  196. * @return bool
  197. */
  198. abstract protected function doIsEmpty();
  199. /**
  200. * Get the number of available (unacquired, non-delayed) jobs in the queue.
  201. * Queue classes should use caching if they are any slower without memcached.
  202. *
  203. * If caching is used, this number might be out of date for a minute.
  204. *
  205. * @return int
  206. * @throws JobQueueError
  207. */
  208. final public function getSize() {
  209. $res = $this->doGetSize();
  210. return $res;
  211. }
  212. /**
  213. * @see JobQueue::getSize()
  214. * @return int
  215. */
  216. abstract protected function doGetSize();
  217. /**
  218. * Get the number of acquired jobs (these are temporarily out of the queue).
  219. * Queue classes should use caching if they are any slower without memcached.
  220. *
  221. * If caching is used, this number might be out of date for a minute.
  222. *
  223. * @return int
  224. * @throws JobQueueError
  225. */
  226. final public function getAcquiredCount() {
  227. $res = $this->doGetAcquiredCount();
  228. return $res;
  229. }
  230. /**
  231. * @see JobQueue::getAcquiredCount()
  232. * @return int
  233. */
  234. abstract protected function doGetAcquiredCount();
  235. /**
  236. * Get the number of delayed jobs (these are temporarily out of the queue).
  237. * Queue classes should use caching if they are any slower without memcached.
  238. *
  239. * If caching is used, this number might be out of date for a minute.
  240. *
  241. * @return int
  242. * @throws JobQueueError
  243. * @since 1.22
  244. */
  245. final public function getDelayedCount() {
  246. $res = $this->doGetDelayedCount();
  247. return $res;
  248. }
  249. /**
  250. * @see JobQueue::getDelayedCount()
  251. * @return int
  252. */
  253. protected function doGetDelayedCount() {
  254. return 0; // not implemented
  255. }
  256. /**
  257. * Get the number of acquired jobs that can no longer be attempted.
  258. * Queue classes should use caching if they are any slower without memcached.
  259. *
  260. * If caching is used, this number might be out of date for a minute.
  261. *
  262. * @return int
  263. * @throws JobQueueError
  264. */
  265. final public function getAbandonedCount() {
  266. $res = $this->doGetAbandonedCount();
  267. return $res;
  268. }
  269. /**
  270. * @see JobQueue::getAbandonedCount()
  271. * @return int
  272. */
  273. protected function doGetAbandonedCount() {
  274. return 0; // not implemented
  275. }
  276. /**
  277. * Push one or more jobs into the queue.
  278. * This does not require $wgJobClasses to be set for the given job type.
  279. * Outside callers should use JobQueueGroup::push() instead of this function.
  280. *
  281. * @param IJobSpecification|IJobSpecification[] $jobs
  282. * @param int $flags Bitfield (supports JobQueue::QOS_ATOMIC)
  283. * @return void
  284. * @throws JobQueueError
  285. */
  286. final public function push( $jobs, $flags = 0 ) {
  287. $jobs = is_array( $jobs ) ? $jobs : [ $jobs ];
  288. $this->batchPush( $jobs, $flags );
  289. }
  290. /**
  291. * Push a batch of jobs into the queue.
  292. * This does not require $wgJobClasses to be set for the given job type.
  293. * Outside callers should use JobQueueGroup::push() instead of this function.
  294. *
  295. * @param IJobSpecification[] $jobs
  296. * @param int $flags Bitfield (supports JobQueue::QOS_ATOMIC)
  297. * @return void
  298. * @throws JobQueueError
  299. */
  300. final public function batchPush( array $jobs, $flags = 0 ) {
  301. $this->assertNotReadOnly();
  302. if ( $jobs === [] ) {
  303. return; // nothing to do
  304. }
  305. foreach ( $jobs as $job ) {
  306. if ( $job->getType() !== $this->type ) {
  307. throw new JobQueueError(
  308. "Got '{$job->getType()}' job; expected a '{$this->type}' job." );
  309. } elseif ( $job->getReleaseTimestamp() && !$this->supportsDelayedJobs() ) {
  310. throw new JobQueueError(
  311. "Got delayed '{$job->getType()}' job; delays are not supported." );
  312. }
  313. }
  314. $this->doBatchPush( $jobs, $flags );
  315. foreach ( $jobs as $job ) {
  316. if ( $job->isRootJob() ) {
  317. $this->deduplicateRootJob( $job );
  318. }
  319. }
  320. }
  321. /**
  322. * @see JobQueue::batchPush()
  323. * @param IJobSpecification[] $jobs
  324. * @param int $flags
  325. */
  326. abstract protected function doBatchPush( array $jobs, $flags );
  327. /**
  328. * Pop a job off of the queue.
  329. * This requires $wgJobClasses to be set for the given job type.
  330. * Outside callers should use JobQueueGroup::pop() instead of this function.
  331. *
  332. * @throws JobQueueError
  333. * @return RunnableJob|bool Returns false if there are no jobs
  334. */
  335. final public function pop() {
  336. $this->assertNotReadOnly();
  337. $job = $this->doPop();
  338. // Flag this job as an old duplicate based on its "root" job...
  339. try {
  340. if ( $job && $this->isRootJobOldDuplicate( $job ) ) {
  341. $this->incrStats( 'dupe_pops', $this->type );
  342. $job = DuplicateJob::newFromJob( $job ); // convert to a no-op
  343. }
  344. } catch ( Exception $e ) {
  345. // don't lose jobs over this
  346. }
  347. return $job;
  348. }
  349. /**
  350. * @see JobQueue::pop()
  351. * @return RunnableJob|bool
  352. */
  353. abstract protected function doPop();
  354. /**
  355. * Acknowledge that a job was completed.
  356. *
  357. * This does nothing for certain queue classes or if "claimTTL" is not set.
  358. * Outside callers should use JobQueueGroup::ack() instead of this function.
  359. *
  360. * @param RunnableJob $job
  361. * @return void
  362. * @throws JobQueueError
  363. */
  364. final public function ack( RunnableJob $job ) {
  365. $this->assertNotReadOnly();
  366. if ( $job->getType() !== $this->type ) {
  367. throw new JobQueueError( "Got '{$job->getType()}' job; expected '{$this->type}'." );
  368. }
  369. $this->doAck( $job );
  370. }
  371. /**
  372. * @see JobQueue::ack()
  373. * @param RunnableJob $job
  374. */
  375. abstract protected function doAck( RunnableJob $job );
  376. /**
  377. * Register the "root job" of a given job into the queue for de-duplication.
  378. * This should only be called right *after* all the new jobs have been inserted.
  379. * This is used to turn older, duplicate, job entries into no-ops. The root job
  380. * information will remain in the registry until it simply falls out of cache.
  381. *
  382. * This requires that $job has two special fields in the "params" array:
  383. * - rootJobSignature : hash (e.g. SHA1) that identifies the task
  384. * - rootJobTimestamp : TS_MW timestamp of this instance of the task
  385. *
  386. * A "root job" is a conceptual job that consist of potentially many smaller jobs
  387. * that are actually inserted into the queue. For example, "refreshLinks" jobs are
  388. * spawned when a template is edited. One can think of the task as "update links
  389. * of pages that use template X" and an instance of that task as a "root job".
  390. * However, what actually goes into the queue are range and leaf job subtypes.
  391. * Since these jobs include things like page ID ranges and DB master positions,
  392. * and can morph into smaller jobs recursively, simple duplicate detection
  393. * for individual jobs being identical (like that of job_sha1) is not useful.
  394. *
  395. * In the case of "refreshLinks", if these jobs are still in the queue when the template
  396. * is edited again, we want all of these old refreshLinks jobs for that template to become
  397. * no-ops. This can greatly reduce server load, since refreshLinks jobs involves parsing.
  398. * Essentially, the new batch of jobs belong to a new "root job" and the older ones to a
  399. * previous "root job" for the same task of "update links of pages that use template X".
  400. *
  401. * This does nothing for certain queue classes.
  402. *
  403. * @param IJobSpecification $job
  404. * @throws JobQueueError
  405. * @return bool
  406. */
  407. final public function deduplicateRootJob( IJobSpecification $job ) {
  408. $this->assertNotReadOnly();
  409. if ( $job->getType() !== $this->type ) {
  410. throw new JobQueueError( "Got '{$job->getType()}' job; expected '{$this->type}'." );
  411. }
  412. return $this->doDeduplicateRootJob( $job );
  413. }
  414. /**
  415. * @see JobQueue::deduplicateRootJob()
  416. * @param IJobSpecification $job
  417. * @throws JobQueueError
  418. * @return bool
  419. */
  420. protected function doDeduplicateRootJob( IJobSpecification $job ) {
  421. $params = $job->hasRootJobParams() ? $job->getRootJobParams() : null;
  422. if ( !$params ) {
  423. throw new JobQueueError( "Cannot register root job; missing parameters." );
  424. }
  425. $key = $this->getRootJobCacheKey( $params['rootJobSignature'] );
  426. // Callers should call JobQueueGroup::push() before this method so that if the
  427. // insert fails, the de-duplication registration will be aborted. Having only the
  428. // de-duplication registration succeed would cause jobs to become no-ops without
  429. // any actual jobs that made them redundant.
  430. $timestamp = $this->wanCache->get( $key ); // last known timestamp of such a root job
  431. if ( $timestamp !== false && $timestamp >= $params['rootJobTimestamp'] ) {
  432. return true; // a newer version of this root job was enqueued
  433. }
  434. // Update the timestamp of the last root job started at the location...
  435. return $this->wanCache->set( $key, $params['rootJobTimestamp'], self::ROOTJOB_TTL );
  436. }
  437. /**
  438. * Check if the "root" job of a given job has been superseded by a newer one
  439. *
  440. * @param IJobSpecification $job
  441. * @throws JobQueueError
  442. * @return bool
  443. */
  444. final protected function isRootJobOldDuplicate( IJobSpecification $job ) {
  445. if ( $job->getType() !== $this->type ) {
  446. throw new JobQueueError( "Got '{$job->getType()}' job; expected '{$this->type}'." );
  447. }
  448. return $this->doIsRootJobOldDuplicate( $job );
  449. }
  450. /**
  451. * @see JobQueue::isRootJobOldDuplicate()
  452. * @param IJobSpecification $job
  453. * @return bool
  454. */
  455. protected function doIsRootJobOldDuplicate( IJobSpecification $job ) {
  456. $params = $job->hasRootJobParams() ? $job->getRootJobParams() : null;
  457. if ( !$params ) {
  458. return false; // job has no de-deplication info
  459. }
  460. $key = $this->getRootJobCacheKey( $params['rootJobSignature'] );
  461. // Get the last time this root job was enqueued
  462. $timestamp = $this->wanCache->get( $key );
  463. if ( $timestamp === false || $params['rootJobTimestamp'] > $timestamp ) {
  464. // Update the timestamp of the last known root job started at the location...
  465. $this->wanCache->set( $key, $params['rootJobTimestamp'], self::ROOTJOB_TTL );
  466. }
  467. // Check if a new root job was started at the location after this one's...
  468. return ( $timestamp && $timestamp > $params['rootJobTimestamp'] );
  469. }
  470. /**
  471. * @param string $signature Hash identifier of the root job
  472. * @return string
  473. */
  474. protected function getRootJobCacheKey( $signature ) {
  475. return $this->wanCache->makeGlobalKey(
  476. 'jobqueue',
  477. $this->domain,
  478. $this->type,
  479. 'rootjob',
  480. $signature
  481. );
  482. }
  483. /**
  484. * Deleted all unclaimed and delayed jobs from the queue
  485. *
  486. * @throws JobQueueError
  487. * @since 1.22
  488. * @return void
  489. */
  490. final public function delete() {
  491. $this->assertNotReadOnly();
  492. $this->doDelete();
  493. }
  494. /**
  495. * @see JobQueue::delete()
  496. * @throws JobQueueError
  497. */
  498. protected function doDelete() {
  499. throw new JobQueueError( "This method is not implemented." );
  500. }
  501. /**
  502. * Wait for any replica DBs or backup servers to catch up.
  503. *
  504. * This does nothing for certain queue classes.
  505. *
  506. * @return void
  507. * @throws JobQueueError
  508. */
  509. final public function waitForBackups() {
  510. $this->doWaitForBackups();
  511. }
  512. /**
  513. * @see JobQueue::waitForBackups()
  514. * @return void
  515. */
  516. protected function doWaitForBackups() {
  517. }
  518. /**
  519. * Clear any process and persistent caches
  520. *
  521. * @return void
  522. */
  523. final public function flushCaches() {
  524. $this->doFlushCaches();
  525. }
  526. /**
  527. * @see JobQueue::flushCaches()
  528. * @return void
  529. */
  530. protected function doFlushCaches() {
  531. }
  532. /**
  533. * Get an iterator to traverse over all available jobs in this queue.
  534. * This does not include jobs that are currently acquired or delayed.
  535. * Note: results may be stale if the queue is concurrently modified.
  536. *
  537. * @return Iterator
  538. * @throws JobQueueError
  539. */
  540. abstract public function getAllQueuedJobs();
  541. /**
  542. * Get an iterator to traverse over all delayed jobs in this queue.
  543. * Note: results may be stale if the queue is concurrently modified.
  544. *
  545. * @return Iterator
  546. * @throws JobQueueError
  547. * @since 1.22
  548. */
  549. public function getAllDelayedJobs() {
  550. return new ArrayIterator( [] ); // not implemented
  551. }
  552. /**
  553. * Get an iterator to traverse over all claimed jobs in this queue
  554. *
  555. * Callers should be quick to iterator over it or few results
  556. * will be returned due to jobs being acknowledged and deleted
  557. *
  558. * @return Iterator
  559. * @throws JobQueueError
  560. * @since 1.26
  561. */
  562. public function getAllAcquiredJobs() {
  563. return new ArrayIterator( [] ); // not implemented
  564. }
  565. /**
  566. * Get an iterator to traverse over all abandoned jobs in this queue
  567. *
  568. * @return Iterator
  569. * @throws JobQueueError
  570. * @since 1.25
  571. */
  572. public function getAllAbandonedJobs() {
  573. return new ArrayIterator( [] ); // not implemented
  574. }
  575. /**
  576. * Do not use this function outside of JobQueue/JobQueueGroup
  577. *
  578. * @return string
  579. * @since 1.22
  580. */
  581. public function getCoalesceLocationInternal() {
  582. return null;
  583. }
  584. /**
  585. * Check whether each of the given queues are empty.
  586. * This is used for batching checks for queues stored at the same place.
  587. *
  588. * @param array $types List of queues types
  589. * @return array|null (list of non-empty queue types) or null if unsupported
  590. * @throws JobQueueError
  591. * @since 1.22
  592. */
  593. final public function getSiblingQueuesWithJobs( array $types ) {
  594. return $this->doGetSiblingQueuesWithJobs( $types );
  595. }
  596. /**
  597. * @see JobQueue::getSiblingQueuesWithJobs()
  598. * @param array $types List of queues types
  599. * @return array|null (list of queue types) or null if unsupported
  600. */
  601. protected function doGetSiblingQueuesWithJobs( array $types ) {
  602. return null; // not supported
  603. }
  604. /**
  605. * Check the size of each of the given queues.
  606. * For queues not served by the same store as this one, 0 is returned.
  607. * This is used for batching checks for queues stored at the same place.
  608. *
  609. * @param array $types List of queues types
  610. * @return array|null (job type => whether queue is empty) or null if unsupported
  611. * @throws JobQueueError
  612. * @since 1.22
  613. */
  614. final public function getSiblingQueueSizes( array $types ) {
  615. return $this->doGetSiblingQueueSizes( $types );
  616. }
  617. /**
  618. * @see JobQueue::getSiblingQueuesSize()
  619. * @param array $types List of queues types
  620. * @return array|null (list of queue types) or null if unsupported
  621. */
  622. protected function doGetSiblingQueueSizes( array $types ) {
  623. return null; // not supported
  624. }
  625. /**
  626. * @param string $command
  627. * @param array $params
  628. * @return Job
  629. */
  630. protected function factoryJob( $command, $params ) {
  631. // @TODO: dependency inject this as a callback
  632. return Job::factory( $command, $params );
  633. }
  634. /**
  635. * @throws JobQueueReadOnlyError
  636. */
  637. protected function assertNotReadOnly() {
  638. if ( $this->readOnlyReason !== false ) {
  639. throw new JobQueueReadOnlyError( "Job queue is read-only: {$this->readOnlyReason}" );
  640. }
  641. }
  642. /**
  643. * Call wfIncrStats() for the queue overall and for the queue type
  644. *
  645. * @param string $key Event type
  646. * @param string $type Job type
  647. * @param int $delta
  648. * @since 1.22
  649. */
  650. protected function incrStats( $key, $type, $delta = 1 ) {
  651. $this->stats->updateCount( "jobqueue.{$key}.all", $delta );
  652. $this->stats->updateCount( "jobqueue.{$key}.{$type}", $delta );
  653. }
  654. }