MDL-72775 tool_task: check long running tasks

This commit is contained in:
Qihui Chan
2022-12-23 09:41:05 +10:00
parent 15a695d573
commit 791e56d3bc
8 changed files with 190 additions and 5 deletions
+37
View File
@@ -26,6 +26,7 @@ namespace core\task;
use core_component;
use core_plugin_manager;
use core\check\result;
/**
* Abstract class for common properties of scheduled_task and adhoc_task.
@@ -257,4 +258,40 @@ abstract class task_base {
return $plugininfo && ($plugininfo->is_enabled() !== false);
}
}
/**
* Returns task runtime
* @return int
*/
public function get_runtime() {
return time() - $this->timestarted;
}
/**
* Returns if the task has been running for too long
* @return result
*/
public function get_runtime_result() {
global $CFG;
$runtime = $this->get_runtime();
$runtimeerror = $CFG->taskruntimeerror;
$runtimewarn = $CFG->taskruntimewarn;
$status = result::OK;
$details = '';
if ($runtime > $runtimewarn) {
$status = result::WARNING;
$details = get_string('slowtask', 'tool_task', format_time($runtimewarn));
}
if ($runtime > $runtimeerror) {
$status = result::ERROR;
$details = get_string('slowtask', 'tool_task', format_time($runtimeerror));
}
// This result is aggregated with other running tasks checks before display.
return new result($status, '', $details);
}
}