Merge "Typo fix"
[mediawiki.git] / includes / job / aggregator / JobQueueAggregatorMemc.php
blob9434da04590fc6e18569697948e54b1b9a655d0d
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 * @params include:
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.
41 * @param array $params
43 protected function __construct( array $params ) {
44 parent::__construct( $params );
45 $this->cache = isset( $params['objectCache'] )
46 ? wfGetCache( $params['objectCache'] )
47 : wfGetMainCache();
48 $this->cacheTTL = isset( $params['cacheTTL'] ) ? $params['cacheTTL'] : 180; // 3 min
51 /**
52 * @see JobQueueAggregator::doNotifyQueueEmpty()
54 protected function doNotifyQueueEmpty( $wiki, $type ) {
55 $key = $this->getReadyQueueCacheKey();
56 // Delist the queue from the "ready queue" list
57 if ( $this->cache->add( "$key:lock", 1, 60 ) ) { // lock
58 $curInfo = $this->cache->get( $key );
59 if ( is_array( $curInfo ) && isset( $curInfo['pendingDBs'][$type] ) ) {
60 if ( in_array( $wiki, $curInfo['pendingDBs'][$type] ) ) {
61 $curInfo['pendingDBs'][$type] = array_diff(
62 $curInfo['pendingDBs'][$type], array( $wiki ) );
63 $this->cache->set( $key, $curInfo );
66 $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
106 return is_array( $pendingDbInfo )
107 ? $pendingDbInfo['pendingDBs']
108 : array(); // cache is both empty and locked
112 * @see JobQueueAggregator::doPurge()
114 protected function doPurge() {
115 return $this->cache->delete( $this->getReadyQueueCacheKey() );
119 * @return string
121 private function getReadyQueueCacheKey() {
122 return "jobqueue:aggregator:ready-queues:v1"; // global