From 91e805b2d02df46e7c5836ad2e851db286442913 Mon Sep 17 00:00:00 2001 From: Alexander Van der Bellen Date: Mon, 4 Dec 2023 12:53:05 +0800 Subject: [PATCH] Fix get_min_duration_for_reason exception --- classes/profile_helper.php | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/classes/profile_helper.php b/classes/profile_helper.php index 8739f16..210521a 100644 --- a/classes/profile_helper.php +++ b/classes/profile_helper.php @@ -123,14 +123,14 @@ public static function get_min_duration_for_group_and_reason(string $group, int * * @param int $reason the profile type or REASON_* * @param bool $usecache whether or not to even bother with caching. This allows for a forceful cache update. - * @return float duration (as seconds) of the fastest profile for a given reason. + * @return float|null duration (as seconds) of the fastest profile for a given reason. */ - public static function get_min_duration_for_reason(int $reason, bool $usecache = true): float { + public static function get_min_duration_for_reason(int $reason, bool $usecache = true): ?float { $quota = self::$quotas[$reason]; $cachefield = 'profile_type_' . $reason . '_min_duration_s'; $cache = \cache::make('tool_excimer', 'request_metadata'); - $result = $cache->get(self::ALL_GROUP_CACHE_KEY) ?: array(); + $result = $cache->get(self::ALL_GROUP_CACHE_KEY) ?: []; if (!$usecache || empty($result) || !isset($result[$cachefield])) { // Get and set cache. @@ -145,9 +145,19 @@ public static function get_min_duration_for_reason(int $reason, bool $usecache = WHERE $reasons != ? ORDER BY duration DESC "; - $resultset = $db->get_records_sql($sql, [ - profile::REASON_NONE, - ], $quota - 1, 1); // Will fetch the Nth item based on the quota. + + try { + $resultset = $db->get_records_sql( + $sql, + [profile::REASON_NONE], + $quota - 1, // Will fetch the Nth item based on the quota. + 1 + ); + } catch (\dml_exception $e) { + debugging('tool_excimer: Failed to get min duration: '.$e->getMessage()); + return null; + } + // Cache the results in (avoids recalculation later). $newvalue = (end($resultset)->min_duration ?? 0.0); // Updates the cache value if the calculated value is different.