diff --git a/admin/tool/task/lib.php b/admin/tool/task/lib.php index 56cd18310dd..64d99754e8e 100644 --- a/admin/tool/task/lib.php +++ b/admin/tool/task/lib.php @@ -45,5 +45,6 @@ function tool_task_status_checks() : array { * @param string $eol End of line character */ function tool_task_mtrace_wrapper(string $message, string $eol): void { - echo s($message . $eol); + $message = s($message); + echo $message . $eol; } diff --git a/admin/tool/task/run_adhoctasks.php b/admin/tool/task/run_adhoctasks.php index d665e40278d..1b45035d893 100644 --- a/admin/tool/task/run_adhoctasks.php +++ b/admin/tool/task/run_adhoctasks.php @@ -111,6 +111,7 @@ require_sesskey(); \core\session\manager::write_close(); // Prepare to handle output via mtrace. +require('lib.php'); $CFG->mtrace_wrapper = 'tool_task_mtrace_wrapper'; // Run the specified tasks. diff --git a/admin/tool/task/schedule_task.php b/admin/tool/task/schedule_task.php index ecaaf7efde0..bdd02bfb693 100644 --- a/admin/tool/task/schedule_task.php +++ b/admin/tool/task/schedule_task.php @@ -89,6 +89,7 @@ echo $OUTPUT->select_element_for_append(); // Prepare to handle output via mtrace. echo html_writer::start_tag('pre', ['style' => 'color: #fff; background: #333; padding: 1em; min-height: 24lh']); +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 58bb2609556..90ea9450205 100644 --- a/lib/classes/task/manager.php +++ b/lib/classes/task/manager.php @@ -1516,12 +1516,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); + } + /** * Executes an ad hoc task from web invocation using PHP CLI. * @@ -1586,7 +1614,7 @@ class manager { } // Execute it. - passthru($command); + self::passthru_via_mtrace($command); } /**