Merge "Make abstract Config class truly implementation-agnostic"
[lhc/web/wiklou.git] / maintenance / runJobs.php
index c3eb4ba..9b86e1b 100644 (file)
@@ -43,6 +43,7 @@ class RunJobs extends Maintenance {
                if ( $this->hasOption( 'memory-limit' ) ) {
                        return parent::memoryLimit();
                }
+
                // Don't eat all memory on the machine if we get a bad job.
                return "150M";
        }
@@ -101,7 +102,7 @@ class RunJobs extends Maintenance {
                                $this->runJobsLog( $job->toString() . " STARTING" );
 
                                // Set timer to stop the job if too much CPU time is used
-                               set_time_limit( $maxTime ?: 0 );
+                               set_time_limit( $maxTime ? : 0 );
                                // Run the job...
                                wfProfileIn( __METHOD__ . '-' . get_class( $job ) );
                                $t = microtime( true );
@@ -175,9 +176,12 @@ class RunJobs extends Maintenance {
        private function getBackoffTimeToWait( Job $job ) {
                global $wgJobBackoffThrottling;
 
-               if ( !isset( $wgJobBackoffThrottling[$job->getType()] ) ) {
+               if ( !isset( $wgJobBackoffThrottling[$job->getType()] ) ||
+                       $job instanceof DuplicateJob // no work was done
+               ) {
                        return 0; // not throttled
                }
+
                $itemsPerSecond = $wgJobBackoffThrottling[$job->getType()];
                if ( $itemsPerSecond <= 0 ) {
                        return 0; // not throttled
@@ -185,9 +189,11 @@ class RunJobs extends Maintenance {
 
                $seconds = 0;
                if ( $job->workItemCount() > 0 ) {
-                       $seconds = floor( $job->workItemCount() / $itemsPerSecond );
-                       $remainder = $job->workItemCount() % $itemsPerSecond;
-                       $seconds += ( mt_rand( 1, $itemsPerSecond ) <= $remainder ) ? 1 : 0;
+                       $exactSeconds = $job->workItemCount() / $itemsPerSecond;
+                       // use randomized rounding
+                       $seconds = floor( $exactSeconds );
+                       $remainder = $exactSeconds - $seconds;
+                       $seconds += ( mt_rand() / mt_getrandmax() < $remainder ) ? 1 : 0;
                }
 
                return (int)$seconds;
@@ -209,7 +215,7 @@ class RunJobs extends Maintenance {
                        $content = stream_get_contents( $handle );
                        flock( $handle, LOCK_UN );
                        fclose( $handle );
-                       $backoffs = json_decode( $content, true ) ?: array();
+                       $backoffs = json_decode( $content, true ) ? : array();
                }
 
                return $backoffs;
@@ -227,7 +233,7 @@ class RunJobs extends Maintenance {
                $handle = fopen( $file, 'wb+' );
                flock( $handle, LOCK_EX );
                $content = stream_get_contents( $handle );
-               $cBackoffs = json_decode( $content, true ) ?: array();
+               $cBackoffs = json_decode( $content, true ) ? : array();
                foreach ( $backoffs as $type => $timestamp ) {
                        $cBackoffs[$type] = isset( $cBackoffs[$type] ) ? $cBackoffs[$type] : 0;
                        $cBackoffs[$type] = max( $cBackoffs[$type], $backoffs[$type] );