[ Index ]

PHP Cross Reference of MediaWiki-1.24.0

title

Body

[close]

/includes/jobqueue/aggregator/ -> JobQueueAggregatorMemc.php (source)

   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       * @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.
  41       */
  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
  48      }
  49  
  50      /**
  51       * @see JobQueueAggregator::doNotifyQueueEmpty()
  52       */
  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 );
  63                  }
  64              }
  65              $this->cache->delete( "$key:lock" ); // unlock
  66          }
  67  
  68          return true;
  69      }
  70  
  71      /**
  72       * @see JobQueueAggregator::doNotifyQueueNonEmpty()
  73       */
  74  	protected function doNotifyQueueNonEmpty( $wiki, $type ) {
  75          return true; // updated periodically
  76      }
  77  
  78      /**
  79       * @see JobQueueAggregator::doAllGetReadyWikiQueues()
  80       */
  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()
  95                  );
  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;
 101                      }
 102                  }
 103                  $this->cache->delete( "$key:rebuild" ); // unlock
 104              }
 105          }
 106  
 107          return is_array( $pendingDbInfo )
 108              ? $pendingDbInfo['pendingDBs']
 109              : array(); // cache is both empty and locked
 110      }
 111  
 112      /**
 113       * @see JobQueueAggregator::doPurge()
 114       */
 115  	protected function doPurge() {
 116          return $this->cache->delete( $this->getReadyQueueCacheKey() );
 117      }
 118  
 119      /**
 120       * @return string
 121       */
 122  	private function getReadyQueueCacheKey() {
 123          return "jobqueue:aggregator:ready-queues:v1"; // global
 124      }
 125  }


Generated: Fri Nov 28 14:03:12 2014 Cross-referenced by PHPXref 0.7.1