Merge "Support for enabling skins in the installer"
[lhc/web/wiklou.git] / maintenance / runJobs.php
index f69a3a3..e56640f 100644 (file)
@@ -86,7 +86,7 @@ class RunJobs extends Maintenance {
 
                $jobsRun = 0; // counter
                $flags = JobQueueGroup::USE_CACHE;
-               $lastTime = time(); // time since last slave check
+               $lastTime = microtime( true ); // time since last slave check
                do {
                        $backoffs = array_filter( $backoffs, $backoffExpireFunc );
                        $blacklist = $noThrottle ? array() : array_keys( $backoffs );
@@ -101,8 +101,6 @@ class RunJobs extends Maintenance {
                                ++$jobsRun;
                                $this->runJobsLog( $job->toString() . " STARTING" );
 
-                               // Set timer to stop the job if too much CPU time is used
-                               set_time_limit( $maxTime ? : 0 );
                                // Run the job...
                                wfProfileIn( __METHOD__ . '-' . get_class( $job ) );
                                $t = microtime( true );
@@ -117,8 +115,6 @@ class RunJobs extends Maintenance {
                                }
                                $timeMs = intval( ( microtime( true ) - $t ) * 1000 );
                                wfProfileOut( __METHOD__ . '-' . get_class( $job ) );
-                               // Disable the timer
-                               set_time_limit( 0 );
 
                                // Mark the job as done on success or when the job cannot be retried
                                if ( $status !== false || !$job->allowRetries() ) {
@@ -131,8 +127,11 @@ class RunJobs extends Maintenance {
                                        $this->runJobsLog( $job->toString() . " t=$timeMs good" );
                                }
 
-                               // Back off of certain jobs for a while
+                               // Back off of certain jobs for a while (for throttling and for errors)
                                $ttw = $this->getBackoffTimeToWait( $job );
+                               if ( $status === false && mt_rand( 0, 49 ) == 0 ) {
+                                       $ttw = max( $ttw, 30 );
+                               }
                                if ( $ttw > 0 ) {
                                        $jType = $job->getType();
                                        $backoffs[$jType] = isset( $backoffs[$jType] ) ? $backoffs[$jType] : 0;
@@ -147,10 +146,10 @@ class RunJobs extends Maintenance {
                                }
 
                                // Don't let any of the main DB slaves get backed up
-                               $timePassed = time() - $lastTime;
+                               $timePassed = microtime( true ) - $lastTime;
                                if ( $timePassed >= 5 || $timePassed < 0 ) {
-                                       wfWaitForSlaves();
-                                       $lastTime = time();
+                                       wfWaitForSlaves( $lastTime );
+                                       $lastTime = microtime( true );
                                }
                                // Don't let any queue slaves/backups fall behind
                                if ( $jobsRun > 0 && ( $jobsRun % 100 ) == 0 ) {
@@ -189,9 +188,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;