2012-08-29 00:01:31 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Job queue base code.
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
* @defgroup JobQueue JobQueue
|
|
|
|
|
*/
|
2017-03-17 10:57:37 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2012-08-29 00:01:31 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class to handle enqueueing and running of background jobs
|
|
|
|
|
*
|
|
|
|
|
* @ingroup JobQueue
|
2012-11-06 00:43:44 +00:00
|
|
|
* @since 1.21
|
2012-08-29 00:01:31 +00:00
|
|
|
*/
|
|
|
|
|
abstract class JobQueue {
|
2013-11-25 17:12:42 +00:00
|
|
|
/** @var string Wiki ID */
|
2018-10-13 07:29:23 +00:00
|
|
|
protected $domain;
|
2013-11-25 17:12:42 +00:00
|
|
|
/** @var string Job type */
|
|
|
|
|
protected $type;
|
|
|
|
|
/** @var string Job priority for pop() */
|
|
|
|
|
protected $order;
|
|
|
|
|
/** @var int Time to live in seconds */
|
|
|
|
|
protected $claimTTL;
|
|
|
|
|
/** @var int Maximum number of times to try a job */
|
|
|
|
|
protected $maxTries;
|
2016-03-25 19:52:39 +00:00
|
|
|
/** @var string|bool Read only rationale (or false if r/w) */
|
|
|
|
|
protected $readOnlyReason;
|
2013-11-25 17:12:42 +00:00
|
|
|
|
2013-10-08 02:30:10 +00:00
|
|
|
/** @var BagOStuff */
|
|
|
|
|
protected $dupCache;
|
2015-02-16 23:34:53 +00:00
|
|
|
/** @var JobQueueAggregator */
|
|
|
|
|
protected $aggr;
|
2013-10-08 02:30:10 +00:00
|
|
|
|
2013-03-28 17:40:21 +00:00
|
|
|
const QOS_ATOMIC = 1; // integer; "all-or-nothing" job insertions
|
2012-08-29 00:01:31 +00:00
|
|
|
|
2013-02-28 22:21:28 +00:00
|
|
|
const ROOTJOB_TTL = 2419200; // integer; seconds to remember root jobs (28 days)
|
|
|
|
|
|
2012-08-29 00:01:31 +00:00
|
|
|
/**
|
2013-11-25 17:12:42 +00:00
|
|
|
* @param array $params
|
|
|
|
|
* @throws MWException
|
2012-08-29 00:01:31 +00:00
|
|
|
*/
|
|
|
|
|
protected function __construct( array $params ) {
|
2018-10-13 07:29:23 +00:00
|
|
|
$this->domain = $params['domain'] ?? $params['wiki']; // b/c
|
2013-03-07 16:50:43 +00:00
|
|
|
$this->type = $params['type'];
|
2017-10-06 22:17:58 +00:00
|
|
|
$this->claimTTL = $params['claimTTL'] ?? 0;
|
|
|
|
|
$this->maxTries = $params['maxTries'] ?? 3;
|
2013-03-03 04:41:38 +00:00
|
|
|
if ( isset( $params['order'] ) && $params['order'] !== 'any' ) {
|
|
|
|
|
$this->order = $params['order'];
|
|
|
|
|
} else {
|
|
|
|
|
$this->order = $this->optimalOrder();
|
|
|
|
|
}
|
|
|
|
|
if ( !in_array( $this->order, $this->supportedOrders() ) ) {
|
|
|
|
|
throw new MWException( __CLASS__ . " does not support '{$this->order}' order." );
|
|
|
|
|
}
|
2013-10-08 02:30:10 +00:00
|
|
|
$this->dupCache = wfGetCache( CACHE_ANYTHING );
|
2017-10-06 22:17:58 +00:00
|
|
|
$this->aggr = $params['aggregator'] ?? new JobQueueAggregatorNull( [] );
|
|
|
|
|
$this->readOnlyReason = $params['readOnlyReason'] ?? false;
|
2012-08-29 00:01:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a job queue object of the specified type.
|
|
|
|
|
* $params includes:
|
2013-03-12 03:40:01 +00:00
|
|
|
* - class : What job class to use (determines job type)
|
|
|
|
|
* - wiki : wiki ID of the wiki the jobs are for (defaults to current wiki)
|
|
|
|
|
* - type : The name of the job types this queue handles
|
|
|
|
|
* - order : Order that pop() selects jobs, one of "fifo", "timestamp" or "random".
|
|
|
|
|
* If "fifo" is used, the queue will effectively be FIFO. Note that job
|
|
|
|
|
* completion will not appear to be exactly FIFO if there are multiple
|
|
|
|
|
* job runners since jobs can take different times to finish once popped.
|
|
|
|
|
* If "timestamp" is used, the queue will at least be loosely ordered
|
|
|
|
|
* by timestamp, allowing for some jobs to be popped off out of order.
|
|
|
|
|
* If "random" is used, pop() will pick jobs in random order.
|
|
|
|
|
* Note that it may only be weakly random (e.g. a lottery of the oldest X).
|
|
|
|
|
* If "any" is choosen, the queue will use whatever order is the fastest.
|
|
|
|
|
* This might be useful for improving concurrency for job acquisition.
|
|
|
|
|
* - claimTTL : If supported, the queue will recycle jobs that have been popped
|
|
|
|
|
* but not acknowledged as completed after this many seconds. Recycling
|
2015-07-09 16:52:07 +00:00
|
|
|
* of jobs simply means re-inserting them into the queue. Jobs can be
|
2013-03-12 03:40:01 +00:00
|
|
|
* attempted up to three times before being discarded.
|
2016-03-25 19:52:39 +00:00
|
|
|
* - readOnlyReason : Set this to a string to make the queue read-only.
|
2012-08-29 00:01:31 +00:00
|
|
|
*
|
2013-01-02 20:12:59 +00:00
|
|
|
* Queue classes should throw an exception if they do not support the options given.
|
|
|
|
|
*
|
2013-11-25 17:12:42 +00:00
|
|
|
* @param array $params
|
2012-08-29 00:01:31 +00:00
|
|
|
* @return JobQueue
|
|
|
|
|
* @throws MWException
|
|
|
|
|
*/
|
|
|
|
|
final public static function factory( array $params ) {
|
|
|
|
|
$class = $params['class'];
|
2013-05-08 06:48:56 +00:00
|
|
|
if ( !class_exists( $class ) ) {
|
2012-08-29 00:01:31 +00:00
|
|
|
throw new MWException( "Invalid job queue class '$class'." );
|
|
|
|
|
}
|
|
|
|
|
$obj = new $class( $params );
|
|
|
|
|
if ( !( $obj instanceof self ) ) {
|
|
|
|
|
throw new MWException( "Class '$class' is not a " . __CLASS__ . " class." );
|
|
|
|
|
}
|
2013-11-25 14:38:37 +00:00
|
|
|
|
2012-08-29 00:01:31 +00:00
|
|
|
return $obj;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string Wiki ID
|
|
|
|
|
*/
|
2018-10-13 07:29:23 +00:00
|
|
|
final public function getDomain() {
|
|
|
|
|
return $this->domain;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string Wiki ID
|
|
|
|
|
* @deprecated 1.33
|
|
|
|
|
*/
|
2012-08-29 00:01:31 +00:00
|
|
|
final public function getWiki() {
|
2018-10-13 07:29:23 +00:00
|
|
|
return $this->domain;
|
2012-08-29 00:01:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string Job type that this queue handles
|
|
|
|
|
*/
|
|
|
|
|
final public function getType() {
|
|
|
|
|
return $this->type;
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-03 04:41:38 +00:00
|
|
|
/**
|
2013-02-28 22:21:28 +00:00
|
|
|
* @return string One of (random, timestamp, fifo, undefined)
|
2013-03-03 04:41:38 +00:00
|
|
|
*/
|
|
|
|
|
final public function getOrder() {
|
|
|
|
|
return $this->order;
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-25 21:45:49 +00:00
|
|
|
/**
|
|
|
|
|
* Get the allowed queue orders for configuration validation
|
|
|
|
|
*
|
2013-11-25 17:12:42 +00:00
|
|
|
* @return array Subset of (random, timestamp, fifo, undefined)
|
2013-03-03 04:41:38 +00:00
|
|
|
*/
|
2013-03-08 18:20:54 +00:00
|
|
|
abstract protected function supportedOrders();
|
2013-03-03 04:41:38 +00:00
|
|
|
|
|
|
|
|
/**
|
2013-04-25 21:45:49 +00:00
|
|
|
* Get the default queue order to use if configuration does not specify one
|
|
|
|
|
*
|
2013-02-28 22:21:28 +00:00
|
|
|
* @return string One of (random, timestamp, fifo, undefined)
|
2013-03-03 04:41:38 +00:00
|
|
|
*/
|
|
|
|
|
abstract protected function optimalOrder();
|
|
|
|
|
|
2012-08-29 00:01:31 +00:00
|
|
|
/**
|
2013-04-25 21:45:49 +00:00
|
|
|
* Find out if delayed jobs are supported for configuration validation
|
|
|
|
|
*
|
2013-11-25 17:12:42 +00:00
|
|
|
* @return bool Whether delayed jobs are supported
|
2013-03-12 03:40:01 +00:00
|
|
|
*/
|
|
|
|
|
protected function supportsDelayedJobs() {
|
|
|
|
|
return false; // not implemented
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-11 18:12:50 +00:00
|
|
|
/**
|
|
|
|
|
* @return bool Whether delayed jobs are enabled
|
|
|
|
|
* @since 1.22
|
|
|
|
|
*/
|
|
|
|
|
final public function delayedJobsEnabled() {
|
|
|
|
|
return $this->supportsDelayedJobs();
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-25 19:52:39 +00:00
|
|
|
/**
|
|
|
|
|
* @return string|bool Read-only rational or false if r/w
|
|
|
|
|
* @since 1.27
|
|
|
|
|
*/
|
|
|
|
|
public function getReadOnlyReason() {
|
|
|
|
|
return $this->readOnlyReason;
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-12 03:40:01 +00:00
|
|
|
/**
|
|
|
|
|
* Quickly check if the queue has no available (unacquired, non-delayed) jobs.
|
2012-11-03 00:31:25 +00:00
|
|
|
* Queue classes should use caching if they are any slower without memcached.
|
|
|
|
|
*
|
2013-03-09 21:00:16 +00:00
|
|
|
* If caching is used, this might return false when there are actually no jobs.
|
|
|
|
|
* If pop() is called and returns false then it should correct the cache. Also,
|
|
|
|
|
* calling flushCaches() first prevents this. However, this affect is typically
|
|
|
|
|
* not distinguishable from the race condition between isEmpty() and pop().
|
|
|
|
|
*
|
2012-11-03 00:31:25 +00:00
|
|
|
* @return bool
|
2013-06-19 03:09:10 +00:00
|
|
|
* @throws JobQueueError
|
2012-08-29 00:01:31 +00:00
|
|
|
*/
|
|
|
|
|
final public function isEmpty() {
|
|
|
|
|
$res = $this->doIsEmpty();
|
2013-11-25 14:38:37 +00:00
|
|
|
|
2012-08-29 00:01:31 +00:00
|
|
|
return $res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @see JobQueue::isEmpty()
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
abstract protected function doIsEmpty();
|
|
|
|
|
|
2012-12-14 18:47:06 +00:00
|
|
|
/**
|
2013-03-12 03:40:01 +00:00
|
|
|
* Get the number of available (unacquired, non-delayed) jobs in the queue.
|
2012-12-14 18:47:06 +00:00
|
|
|
* Queue classes should use caching if they are any slower without memcached.
|
|
|
|
|
*
|
2013-03-09 21:00:16 +00:00
|
|
|
* If caching is used, this number might be out of date for a minute.
|
|
|
|
|
*
|
2013-11-25 17:12:42 +00:00
|
|
|
* @return int
|
2013-06-19 03:09:10 +00:00
|
|
|
* @throws JobQueueError
|
2012-12-14 18:47:06 +00:00
|
|
|
*/
|
|
|
|
|
final public function getSize() {
|
|
|
|
|
$res = $this->doGetSize();
|
2013-11-25 14:38:37 +00:00
|
|
|
|
2012-12-14 18:47:06 +00:00
|
|
|
return $res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @see JobQueue::getSize()
|
2013-11-25 17:12:42 +00:00
|
|
|
* @return int
|
2012-12-14 18:47:06 +00:00
|
|
|
*/
|
|
|
|
|
abstract protected function doGetSize();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the number of acquired jobs (these are temporarily out of the queue).
|
|
|
|
|
* Queue classes should use caching if they are any slower without memcached.
|
|
|
|
|
*
|
2013-03-09 21:00:16 +00:00
|
|
|
* If caching is used, this number might be out of date for a minute.
|
|
|
|
|
*
|
2013-11-25 17:12:42 +00:00
|
|
|
* @return int
|
2013-06-19 03:09:10 +00:00
|
|
|
* @throws JobQueueError
|
2012-12-14 18:47:06 +00:00
|
|
|
*/
|
|
|
|
|
final public function getAcquiredCount() {
|
|
|
|
|
$res = $this->doGetAcquiredCount();
|
2013-11-25 14:38:37 +00:00
|
|
|
|
2012-12-14 18:47:06 +00:00
|
|
|
return $res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @see JobQueue::getAcquiredCount()
|
2013-11-25 17:12:42 +00:00
|
|
|
* @return int
|
2012-12-14 18:47:06 +00:00
|
|
|
*/
|
|
|
|
|
abstract protected function doGetAcquiredCount();
|
|
|
|
|
|
2013-03-12 03:40:01 +00:00
|
|
|
/**
|
|
|
|
|
* Get the number of delayed jobs (these are temporarily out of the queue).
|
|
|
|
|
* Queue classes should use caching if they are any slower without memcached.
|
|
|
|
|
*
|
|
|
|
|
* If caching is used, this number might be out of date for a minute.
|
|
|
|
|
*
|
2013-11-25 17:12:42 +00:00
|
|
|
* @return int
|
2013-06-19 03:09:10 +00:00
|
|
|
* @throws JobQueueError
|
2013-03-12 03:40:01 +00:00
|
|
|
* @since 1.22
|
|
|
|
|
*/
|
|
|
|
|
final public function getDelayedCount() {
|
|
|
|
|
$res = $this->doGetDelayedCount();
|
2013-11-25 14:38:37 +00:00
|
|
|
|
2013-03-12 03:40:01 +00:00
|
|
|
return $res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @see JobQueue::getDelayedCount()
|
2013-11-25 17:12:42 +00:00
|
|
|
* @return int
|
2013-03-12 03:40:01 +00:00
|
|
|
*/
|
|
|
|
|
protected function doGetDelayedCount() {
|
|
|
|
|
return 0; // not implemented
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-20 19:40:09 +00:00
|
|
|
/**
|
|
|
|
|
* Get the number of acquired jobs that can no longer be attempted.
|
|
|
|
|
* Queue classes should use caching if they are any slower without memcached.
|
|
|
|
|
*
|
|
|
|
|
* If caching is used, this number might be out of date for a minute.
|
|
|
|
|
*
|
2013-11-25 17:12:42 +00:00
|
|
|
* @return int
|
2013-06-19 03:09:10 +00:00
|
|
|
* @throws JobQueueError
|
2013-03-20 19:40:09 +00:00
|
|
|
*/
|
|
|
|
|
final public function getAbandonedCount() {
|
|
|
|
|
$res = $this->doGetAbandonedCount();
|
2013-11-25 14:38:37 +00:00
|
|
|
|
2013-03-20 19:40:09 +00:00
|
|
|
return $res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @see JobQueue::getAbandonedCount()
|
2013-11-25 17:12:42 +00:00
|
|
|
* @return int
|
2013-03-20 19:40:09 +00:00
|
|
|
*/
|
|
|
|
|
protected function doGetAbandonedCount() {
|
|
|
|
|
return 0; // not implemented
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-11 23:00:20 +00:00
|
|
|
/**
|
2014-01-24 19:14:11 +00:00
|
|
|
* Push one or more jobs into the queue.
|
2013-01-11 23:00:20 +00:00
|
|
|
* This does not require $wgJobClasses to be set for the given job type.
|
2013-02-21 19:45:51 +00:00
|
|
|
* Outside callers should use JobQueueGroup::push() instead of this function.
|
2013-01-11 23:00:20 +00:00
|
|
|
*
|
2016-01-15 09:31:07 +00:00
|
|
|
* @param IJobSpecification|IJobSpecification[] $jobs
|
2013-11-25 17:12:42 +00:00
|
|
|
* @param int $flags Bitfield (supports JobQueue::QOS_ATOMIC)
|
2014-04-16 17:51:11 +00:00
|
|
|
* @return void
|
2013-06-19 03:09:10 +00:00
|
|
|
* @throws JobQueueError
|
2013-01-11 23:00:20 +00:00
|
|
|
*/
|
|
|
|
|
final public function push( $jobs, $flags = 0 ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$jobs = is_array( $jobs ) ? $jobs : [ $jobs ];
|
2015-02-16 23:34:53 +00:00
|
|
|
$this->batchPush( $jobs, $flags );
|
2013-01-11 23:00:20 +00:00
|
|
|
}
|
|
|
|
|
|
2012-08-29 00:01:31 +00:00
|
|
|
/**
|
2013-01-10 21:16:23 +00:00
|
|
|
* Push a batch of jobs into the queue.
|
|
|
|
|
* This does not require $wgJobClasses to be set for the given job type.
|
2013-02-21 19:45:51 +00:00
|
|
|
* Outside callers should use JobQueueGroup::push() instead of this function.
|
2012-08-29 00:01:31 +00:00
|
|
|
*
|
2016-01-15 09:31:07 +00:00
|
|
|
* @param IJobSpecification[] $jobs
|
2013-11-25 17:12:42 +00:00
|
|
|
* @param int $flags Bitfield (supports JobQueue::QOS_ATOMIC)
|
2014-04-16 17:51:11 +00:00
|
|
|
* @return void
|
2013-11-25 17:12:42 +00:00
|
|
|
* @throws MWException
|
2012-08-29 00:01:31 +00:00
|
|
|
*/
|
|
|
|
|
final public function batchPush( array $jobs, $flags = 0 ) {
|
2016-03-25 19:52:39 +00:00
|
|
|
$this->assertNotReadOnly();
|
|
|
|
|
|
2013-02-05 20:00:24 +00:00
|
|
|
if ( !count( $jobs ) ) {
|
2014-04-16 18:07:26 +00:00
|
|
|
return; // nothing to do
|
2013-02-05 20:00:24 +00:00
|
|
|
}
|
2013-03-09 21:00:16 +00:00
|
|
|
|
2012-08-29 00:01:31 +00:00
|
|
|
foreach ( $jobs as $job ) {
|
|
|
|
|
if ( $job->getType() !== $this->type ) {
|
2013-03-12 03:40:01 +00:00
|
|
|
throw new MWException(
|
|
|
|
|
"Got '{$job->getType()}' job; expected a '{$this->type}' job." );
|
2015-03-11 18:12:50 +00:00
|
|
|
} elseif ( $job->getReleaseTimestamp() && !$this->supportsDelayedJobs() ) {
|
2013-03-12 03:40:01 +00:00
|
|
|
throw new MWException(
|
|
|
|
|
"Got delayed '{$job->getType()}' job; delays are not supported." );
|
2012-08-29 00:01:31 +00:00
|
|
|
}
|
|
|
|
|
}
|
2013-02-05 20:00:24 +00:00
|
|
|
|
2014-04-16 17:51:11 +00:00
|
|
|
$this->doBatchPush( $jobs, $flags );
|
2018-10-13 07:29:23 +00:00
|
|
|
$this->aggr->notifyQueueNonEmpty( $this->domain, $this->type );
|
2015-05-23 17:53:12 +00:00
|
|
|
|
|
|
|
|
foreach ( $jobs as $job ) {
|
|
|
|
|
if ( $job->isRootJob() ) {
|
|
|
|
|
$this->deduplicateRootJob( $job );
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-08-29 00:01:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @see JobQueue::batchPush()
|
2016-01-15 09:31:07 +00:00
|
|
|
* @param IJobSpecification[] $jobs
|
2014-04-16 18:07:26 +00:00
|
|
|
* @param int $flags
|
2012-08-29 00:01:31 +00:00
|
|
|
*/
|
|
|
|
|
abstract protected function doBatchPush( array $jobs, $flags );
|
|
|
|
|
|
|
|
|
|
/**
|
2013-01-10 21:16:23 +00:00
|
|
|
* Pop a job off of the queue.
|
|
|
|
|
* This requires $wgJobClasses to be set for the given job type.
|
2013-02-21 19:45:51 +00:00
|
|
|
* Outside callers should use JobQueueGroup::pop() instead of this function.
|
2012-08-29 00:01:31 +00:00
|
|
|
*
|
2013-11-25 17:12:42 +00:00
|
|
|
* @throws MWException
|
2013-02-05 20:00:24 +00:00
|
|
|
* @return Job|bool Returns false if there are no jobs
|
2012-08-29 00:01:31 +00:00
|
|
|
*/
|
|
|
|
|
final public function pop() {
|
2013-02-03 22:36:48 +00:00
|
|
|
global $wgJobClasses;
|
|
|
|
|
|
2016-03-25 19:52:39 +00:00
|
|
|
$this->assertNotReadOnly();
|
2018-10-13 07:29:23 +00:00
|
|
|
if ( !WikiMap::isCurrentWikiDomain( $this->domain ) ) {
|
|
|
|
|
throw new MWException(
|
|
|
|
|
"Cannot pop '{$this->type}' job off foreign '{$this->domain}' wiki queue." );
|
2013-02-03 22:36:48 +00:00
|
|
|
} elseif ( !isset( $wgJobClasses[$this->type] ) ) {
|
|
|
|
|
// Do not pop jobs if there is no class for the queue type
|
|
|
|
|
throw new MWException( "Unrecognized job type '{$this->type}'." );
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-29 00:01:31 +00:00
|
|
|
$job = $this->doPop();
|
2013-02-28 22:21:28 +00:00
|
|
|
|
2015-02-16 23:34:53 +00:00
|
|
|
if ( !$job ) {
|
2018-10-13 07:29:23 +00:00
|
|
|
$this->aggr->notifyQueueEmpty( $this->domain, $this->type );
|
2015-02-16 23:34:53 +00:00
|
|
|
}
|
|
|
|
|
|
2013-02-28 22:21:28 +00:00
|
|
|
// Flag this job as an old duplicate based on its "root" job...
|
|
|
|
|
try {
|
|
|
|
|
if ( $job && $this->isRootJobOldDuplicate( $job ) ) {
|
2017-07-23 01:24:09 +00:00
|
|
|
self::incrStats( 'dupe_pops', $this->type );
|
2013-02-28 22:21:28 +00:00
|
|
|
$job = DuplicateJob::newFromJob( $job ); // convert to a no-op
|
|
|
|
|
}
|
2015-01-09 23:44:47 +00:00
|
|
|
} catch ( Exception $e ) {
|
2013-11-25 14:38:37 +00:00
|
|
|
// don't lose jobs over this
|
|
|
|
|
}
|
2013-02-28 22:21:28 +00:00
|
|
|
|
2012-08-29 00:01:31 +00:00
|
|
|
return $job;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @see JobQueue::pop()
|
2016-01-15 09:31:07 +00:00
|
|
|
* @return Job|bool
|
2012-08-29 00:01:31 +00:00
|
|
|
*/
|
|
|
|
|
abstract protected function doPop();
|
|
|
|
|
|
|
|
|
|
/**
|
2013-01-02 20:12:59 +00:00
|
|
|
* Acknowledge that a job was completed.
|
|
|
|
|
*
|
|
|
|
|
* This does nothing for certain queue classes or if "claimTTL" is not set.
|
2013-02-21 19:45:51 +00:00
|
|
|
* Outside callers should use JobQueueGroup::ack() instead of this function.
|
2012-08-29 00:01:31 +00:00
|
|
|
*
|
2013-11-25 17:12:42 +00:00
|
|
|
* @param Job $job
|
2014-04-16 17:51:11 +00:00
|
|
|
* @return void
|
2013-11-25 17:12:42 +00:00
|
|
|
* @throws MWException
|
2012-08-29 00:01:31 +00:00
|
|
|
*/
|
|
|
|
|
final public function ack( Job $job ) {
|
2016-03-25 19:52:39 +00:00
|
|
|
$this->assertNotReadOnly();
|
2012-08-29 00:01:31 +00:00
|
|
|
if ( $job->getType() !== $this->type ) {
|
|
|
|
|
throw new MWException( "Got '{$job->getType()}' job; expected '{$this->type}'." );
|
|
|
|
|
}
|
2016-03-25 19:52:39 +00:00
|
|
|
|
2014-04-16 17:51:11 +00:00
|
|
|
$this->doAck( $job );
|
2012-08-29 00:01:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @see JobQueue::ack()
|
2013-11-25 17:12:42 +00:00
|
|
|
* @param Job $job
|
2012-08-29 00:01:31 +00:00
|
|
|
*/
|
|
|
|
|
abstract protected function doAck( Job $job );
|
|
|
|
|
|
2012-11-08 22:01:40 +00:00
|
|
|
/**
|
|
|
|
|
* Register the "root job" of a given job into the queue for de-duplication.
|
|
|
|
|
* This should only be called right *after* all the new jobs have been inserted.
|
|
|
|
|
* This is used to turn older, duplicate, job entries into no-ops. The root job
|
|
|
|
|
* information will remain in the registry until it simply falls out of cache.
|
|
|
|
|
*
|
|
|
|
|
* This requires that $job has two special fields in the "params" array:
|
|
|
|
|
* - rootJobSignature : hash (e.g. SHA1) that identifies the task
|
|
|
|
|
* - rootJobTimestamp : TS_MW timestamp of this instance of the task
|
|
|
|
|
*
|
|
|
|
|
* A "root job" is a conceptual job that consist of potentially many smaller jobs
|
|
|
|
|
* that are actually inserted into the queue. For example, "refreshLinks" jobs are
|
|
|
|
|
* spawned when a template is edited. One can think of the task as "update links
|
|
|
|
|
* of pages that use template X" and an instance of that task as a "root job".
|
2014-02-28 05:48:14 +00:00
|
|
|
* However, what actually goes into the queue are range and leaf job subtypes.
|
|
|
|
|
* Since these jobs include things like page ID ranges and DB master positions,
|
|
|
|
|
* and can morph into smaller jobs recursively, simple duplicate detection
|
|
|
|
|
* for individual jobs being identical (like that of job_sha1) is not useful.
|
2012-11-08 22:01:40 +00:00
|
|
|
*
|
|
|
|
|
* In the case of "refreshLinks", if these jobs are still in the queue when the template
|
|
|
|
|
* is edited again, we want all of these old refreshLinks jobs for that template to become
|
|
|
|
|
* no-ops. This can greatly reduce server load, since refreshLinks jobs involves parsing.
|
|
|
|
|
* Essentially, the new batch of jobs belong to a new "root job" and the older ones to a
|
|
|
|
|
* previous "root job" for the same task of "update links of pages that use template X".
|
|
|
|
|
*
|
2013-01-02 20:12:59 +00:00
|
|
|
* This does nothing for certain queue classes.
|
|
|
|
|
*
|
2015-05-23 19:28:35 +00:00
|
|
|
* @param IJobSpecification $job
|
2013-11-25 17:12:42 +00:00
|
|
|
* @throws MWException
|
2012-11-08 22:01:40 +00:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
2015-05-23 19:28:35 +00:00
|
|
|
final public function deduplicateRootJob( IJobSpecification $job ) {
|
2016-03-25 19:52:39 +00:00
|
|
|
$this->assertNotReadOnly();
|
2012-11-08 22:01:40 +00:00
|
|
|
if ( $job->getType() !== $this->type ) {
|
|
|
|
|
throw new MWException( "Got '{$job->getType()}' job; expected '{$this->type}'." );
|
|
|
|
|
}
|
2013-11-25 14:38:37 +00:00
|
|
|
|
2016-03-25 19:52:39 +00:00
|
|
|
return $this->doDeduplicateRootJob( $job );
|
2012-11-08 22:01:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @see JobQueue::deduplicateRootJob()
|
2015-05-23 19:28:35 +00:00
|
|
|
* @param IJobSpecification $job
|
2013-11-25 17:12:42 +00:00
|
|
|
* @throws MWException
|
2012-11-08 22:01:40 +00:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
2015-05-23 19:28:35 +00:00
|
|
|
protected function doDeduplicateRootJob( IJobSpecification $job ) {
|
2013-04-03 20:51:02 +00:00
|
|
|
if ( !$job->hasRootJobParams() ) {
|
|
|
|
|
throw new MWException( "Cannot register root job; missing parameters." );
|
2013-02-28 22:21:28 +00:00
|
|
|
}
|
2013-04-03 20:51:02 +00:00
|
|
|
$params = $job->getRootJobParams();
|
|
|
|
|
|
2013-02-28 22:21:28 +00:00
|
|
|
$key = $this->getRootJobCacheKey( $params['rootJobSignature'] );
|
2018-05-20 12:10:20 +00:00
|
|
|
// Callers should call JobQueueGroup::push() before this method so that if the insert
|
2013-02-28 22:21:28 +00:00
|
|
|
// fails, the de-duplication registration will be aborted. Since the insert is
|
|
|
|
|
// deferred till "transaction idle", do the same here, so that the ordering is
|
|
|
|
|
// maintained. Having only the de-duplication registration succeed would cause
|
|
|
|
|
// jobs to become no-ops without any actual jobs that made them redundant.
|
2013-10-08 02:30:10 +00:00
|
|
|
$timestamp = $this->dupCache->get( $key ); // current last timestamp of this job
|
2013-02-28 22:21:28 +00:00
|
|
|
if ( $timestamp && $timestamp >= $params['rootJobTimestamp'] ) {
|
|
|
|
|
return true; // a newer version of this root job was enqueued
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update the timestamp of the last root job started at the location...
|
2013-10-08 02:30:10 +00:00
|
|
|
return $this->dupCache->set( $key, $params['rootJobTimestamp'], JobQueueDB::ROOTJOB_TTL );
|
2013-02-28 22:21:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if the "root" job of a given job has been superseded by a newer one
|
|
|
|
|
*
|
2013-11-25 17:12:42 +00:00
|
|
|
* @param Job $job
|
|
|
|
|
* @throws MWException
|
2013-02-28 22:21:28 +00:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
final protected function isRootJobOldDuplicate( Job $job ) {
|
|
|
|
|
if ( $job->getType() !== $this->type ) {
|
|
|
|
|
throw new MWException( "Got '{$job->getType()}' job; expected '{$this->type}'." );
|
|
|
|
|
}
|
|
|
|
|
$isDuplicate = $this->doIsRootJobOldDuplicate( $job );
|
2013-11-25 14:38:37 +00:00
|
|
|
|
2013-02-28 22:21:28 +00:00
|
|
|
return $isDuplicate;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @see JobQueue::isRootJobOldDuplicate()
|
|
|
|
|
* @param Job $job
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
protected function doIsRootJobOldDuplicate( Job $job ) {
|
2013-04-03 20:51:02 +00:00
|
|
|
if ( !$job->hasRootJobParams() ) {
|
2013-02-28 22:21:28 +00:00
|
|
|
return false; // job has no de-deplication info
|
|
|
|
|
}
|
2013-04-03 20:51:02 +00:00
|
|
|
$params = $job->getRootJobParams();
|
2013-02-28 22:21:28 +00:00
|
|
|
|
2013-10-08 02:30:10 +00:00
|
|
|
$key = $this->getRootJobCacheKey( $params['rootJobSignature'] );
|
2013-02-28 22:21:28 +00:00
|
|
|
// Get the last time this root job was enqueued
|
2013-10-08 02:30:10 +00:00
|
|
|
$timestamp = $this->dupCache->get( $key );
|
2013-02-28 22:21:28 +00:00
|
|
|
|
|
|
|
|
// Check if a new root job was started at the location after this one's...
|
|
|
|
|
return ( $timestamp && $timestamp > $params['rootJobTimestamp'] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $signature Hash identifier of the root job
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
protected function getRootJobCacheKey( $signature ) {
|
2018-11-14 18:41:04 +00:00
|
|
|
return $this->dupCache->makeGlobalKey(
|
2018-10-13 07:29:23 +00:00
|
|
|
'jobqueue',
|
|
|
|
|
$this->domain,
|
|
|
|
|
$this->type,
|
|
|
|
|
'rootjob',
|
|
|
|
|
$signature
|
|
|
|
|
);
|
2012-11-08 22:01:40 +00:00
|
|
|
}
|
|
|
|
|
|
2013-05-15 02:29:22 +00:00
|
|
|
/**
|
|
|
|
|
* Deleted all unclaimed and delayed jobs from the queue
|
|
|
|
|
*
|
2013-06-19 03:09:10 +00:00
|
|
|
* @throws JobQueueError
|
2013-05-15 02:29:22 +00:00
|
|
|
* @since 1.22
|
2014-04-16 17:51:11 +00:00
|
|
|
* @return void
|
2013-05-15 02:29:22 +00:00
|
|
|
*/
|
|
|
|
|
final public function delete() {
|
2016-03-25 19:52:39 +00:00
|
|
|
$this->assertNotReadOnly();
|
|
|
|
|
|
2014-04-16 17:51:11 +00:00
|
|
|
$this->doDelete();
|
2013-05-15 02:29:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @see JobQueue::delete()
|
2013-11-25 17:12:42 +00:00
|
|
|
* @throws MWException
|
2013-05-15 02:29:22 +00:00
|
|
|
*/
|
|
|
|
|
protected function doDelete() {
|
|
|
|
|
throw new MWException( "This method is not implemented." );
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-29 00:01:31 +00:00
|
|
|
/**
|
2016-09-05 20:21:26 +00:00
|
|
|
* Wait for any replica DBs or backup servers to catch up.
|
2013-01-02 20:12:59 +00:00
|
|
|
*
|
|
|
|
|
* This does nothing for certain queue classes.
|
2012-08-29 00:01:31 +00:00
|
|
|
*
|
|
|
|
|
* @return void
|
2013-06-19 03:09:10 +00:00
|
|
|
* @throws JobQueueError
|
2012-08-29 00:01:31 +00:00
|
|
|
*/
|
|
|
|
|
final public function waitForBackups() {
|
|
|
|
|
$this->doWaitForBackups();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @see JobQueue::waitForBackups()
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2013-11-25 14:38:37 +00:00
|
|
|
protected function doWaitForBackups() {
|
|
|
|
|
}
|
2013-01-12 00:13:29 +00:00
|
|
|
|
2013-02-06 22:45:33 +00:00
|
|
|
/**
|
|
|
|
|
* Clear any process and persistent caches
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
final public function flushCaches() {
|
|
|
|
|
$this->doFlushCaches();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @see JobQueue::flushCaches()
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2013-11-25 14:38:37 +00:00
|
|
|
protected function doFlushCaches() {
|
|
|
|
|
}
|
2013-02-08 18:52:54 +00:00
|
|
|
|
2013-02-21 01:19:38 +00:00
|
|
|
/**
|
2013-03-12 03:40:01 +00:00
|
|
|
* Get an iterator to traverse over all available jobs in this queue.
|
|
|
|
|
* This does not include jobs that are currently acquired or delayed.
|
2013-07-10 00:15:26 +00:00
|
|
|
* Note: results may be stale if the queue is concurrently modified.
|
2013-02-21 01:19:38 +00:00
|
|
|
*
|
2013-04-03 18:56:27 +00:00
|
|
|
* @return Iterator
|
2013-06-19 03:09:10 +00:00
|
|
|
* @throws JobQueueError
|
2013-02-21 01:19:38 +00:00
|
|
|
*/
|
|
|
|
|
abstract public function getAllQueuedJobs();
|
|
|
|
|
|
2013-03-12 03:40:01 +00:00
|
|
|
/**
|
|
|
|
|
* Get an iterator to traverse over all delayed jobs in this queue.
|
2013-07-10 00:15:26 +00:00
|
|
|
* Note: results may be stale if the queue is concurrently modified.
|
2013-03-12 03:40:01 +00:00
|
|
|
*
|
2013-04-03 18:56:27 +00:00
|
|
|
* @return Iterator
|
2013-06-19 03:09:10 +00:00
|
|
|
* @throws JobQueueError
|
2013-03-12 03:40:01 +00:00
|
|
|
* @since 1.22
|
|
|
|
|
*/
|
|
|
|
|
public function getAllDelayedJobs() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return new ArrayIterator( [] ); // not implemented
|
2013-03-12 03:40:01 +00:00
|
|
|
}
|
|
|
|
|
|
2015-04-17 18:08:26 +00:00
|
|
|
/**
|
|
|
|
|
* Get an iterator to traverse over all claimed jobs in this queue
|
|
|
|
|
*
|
|
|
|
|
* Callers should be quick to iterator over it or few results
|
|
|
|
|
* will be returned due to jobs being acknowledged and deleted
|
|
|
|
|
*
|
|
|
|
|
* @return Iterator
|
|
|
|
|
* @throws JobQueueError
|
|
|
|
|
* @since 1.26
|
|
|
|
|
*/
|
|
|
|
|
public function getAllAcquiredJobs() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return new ArrayIterator( [] ); // not implemented
|
2015-04-17 18:08:26 +00:00
|
|
|
}
|
|
|
|
|
|
2015-03-26 19:17:10 +00:00
|
|
|
/**
|
|
|
|
|
* Get an iterator to traverse over all abandoned jobs in this queue
|
|
|
|
|
*
|
|
|
|
|
* @return Iterator
|
|
|
|
|
* @throws JobQueueError
|
|
|
|
|
* @since 1.25
|
|
|
|
|
*/
|
|
|
|
|
public function getAllAbandonedJobs() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return new ArrayIterator( [] ); // not implemented
|
2015-03-26 19:17:10 +00:00
|
|
|
}
|
|
|
|
|
|
2013-07-04 07:05:19 +00:00
|
|
|
/**
|
|
|
|
|
* Do not use this function outside of JobQueue/JobQueueGroup
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
* @since 1.22
|
|
|
|
|
*/
|
|
|
|
|
public function getCoalesceLocationInternal() {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check whether each of the given queues are empty.
|
|
|
|
|
* This is used for batching checks for queues stored at the same place.
|
|
|
|
|
*
|
|
|
|
|
* @param array $types List of queues types
|
|
|
|
|
* @return array|null (list of non-empty queue types) or null if unsupported
|
|
|
|
|
* @throws MWException
|
|
|
|
|
* @since 1.22
|
|
|
|
|
*/
|
|
|
|
|
final public function getSiblingQueuesWithJobs( array $types ) {
|
|
|
|
|
return $this->doGetSiblingQueuesWithJobs( $types );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @see JobQueue::getSiblingQueuesWithJobs()
|
|
|
|
|
* @param array $types List of queues types
|
|
|
|
|
* @return array|null (list of queue types) or null if unsupported
|
|
|
|
|
*/
|
|
|
|
|
protected function doGetSiblingQueuesWithJobs( array $types ) {
|
|
|
|
|
return null; // not supported
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check the size of each of the given queues.
|
|
|
|
|
* For queues not served by the same store as this one, 0 is returned.
|
|
|
|
|
* This is used for batching checks for queues stored at the same place.
|
|
|
|
|
*
|
|
|
|
|
* @param array $types List of queues types
|
|
|
|
|
* @return array|null (job type => whether queue is empty) or null if unsupported
|
|
|
|
|
* @throws MWException
|
|
|
|
|
* @since 1.22
|
|
|
|
|
*/
|
|
|
|
|
final public function getSiblingQueueSizes( array $types ) {
|
|
|
|
|
return $this->doGetSiblingQueueSizes( $types );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @see JobQueue::getSiblingQueuesSize()
|
|
|
|
|
* @param array $types List of queues types
|
|
|
|
|
* @return array|null (list of queue types) or null if unsupported
|
|
|
|
|
*/
|
|
|
|
|
protected function doGetSiblingQueueSizes( array $types ) {
|
|
|
|
|
return null; // not supported
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-25 19:52:39 +00:00
|
|
|
/**
|
|
|
|
|
* @throws JobQueueReadOnlyError
|
|
|
|
|
*/
|
|
|
|
|
protected function assertNotReadOnly() {
|
|
|
|
|
if ( $this->readOnlyReason !== false ) {
|
|
|
|
|
throw new JobQueueReadOnlyError( "Job queue is read-only: {$this->readOnlyReason}" );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-16 17:43:37 +00:00
|
|
|
/**
|
|
|
|
|
* Call wfIncrStats() for the queue overall and for the queue type
|
|
|
|
|
*
|
|
|
|
|
* @param string $key Event type
|
|
|
|
|
* @param string $type Job type
|
2013-11-25 17:12:42 +00:00
|
|
|
* @param int $delta
|
2013-04-16 17:43:37 +00:00
|
|
|
* @since 1.22
|
|
|
|
|
*/
|
2015-04-20 19:22:30 +00:00
|
|
|
public static function incrStats( $key, $type, $delta = 1 ) {
|
2015-06-12 17:41:52 +00:00
|
|
|
static $stats;
|
|
|
|
|
if ( !$stats ) {
|
2017-03-17 10:57:37 +00:00
|
|
|
$stats = MediaWikiServices::getInstance()->getStatsdDataFactory();
|
2015-06-12 17:41:52 +00:00
|
|
|
}
|
2015-06-15 05:31:13 +00:00
|
|
|
$stats->updateCount( "jobqueue.{$key}.all", $delta );
|
2015-06-12 17:41:52 +00:00
|
|
|
$stats->updateCount( "jobqueue.{$key}.{$type}", $delta );
|
2013-04-16 17:43:37 +00:00
|
|
|
}
|
2012-08-29 00:01:31 +00:00
|
|
|
}
|
2013-06-19 03:09:10 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @ingroup JobQueue
|
|
|
|
|
* @since 1.22
|
|
|
|
|
*/
|
2013-11-25 14:38:37 +00:00
|
|
|
class JobQueueError extends MWException {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class JobQueueConnectionError extends JobQueueError {
|
|
|
|
|
}
|
2016-03-25 19:52:39 +00:00
|
|
|
|
|
|
|
|
class JobQueueReadOnlyError extends JobQueueError {
|
|
|
|
|
|
|
|
|
|
}
|