Revert "Refactored use of $wgMemc in JobQueueDB to use a field."
[lhc/web/wiklou.git] / includes / job / JobQueueDB.php
index fd64895..a7a459f 100644 (file)
  * @since 1.21
  */
 class JobQueueDB extends JobQueue {
-       const ROOTJOB_TTL     = 1209600; // integer; seconds to remember root jobs (14 days)
+       const ROOTJOB_TTL = 1209600; // integer; seconds to remember root jobs (14 days)
        const CACHE_TTL_SHORT = 30; // integer; seconds to cache info without re-validating
-       const CACHE_TTL_LONG  = 300; // integer; seconds to cache info that is kept up to date
-       const MAX_AGE_PRUNE   = 604800; // integer; seconds a job can live once claimed
-       const MAX_JOB_RANDOM  = 2147483647; // integer; 2^31 - 1, used for job_random
-       const MAX_OFFSET      = 255; // integer; maximum number of rows to skip
+       const CACHE_TTL_LONG = 300; // integer; seconds to cache info that is kept up to date
+       const MAX_AGE_PRUNE = 604800; // integer; seconds a job can live once claimed
+       const MAX_JOB_RANDOM = 2147483647; // integer; 2^31 - 1, used for job_random
+       const MAX_OFFSET = 255; // integer; maximum number of rows to skip
 
        protected $cluster = false; // string; name of an external DB cluster
 
@@ -50,6 +50,14 @@ class JobQueueDB extends JobQueue {
                $this->cluster = isset( $params['cluster'] ) ? $params['cluster'] : false;
        }
 
+       protected function supportedOrders() {
+               return array( 'random', 'timestamp', 'fifo' );
+       }
+
+       protected function optimalOrder() {
+               return 'random';
+       }
+
        /**
         * @see JobQueue::doIsEmpty()
         * @return bool
@@ -225,8 +233,8 @@ class JobQueueDB extends JobQueue {
                                $row = $this->claimOldest( $uuid );
                        } else { // random first
                                $rand = mt_rand( 0, self::MAX_JOB_RANDOM ); // encourage concurrent UPDATEs
-                               $gte  = (bool)mt_rand( 0, 1 ); // find rows with rand before/after $rand
-                               $row  = $this->claimRandom( $uuid, $rand, $gte );
+                               $gte = (bool)mt_rand( 0, 1 ); // find rows with rand before/after $rand
+                               $row = $this->claimRandom( $uuid, $rand, $gte );
                        }
                        // Check if we found a row to reserve...
                        if ( !$row ) {
@@ -258,9 +266,9 @@ class JobQueueDB extends JobQueue {
        /**
         * Reserve a row with a single UPDATE without holding row locks over RTTs...
         *
-        * @param $uuid string 32 char hex string
+        * @param string $uuid 32 char hex string
         * @param $rand integer Random unsigned integer (31 bits)
-        * @param $gte bool Search for job_random >= $random (otherwise job_random <= $random)
+        * @param bool $gte Search for job_random >= $random (otherwise job_random <= $random)
         * @return Row|false
         */
        protected function claimRandom( $uuid, $rand, $gte ) {
@@ -281,8 +289,8 @@ class JobQueueDB extends JobQueue {
                                // For small queues, using OFFSET will overshoot and return no rows more often.
                                // Instead, this uses job_random to pick a row (possibly checking both directions).
                                $ineq = $gte ? '>=' : '<=';
-                               $dir  = $gte ? 'ASC' : 'DESC';
-                               $row  = $dbw->selectRow( 'job', '*', // find a random job
+                               $dir = $gte ? 'ASC' : 'DESC';
+                               $row = $dbw->selectRow( 'job', '*', // find a random job
                                        array(
                                                'job_cmd'   => $this->type,
                                                'job_token' => '', // unclaimed
@@ -338,7 +346,7 @@ class JobQueueDB extends JobQueue {
        /**
         * Reserve a row with a single UPDATE without holding row locks over RTTs...
         *
-        * @param $uuid string 32 char hex string
+        * @param string $uuid 32 char hex string
         * @return Row|false
         */
        protected function claimOldest( $uuid ) {
@@ -580,6 +588,27 @@ class JobQueueDB extends JobQueue {
                }
        }
 
+       /**
+        * @see JobQueue::getAllQueuedJobs()
+        * @return Iterator
+        */
+       public function getAllQueuedJobs() {
+               list( $dbr, $scope ) = $this->getSlaveDB();
+               return new MappedIterator(
+                       $dbr->select( 'job', '*', array( 'job_cmd' => $this->getType(), 'job_token' => '' ) ),
+                       function( $row ) use ( $scope ) {
+                               $job = Job::factory(
+                                       $row->job_cmd,
+                                       Title::makeTitle( $row->job_namespace, $row->job_title ),
+                                       strlen( $row->job_params ) ? unserialize( $row->job_params ) : false,
+                                       $row->job_id
+                               );
+                               $job->id = $row->job_id; // XXX: work around broken subclasses
+                               return $job;
+                       }
+               );
+       }
+
        /**
         * @return Array (DatabaseBase, ScopedCallback)
         */