Changed on-request job running to shell out instead of doing a loop.
authorAaron Schulz <aschulz@wikimedia.org>
Thu, 18 Apr 2013 05:05:47 +0000 (22:05 -0700)
committerGerrit Code Review <gerrit@wikimedia.org>
Thu, 25 Apr 2013 23:46:05 +0000 (23:46 +0000)
* Also factored out a new wfShellExecDisabled() function.
* This will keep the process in the background if possible to avoid
  killing site performance, especially with slow jobs.
* This also keep fatals and uncatcheable exceptions from
  hitting the user.
* If $wgPhpCli is not set to an actual path or safe mode
  is on, then the old code will be used.

Change-Id: I6a28152251659ee53eee2604f16d5bf02c85a44f

includes/GlobalFunctions.php
includes/Wiki.php

index 458ab54..a81a338 100644 (file)
@@ -2736,22 +2736,12 @@ function wfEscapeShellArg() {
 }
 
 /**
- * Execute a shell command, with time and memory limits mirrored from the PHP
- * configuration if supported.
- * @param string $cmd Command line, properly escaped for shell.
- * @param &$retval null|Mixed optional, will receive the program's exit code.
- *                 (non-zero is usually failure)
- * @param array $environ optional environment variables which should be
- *                 added to the executed command environment.
- * @param array $limits optional array with limits(filesize, memory, time, walltime)
- *                 this overwrites the global wgShellMax* limits.
- * @return string collected stdout as a string (trailing newlines stripped)
+ * Check if wfShellExec() is effectively disabled via php.ini config
+ * @return bool|string False or one of (safemode,disabled)
+ * @since 1.22
  */
-function wfShellExec( $cmd, &$retval = null, $environ = array(), $limits = array() ) {
-       global $IP, $wgMaxShellMemory, $wgMaxShellFileSize, $wgMaxShellTime,
-               $wgMaxShellWallClockTime, $wgShellCgroup;
-
-       static $disabled;
+function wfShellExecDisabled() {
+       static $disabled = null;
        if ( is_null( $disabled ) ) {
                $disabled = false;
                if ( wfIniGetBool( 'safe_mode' ) ) {
@@ -2767,6 +2757,26 @@ function wfShellExec( $cmd, &$retval = null, $environ = array(), $limits = array
                        }
                }
        }
+       return $disabled;
+}
+
+/**
+ * Execute a shell command, with time and memory limits mirrored from the PHP
+ * configuration if supported.
+ * @param string $cmd Command line, properly escaped for shell.
+ * @param &$retval null|Mixed optional, will receive the program's exit code.
+ *                 (non-zero is usually failure)
+ * @param array $environ optional environment variables which should be
+ *                 added to the executed command environment.
+ * @param array $limits optional array with limits(filesize, memory, time, walltime)
+ *                 this overwrites the global wgShellMax* limits.
+ * @return string collected stdout as a string (trailing newlines stripped)
+ */
+function wfShellExec( $cmd, &$retval = null, $environ = array(), $limits = array() ) {
+       global $IP, $wgMaxShellMemory, $wgMaxShellFileSize, $wgMaxShellTime,
+               $wgMaxShellWallClockTime, $wgShellCgroup;
+
+       $disabled = wfShellExecDisabled();
        if ( $disabled ) {
                $retval = 1;
                return $disabled == 'safemode' ?
index 7d0d5af..2fd12d5 100644 (file)
@@ -599,7 +599,7 @@ class MediaWiki {
         * Do a job from the job queue
         */
        private function doJobs() {
-               global $wgJobRunRate;
+               global $wgJobRunRate, $wgPhpCli, $IP;
 
                if ( $wgJobRunRate <= 0 || wfReadOnly() ) {
                        return;
@@ -615,25 +615,36 @@ class MediaWiki {
                        $n = intval( $wgJobRunRate );
                }
 
-               $group = JobQueueGroup::singleton();
-               do {
-                       $job = $group->pop( JobQueueGroup::USE_CACHE ); // job from any queue
-                       if ( $job ) {
-                               $output = $job->toString() . "\n";
-                               $t = - microtime( true );
-                               wfProfileIn( __METHOD__ . '-' . get_class( $job ) );
-                               $success = $job->run();
-                               wfProfileOut( __METHOD__ . '-' . get_class( $job ) );
-                               $group->ack( $job ); // done
-                               $t += microtime( true );
-                               $t = round( $t * 1000 );
-                               if ( $success === false ) {
-                                       $output .= "Error: " . $job->getLastError() . ", Time: $t ms\n";
-                               } else {
-                                       $output .= "Success, Time: $t ms\n";
+               if ( !wfShellExecDisabled() && is_executable( $wgPhpCli ) ) {
+                       // Start a background process to run some of the jobs.
+                       // This will be asynchronous on *nix though not on Windows.
+                       wfProfileIn( __METHOD__ . '-exec' );
+                       $retVal = 1;
+                       $cmd = wfShellWikiCmd( "$IP/maintenance/runJobs.php", array( '--maxjobs', $n ) );
+                       wfShellExec( "$cmd &", $retVal );
+                       wfProfileOut( __METHOD__ . '-exec' );
+               } else {
+                       // Fallback to running the jobs here while the user waits
+                       $group = JobQueueGroup::singleton();
+                       do {
+                               $job = $group->pop( JobQueueGroup::USE_CACHE ); // job from any queue
+                               if ( $job ) {
+                                       $output = $job->toString() . "\n";
+                                       $t = - microtime( true );
+                                       wfProfileIn( __METHOD__ . '-' . get_class( $job ) );
+                                       $success = $job->run();
+                                       wfProfileOut( __METHOD__ . '-' . get_class( $job ) );
+                                       $group->ack( $job ); // done
+                                       $t += microtime( true );
+                                       $t = round( $t * 1000 );
+                                       if ( $success === false ) {
+                                               $output .= "Error: " . $job->getLastError() . ", Time: $t ms\n";
+                                       } else {
+                                               $output .= "Success, Time: $t ms\n";
+                                       }
+                                       wfDebugLog( 'jobqueue', $output );
                                }
-                               wfDebugLog( 'jobqueue', $output );
-                       }
-               } while ( --$n && $job );
+                       } while ( --$n && $job );
+               }
        }
 }