diff --git a/admin/tool/task/lib.php b/admin/tool/task/lib.php index ae23257b904..fd89b906ad8 100644 --- a/admin/tool/task/lib.php +++ b/admin/tool/task/lib.php @@ -37,3 +37,13 @@ function tool_task_status_checks() : array { ]; } +/** + * Function used to handle mtrace by outputting the text to normal browser window. + * + * @param string $message Message to output + * @param string $eol End of line character + */ +function tool_task_mtrace_wrapper(string $message, string $eol): void { + $message = s($message); + echo $message . $eol; +} diff --git a/admin/tool/task/schedule_task.php b/admin/tool/task/schedule_task.php index aabd3de70ea..84097ba0571 100644 --- a/admin/tool/task/schedule_task.php +++ b/admin/tool/task/schedule_task.php @@ -30,16 +30,6 @@ require('../../../config.php'); require_once($CFG->libdir.'/cronlib.php'); -/** - * Function used to handle mtrace by outputting the text to normal browser window. - * - * @param string $message Message to output - * @param string $eol End of line character - */ -function tool_task_mtrace_wrapper($message, $eol) { - echo s($message . $eol); -} - // Allow execution of single task. This requires login and has different rules. $taskname = required_param('task', PARAM_RAW_TRIMMED); @@ -97,6 +87,7 @@ require_sesskey(); // Prepare to handle output via mtrace. echo html_writer::start_tag('pre'); +require('lib.php'); $CFG->mtrace_wrapper = 'tool_task_mtrace_wrapper'; // Run the specified task (this will output an error if it doesn't exist). diff --git a/lib/classes/task/manager.php b/lib/classes/task/manager.php index 765fba2f726..24c236a8f30 100644 --- a/lib/classes/task/manager.php +++ b/lib/classes/task/manager.php @@ -1133,12 +1133,40 @@ class manager { $command = "{$phpbinary} {$scriptpath} {$taskarg}"; // Execute it. - passthru($command); + self::passthru_via_mtrace($command); } return true; } + /** + * This behaves similar to passthru but filters every line via + * the mtrace function so it can be post processed. + * + * @param string $command to run + * @return void + */ + public static function passthru_via_mtrace(string $command) { + $descriptorspec = [ + 0 => ['pipe', 'r'], // STDIN. + 1 => ['pipe', 'w'], // STDOUT. + 2 => ['pipe', 'w'], // STDERR. + ]; + flush(); + $process = proc_open($command, $descriptorspec, $pipes, realpath('./'), []); + if (is_resource($process)) { + while ($s = fgets($pipes[1])) { + mtrace($s, ''); + flush(); + } + } + + fclose($pipes[0]); + fclose($pipes[1]); + fclose($pipes[2]); + proc_close($process); + } + /** * For a given scheduled task record, this method will check to see if any overrides have * been applied in config and return a copy of the record with any overridden values.