Avoid making a derivative request to Special:RunJobs when the job queue is empty
authorAaron Schulz <aschulz@wikimedia.org>
Tue, 8 Apr 2014 16:34:49 +0000 (09:34 -0700)
committerOri Livneh <ori@wikimedia.org>
Thu, 10 Apr 2014 05:49:46 +0000 (22:49 -0700)
Checking that the job queue is nonempty is cheap, and when the result is
negative (that is, when the queue is actually empty), it spares from having to
launch a derivative request to Special:RunJobs for job processing. The
derivative request is not cheap, because processing it requires having an
additional full instance of the application. This patch adds the check.

Bug: 60210
Change-Id: Icb95c35f4a8a3c9d4b5aff97fdfabf596d05940a

includes/Wiki.php
includes/jobqueue/JobQueueGroup.php

index ccc9b8d..fbafba8 100644 (file)
@@ -651,6 +651,10 @@ class MediaWiki {
                        return;
                }
 
+               if ( !JobQueueGroup::singleton()->queuesHaveJobs( JobQueueGroup::TYPE_DEFAULT ) ) {
+                       return; // do not send request if there are probably no jobs
+               }
+
                $query = array( 'title' => 'Special:RunJobs',
                        'tasks' => 'jobs', 'maxjobs' => $n, 'sigexpiry' => time() + 5 );
                $query['signature'] = SpecialRunJobs::getQuerySignature( $query );
index 90742ce..b1dfe14 100644 (file)
@@ -254,6 +254,31 @@ class JobQueueGroup {
                return array_diff( $this->getQueueTypes(), $wgJobTypesExcludedFromDefaultQueue );
        }
 
+       /**
+        * Check if there are any queues with jobs (this is cached)
+        *
+        * @param integer $type JobQueueGroup::TYPE_* constant
+        * @return bool
+        * @since 1.23
+        */
+       public function queuesHaveJobs( $type = self::TYPE_ANY ) {
+               global $wgMemc;
+
+               $key = wfMemcKey( 'jobqueue', 'queueshavejobs', $type );
+
+               $value = $wgMemc->get( $key );
+               if ( $value === false ) {
+                       $queues = $this->getQueuesWithJobs();
+                       if ( $type == self::TYPE_DEFAULT ) {
+                               $queues = array_intersect( $queues, $this->getDefaultQueueTypes() );
+                       }
+                       $value = count( $queues ) ? 'true' : 'false';
+                       $wgMemc->add( $key, $value, 15 );
+               }
+
+               return ( $value === 'true' );
+       }
+
        /**
         * Get the list of job types that have non-empty queues
         *