MDL-80309 tasks: Clean up mtrace logs

This commit is contained in:
Brendan Heywood
2023-12-07 12:07:45 +08:00
committed by Jun Pataleta
parent 7762fc61e8
commit 628e7ecc4c
3 changed files with 40 additions and 11 deletions
+10
View File
@@ -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;
}
+1 -10
View File
@@ -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).
+29 -1
View File
@@ -1392,12 +1392,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.