From b34169e6cb456c0acbeba02e26a8cdfab537f14f Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Fri, 11 Sep 2020 14:07:50 +0200 Subject: [locationinfo] Always use cached data if backend fails Previously, we'd only deliver calendar data to the panel if the cache timeout hasn't been reached yet. In case fetching fresh data from the backend fails, go ahead and deliver the old cached data, no matter how old it is. --- .../locationinfo/inc/coursebackend.inc.php | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) (limited to 'modules-available') diff --git a/modules-available/locationinfo/inc/coursebackend.inc.php b/modules-available/locationinfo/inc/coursebackend.inc.php index fc03671c..6e4d77ac 100644 --- a/modules-available/locationinfo/inc/coursebackend.inc.php +++ b/modules-available/locationinfo/inc/coursebackend.inc.php @@ -196,13 +196,12 @@ abstract class CourseBackend $returnValue = []; $remoteIds = []; while ($row = $dbquery1->fetch(PDO::FETCH_ASSOC)) { - //Check if in cache if lastUpdate is null then it is interpreted as 1970 - if ($row['lastcalendarupdate'] + $this->getCacheTime() > $NOW) { - $returnValue[$row['locationid']] = json_decode($row['calendar']); - } else { + // Check if in cache - if lastUpdate is null then it is interpreted as 1970 + if ($row['lastcalendarupdate'] + $this->getCacheTime() < $NOW) { $remoteIds[$row['locationid']] = $row['serverlocationid']; } - + // Always add to return value - if updating fails, we better use the stale data than nothing + $returnValue[$row['locationid']] = json_decode($row['calendar'], true); } // No need for additional round trips to backend if (empty($remoteIds)) { @@ -234,7 +233,7 @@ abstract class CourseBackend // if, nothing bad will happen... Database::exec("UPDATE locationinfo_locationconfig SET lastcalendarupdate = :time WHERE lastcalendarupdate < :time AND serverid = :serverid AND serverlocationid IN (:slocs)", [ - 'time' => $NOW - $this->getCacheTime() / 2, + 'time' => $NOW - ($this->getCacheTime() - 60), // Protect for one minute max. 'serverid' => $this->serverId, 'slocs' => array_values($remoteIds), ]); @@ -266,15 +265,15 @@ abstract class CourseBackend 'serverid' => $this->serverId, 'serverlocationid' => $serverRoomId, 'ttable' => $value, - 'now' => $NOW + 'now' => $NOW, // Set real "lastupdate" here )); } - - unset($calendar); } + unset($calendar); // Add rooms that were requested to the final return value foreach ($remoteIds as $location => $serverRoomId) { - if (isset($backendResponse[$serverRoomId]) && in_array($location, $requestedLocationIds)) { + if (isset($backendResponse[$serverRoomId]) && is_array($backendResponse[$serverRoomId]) + && in_array($location, $requestedLocationIds)) { // Only add if we can map it back to our location id AND it was not an unsolicited coalesced refresh $returnValue[$location] = $backendResponse[$serverRoomId]; } -- cgit v1.2.3-55-g7522