JobQueue.php 21 KB

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