[JobQueue] Made the maximum number of job attempts configurable.
authorAaron Schulz <aschulz@wikimedia.org>
Wed, 20 Feb 2013 01:05:28 +0000 (17:05 -0800)
committerGerrit Code Review <gerrit@wikimedia.org>
Thu, 21 Feb 2013 05:42:15 +0000 (05:42 +0000)
Change-Id: I3aaab748913af68f4a676855c5c4f825706f7be7

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

index 67fe180..f02ed98 100644 (file)
@@ -33,11 +33,10 @@ abstract class JobQueue {
        protected $type; // string; job type
        protected $order; // string; job priority for pop()
        protected $claimTTL; // integer; seconds
+       protected $maxTries; // integer; maximum number of times to try a job
 
        const QoS_Atomic = 1; // integer; "all-or-nothing" job insertions
 
-       const MAX_ATTEMPTS = 3; // integer; number of times to try a job
-
        /**
         * @param $params array
         */
@@ -46,6 +45,7 @@ abstract class JobQueue {
                $this->type     = $params['type'];
                $this->order    = isset( $params['order'] ) ? $params['order'] : 'random';
                $this->claimTTL = isset( $params['claimTTL'] ) ? $params['claimTTL'] : 0;
+               $this->maxTries = isset( $params['maxTries'] ) ? $params['maxTries'] : 3;
        }
 
        /**
index f583c52..fd64895 100644 (file)
@@ -423,7 +423,7 @@ class JobQueueDB extends JobQueue {
                                        'job_cmd' => $this->type,
                                        "job_token != {$dbw->addQuotes( '' )}", // was acquired
                                        "job_token_timestamp < {$dbw->addQuotes( $claimCutoff )}", // stale
-                                       "job_attempts < {$dbw->addQuotes( self::MAX_ATTEMPTS )}" ), // retries left
+                                       "job_attempts < {$dbw->addQuotes( $this->maxTries )}" ), // retries left
                                __METHOD__
                        );
                        $ids = array_map( function( $o ) { return $o->job_id; }, iterator_to_array( $res ) );
@@ -453,7 +453,7 @@ class JobQueueDB extends JobQueue {
                        "job_token_timestamp < {$dbw->addQuotes( $pruneCutoff )}" // stale
                );
                if ( $this->claimTTL > 0 ) { // only prune jobs attempted too many times...
-                       $conds[] = "job_attempts >= {$dbw->addQuotes( self::MAX_ATTEMPTS )}";
+                       $conds[] = "job_attempts >= {$dbw->addQuotes( $this->maxTries )}";
                }
                // Get the IDs of jobs that are considered stale and should be removed. Selecting
                // the IDs first means that the UPDATE can be done by primary key (less deadlocks).
index 07a7410..2ce47bb 100644 (file)
@@ -372,7 +372,7 @@ class JobQueueRedis extends JobQueue {
                                        if ( $ctime < $claimCutoff ) {
                                                // Get the number of failed attempts
                                                $attempts = isset( $info['attempts'] ) ? $info['attempts'] : 0;
-                                               if ( $attempts < self::MAX_ATTEMPTS ) {
+                                               if ( $attempts < $this->maxTries ) {
                                                        $uidsPush[] = $uid; // retry it
                                                } elseif ( $ctime < $pruneCutoff ) {
                                                        $uidsRemove[] = $uid; // just remove it