Implement extension registration from an extension.json file
[mediawiki.git] / includes / jobqueue / aggregator / JobQueueAggregatorMemc.php
blobae266ef384de9deb1882a3ab7f23a499107f7987
1 <?php
2 /**
3 * Job queue aggregator code that uses BagOStuff.
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.
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.
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
20 * @file
21 * @author Aaron Schulz
24 /**
25 * Class to handle tracking information about all queues using BagOStuff
27 * @ingroup JobQueue
28 * @since 1.21
30 class JobQueueAggregatorMemc extends JobQueueAggregator {
31 /** @var BagOStuff */
32 protected $cache;
34 protected $cacheTTL; // integer; seconds
36 /**
37 * @param array $params Possible keys:
38 * - objectCache : Name of an object cache registered in $wgObjectCaches.
39 * This defaults to the one specified by $wgMainCacheType.
40 * - cacheTTL : Seconds to cache the aggregate data before regenerating.
42 protected function __construct( array $params ) {
43 parent::__construct( $params );
44 $this->cache = isset( $params['objectCache'] )
45 ? wfGetCache( $params['objectCache'] )
46 : wfGetMainCache();
47 $this->cacheTTL = isset( $params['cacheTTL'] ) ? $params['cacheTTL'] : 180; // 3 min
50 /**
51 * @see JobQueueAggregator::doNotifyQueueEmpty()
53 protected function doNotifyQueueEmpty( $wiki, $type ) {
54 $key = $this->getReadyQueueCacheKey();
55 // Delist the queue from the "ready queue" list
56 if ( $this->cache->add( "$key:lock", 1, 60 ) ) { // lock
57 $curInfo = $this->cache->get( $key );
58 if ( is_array( $curInfo ) && isset( $curInfo['pendingDBs'][$type] ) ) {
59 if ( in_array( $wiki, $curInfo['pendingDBs'][$type] ) ) {
60 $curInfo['pendingDBs'][$type] = array_diff(
61 $curInfo['pendingDBs'][$type], array( $wiki ) );
62 $this->cache->set( $key, $curInfo );
65 $this->cache->delete( "$key:lock" ); // unlock
68 return true;
71 /**
72 * @see JobQueueAggregator::doNotifyQueueNonEmpty()
74 protected function doNotifyQueueNonEmpty( $wiki, $type ) {
75 return true; // updated periodically
78 /**
79 * @see JobQueueAggregator::doAllGetReadyWikiQueues()
81 protected function doGetAllReadyWikiQueues() {
82 $key = $this->getReadyQueueCacheKey();
83 // If the cache entry wasn't present, is stale, or in .1% of cases otherwise,
84 // regenerate the cache. Use any available stale cache if another process is
85 // currently regenerating the pending DB information.
86 $pendingDbInfo = $this->cache->get( $key );
87 if ( !is_array( $pendingDbInfo )
88 || ( time() - $pendingDbInfo['timestamp'] ) > $this->cacheTTL
89 || mt_rand( 0, 999 ) == 0
90 ) {
91 if ( $this->cache->add( "$key:rebuild", 1, 1800 ) ) { // lock
92 $pendingDbInfo = array(
93 'pendingDBs' => $this->findPendingWikiQueues(),
94 'timestamp' => time()
96 for ( $attempts = 1; $attempts <= 25; ++$attempts ) {
97 if ( $this->cache->add( "$key:lock", 1, 60 ) ) { // lock
98 $this->cache->set( $key, $pendingDbInfo );
99 $this->cache->delete( "$key:lock" ); // unlock
100 break;
103 $this->cache->delete( "$key:rebuild" ); // unlock
107 return is_array( $pendingDbInfo )
108 ? $pendingDbInfo['pendingDBs']
109 : array(); // cache is both empty and locked
113 * @see JobQueueAggregator::doPurge()
115 protected function doPurge() {
116 return $this->cache->delete( $this->getReadyQueueCacheKey() );
120 * @return string
122 private function getReadyQueueCacheKey() {
123 return "jobqueue:aggregator:ready-queues:v1"; // global