Merge "Cleanup a bunch of tests and add todos"
[lhc/web/wiklou.git] / includes / jobqueue / aggregator / JobQueueAggregatorMemc.php
1 <?php
2 /**
3 * Job queue aggregator code that uses BagOStuff.
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 * @author Aaron Schulz
22 */
23
24 /**
25 * Class to handle tracking information about all queues using BagOStuff
26 *
27 * @ingroup JobQueue
28 * @since 1.21
29 */
30 class JobQueueAggregatorMemc extends JobQueueAggregator {
31 /** @var BagOStuff */
32 protected $cache;
33
34 protected $cacheTTL; // integer; seconds
35
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
42 */
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
49 }
50
51 /**
52 * @see JobQueueAggregator::doNotifyQueueEmpty()
53 */
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 );
64 }
65 }
66 $this->cache->delete( "$key:lock" ); // unlock
67 }
68
69 return true;
70 }
71
72 /**
73 * @see JobQueueAggregator::doNotifyQueueNonEmpty()
74 */
75 protected function doNotifyQueueNonEmpty( $wiki, $type ) {
76 return true; // updated periodically
77 }
78
79 /**
80 * @see JobQueueAggregator::doAllGetReadyWikiQueues()
81 */
82 protected function doGetAllReadyWikiQueues() {
83 $key = $this->getReadyQueueCacheKey();
84 // If the cache entry wasn't present, is stale, or in .1% of cases otherwise,
85 // regenerate the cache. Use any available stale cache if another process is
86 // currently regenerating the pending DB information.
87 $pendingDbInfo = $this->cache->get( $key );
88 if ( !is_array( $pendingDbInfo )
89 || ( time() - $pendingDbInfo['timestamp'] ) > $this->cacheTTL
90 || mt_rand( 0, 999 ) == 0
91 ) {
92 if ( $this->cache->add( "$key:rebuild", 1, 1800 ) ) { // lock
93 $pendingDbInfo = array(
94 'pendingDBs' => $this->findPendingWikiQueues(),
95 'timestamp' => time()
96 );
97 for ( $attempts = 1; $attempts <= 25; ++$attempts ) {
98 if ( $this->cache->add( "$key:lock", 1, 60 ) ) { // lock
99 $this->cache->set( $key, $pendingDbInfo );
100 $this->cache->delete( "$key:lock" ); // unlock
101 break;
102 }
103 }
104 $this->cache->delete( "$key:rebuild" ); // unlock
105 }
106 }
107
108 return is_array( $pendingDbInfo )
109 ? $pendingDbInfo['pendingDBs']
110 : array(); // cache is both empty and locked
111 }
112
113 /**
114 * @see JobQueueAggregator::doPurge()
115 */
116 protected function doPurge() {
117 return $this->cache->delete( $this->getReadyQueueCacheKey() );
118 }
119
120 /**
121 * @return string
122 */
123 private function getReadyQueueCacheKey() {
124 return "jobqueue:aggregator:ready-queues:v1"; // global
125 }
126 }