Avoid making a derivative request to Special:RunJobs when the job queue is empty

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
This commit is contained in:
Aaron Schulz 2014-04-08 09:34:49 -07:00 committed by Ori Livneh
parent 8815c2ddf6
commit 4865e34630
2 changed files with 29 additions and 0 deletions

View 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 );

View 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
*