Merge "Fixed headers sent in addMissingMetadata()"
[lhc/web/wiklou.git] / includes / jobqueue / JobRunner.php
index 77c4238..700a63d 100644 (file)
@@ -109,10 +109,8 @@ class JobRunner implements LoggerAwareInterface {
                        return $response;
                }
 
-               $profiler = Profiler::instance();
-
                // Catch huge single updates that lead to slave lag
-               $trxProfiler = $profiler->getTransactionProfiler();
+               $trxProfiler = Profiler::instance()->getTransactionProfiler();
                $trxProfiler->setLogger( LoggerFactory::getInstance( 'DBPerformance' ) );
                $trxProfiler->setExpectations( $wgTrxProfilerLimits['JobRunner'], __METHOD__ );
 
@@ -176,7 +174,6 @@ class JobRunner implements LoggerAwareInterface {
                                $this->debugCallback( $msg );
 
                                // Run the job...
-                               $psection = $profiler->scopedProfileIn( __METHOD__ . '-' . $jType );
                                $jobStartTime = microtime( true );
                                try {
                                        ++$jobsPopped;
@@ -197,15 +194,15 @@ class JobRunner implements LoggerAwareInterface {
                                wfGetLBFactory()->commitAll();
                                $timeMs = intval( ( microtime( true ) - $jobStartTime ) * 1000 );
                                $timeMsTotal += $timeMs;
-                               $profiler->scopedProfileOut( $psection );
 
-                               $queuedTs = $job->getQueuedTimestamp();
-                               if ( $queuedTs ) {
+                               $readyTs = $job->getReadyTimestamp();
+                               if ( $readyTs ) {
                                        // Record time to run for the job type
-                                       $pickupDelay = $popTime - $queuedTs;
-                                       $stats->timing( 'jobqueue.pickup_delay.all', $queuedTs );
-                                       $stats->timing( "jobqueue.pickup_delay.$jType", $queuedTs );
+                                       $pickupDelay = $popTime - $readyTs;
+                                       $stats->timing( 'jobqueue.pickup_delay.all', 1000 * $pickupDelay );
+                                       $stats->timing( "jobqueue.pickup_delay.$jType", 1000 * $pickupDelay );
                                }
+                               $stats->timing( "jobqueue.run.$jType", $timeMs );
 
                                // Mark the job as done on success or when the job cannot be retried
                                if ( $status !== false || !$job->allowRetries() ) {
@@ -263,7 +260,10 @@ class JobRunner implements LoggerAwareInterface {
                                }
 
                                // Bail if near-OOM instead of in a job
-                               $this->assertMemoryOK();
+                               if ( !$this->checkMemoryOK() ) {
+                                       $response['reached'] = 'memory-limit';
+                                       break;
+                               }
                        }
                } while ( $job ); // stop when there are no jobs
 
@@ -392,9 +392,9 @@ class JobRunner implements LoggerAwareInterface {
        /**
         * Make sure that this script is not too close to the memory usage limit.
         * It is better to die in between jobs than OOM right in the middle of one.
-        * @throws MWException
+        * @return bool
         */
-       private function assertMemoryOK() {
+       private function checkMemoryOK() {
                static $maxBytes = null;
                if ( $maxBytes === null ) {
                        $m = array();
@@ -408,8 +408,14 @@ class JobRunner implements LoggerAwareInterface {
                }
                $usedBytes = memory_get_usage();
                if ( $maxBytes && $usedBytes >= 0.95 * $maxBytes ) {
-                       throw new MWException( "Detected excessive memory usage ($usedBytes/$maxBytes)." );
+                       $msg = "Detected excessive memory usage ($usedBytes/$maxBytes).";
+                       $this->debugCallback( $msg );
+                       $this->logger->error( $msg );
+
+                       return false;
                }
+
+               return true;
        }
 
        /**