MDL-76649 quiz statistics task: output more useful trace info

At the moment, quiz_statistics\task\recalculate gives no useful
information about what it is doing, which makes it hard to investigate
if the task fails. This commit makes it more usefully verbose.

Also, following this change, one instance of this task will not
run for more than one hour at a time.

As part of this commit, I have added a new helper mtrace_exception.
to consistently log exceptions in scheduled tasks. It is sad to
add a new function to moodlelib.php, but that seemed the logical place.
Looking at other tasks, this method is badly needed. Many are just
logging the ->getMessage() which is normaly insufficient for proper
debugging. However, swiching all existing tasks to use the new function
will need to wait for a future MDL.
This commit is contained in:
Tim Hunt
2023-01-09 13:46:29 +00:00
parent da216a5545
commit 0dcd79d2e8
4 changed files with 78 additions and 21 deletions
+19
View File
@@ -9280,6 +9280,25 @@ function mtrace($string, $eol="\n", $sleep=0) {
}
}
/**
* Helper to {@see mtrace()} an exception or throwable, including all relevant information.
*
* @param Throwable $e the error to ouptput.
*/
function mtrace_exception(Throwable $e): void {
$info = get_exception_info($e);
$message = $info->message;
if ($info->debuginfo) {
$message .= "\n\n" . $info->debuginfo;
}
if ($info->backtrace) {
$message .= "\n\n" . format_backtrace($info->backtrace, true);
}
mtrace($message);
}
/**
* Replace 1 or more slashes or backslashes to 1 slash
*