[JobQueue] Added JobQueue::getAbandonedCount() and use it in showJobs.php.
authorAaron Schulz <aschulz@wikimedia.org>
Wed, 20 Mar 2013 19:40:09 +0000 (12:40 -0700)
committerAaron Schulz <aschulz@wikimedia.org>
Mon, 1 Apr 2013 20:53:20 +0000 (13:53 -0700)
Change-Id: I5d96c61165b2693589d5cf36309fdb6a8b5a137e

includes/job/JobQueue.php
includes/job/JobQueueDB.php
includes/job/JobQueueRedis.php
maintenance/showJobs.php

index 09ca67c..41ef443 100644 (file)
@@ -238,6 +238,30 @@ abstract class JobQueue {
                return 0; // not implemented
        }
 
+       /**
+        * Get the number of acquired jobs that can no longer be attempted.
+        * Queue classes should use caching if they are any slower without memcached.
+        *
+        * If caching is used, this number might be out of date for a minute.
+        *
+        * @return integer
+        * @throws MWException
+        */
+       final public function getAbandonedCount() {
+               wfProfileIn( __METHOD__ );
+               $res = $this->doGetAbandonedCount();
+               wfProfileOut( __METHOD__ );
+               return $res;
+       }
+
+       /**
+        * @see JobQueue::getAbandonedCount()
+        * @return integer
+        */
+       protected function doGetAbandonedCount() {
+               return 0; // not implemented
+       }
+
        /**
         * Push a single jobs into the queue.
         * This does not require $wgJobClasses to be set for the given job type.
index d6e96ef..ae4576c 100644 (file)
@@ -136,6 +136,39 @@ class JobQueueDB extends JobQueue {
                return $count;
        }
 
+       /**
+        * @see JobQueue::doGetAbandonedCount()
+        * @return integer
+        * @throws MWException
+        */
+       protected function doGetAbandonedCount() {
+               global $wgMemc;
+
+               if ( $this->claimTTL <= 0 ) {
+                       return 0; // no acknowledgements
+               }
+
+               $key = $this->getCacheKey( 'abandonedcount' );
+
+               $count = $wgMemc->get( $key );
+               if ( is_int( $count ) ) {
+                       return $count;
+               }
+
+               list( $dbr, $scope ) = $this->getSlaveDB();
+               $count = (int)$dbr->selectField( 'job', 'COUNT(*)',
+                       array(
+                               'job_cmd' => $this->type,
+                               "job_token != {$dbr->addQuotes( '' )}",
+                               "job_attempts >= " . $dbr->addQuotes( $this->maxTries )
+                       ),
+                       __METHOD__
+               );
+               $wgMemc->set( $key, $count, self::CACHE_TTL_SHORT );
+
+               return $count;
+       }
+
        /**
         * @see JobQueue::doBatchPush()
         * @param array $jobs
index 706d42d..d0901ef 100644 (file)
@@ -154,6 +154,23 @@ class JobQueueRedis extends JobQueue {
                }
        }
 
+       /**
+        * @see JobQueue::doGetAbandonedCount()
+        * @return integer
+        * @throws MWException
+        */
+       protected function doGetAbandonedCount() {
+               if ( $this->claimTTL <= 0 ) {
+                       return 0; // no acknowledgements
+               }
+               $conn = $this->getConnection();
+               try {
+                       return $conn->zSize( $this->getQueueKey( 'z-abandoned' ) );
+               } catch ( RedisException $e ) {
+                       $this->throwRedisException( $this->server, $conn, $e );
+               }
+       }
+
        /**
         * @see JobQueue::doBatchPush()
         * @param array $jobs
index 8b49517..831746d 100644 (file)
@@ -47,8 +47,13 @@ class ShowJobs extends Maintenance {
                                $queue   = $group->get( $type );
                                $pending = $queue->getSize();
                                $claimed = $queue->getAcquiredCount();
+                               $abandoned = $queue->getAbandonedCount();
+                               $active = ( $claimed - $abandoned );
                                if ( ( $pending + $claimed ) > 0 ) {
-                                       $this->output( "{$type}: $pending queued; $claimed acquired\n" );
+                                       $this->output(
+                                               "{$type}: $pending queued; " .
+                                               "$claimed claimed ($active active, $abandoned abandoned)\n"
+                                       );
                                }
                        }
                } else {