From aefc567d29f40e5f1b8687106c4abb52aa78b14e Mon Sep 17 00:00:00 2001 From: Claude Brisson Date: Sat, 30 Mar 2024 06:03:51 +0100 Subject: [PATCH] Fall back to last fetched ratings file on i/o error while updating --- .../pairgoth/ratings/RatingsHandler.kt | 32 ++++++++++++++----- .../pairgoth/ratings/RatingsManager.kt | 4 +-- .../main/webapp/tour-registration.inc.html | 4 +-- 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/view-webapp/src/main/kotlin/org/jeudego/pairgoth/ratings/RatingsHandler.kt b/view-webapp/src/main/kotlin/org/jeudego/pairgoth/ratings/RatingsHandler.kt index 3819bef..2b839f6 100644 --- a/view-webapp/src/main/kotlin/org/jeudego/pairgoth/ratings/RatingsHandler.kt +++ b/view-webapp/src/main/kotlin/org/jeudego/pairgoth/ratings/RatingsHandler.kt @@ -7,6 +7,7 @@ import okhttp3.Request import org.jeudego.pairgoth.web.WebappManager import org.slf4j.LoggerFactory import java.io.File +import java.io.IOException import java.net.URL import java.nio.charset.StandardCharsets import java.time.LocalDate @@ -27,6 +28,7 @@ abstract class RatingsHandler(val origin: RatingsManager.Ratings) { lateinit var players: Json.Array lateinit var activeRatingsFile: File private var updated = false + val ready get() = this::activeRatingsFile.isInitialized && this::players.isInitialized val url: URL by lazy { WebappManager.properties.getProperty("ratings.${origin.name.lowercase(Locale.ROOT)}")?.let { URL(it) } ?: defaultURL @@ -63,6 +65,15 @@ abstract class RatingsHandler(val origin: RatingsManager.Ratings) { return initIfNeeded(latestRatingsFile) } val payload = fetchPayload() + if (payload == null) { + // an error occurred while fetching the payload, and has been reported + if (latestRatingsFile != null) { + // fall back to last ratings file + return initIfNeeded(latestRatingsFile) + } else { + return false + } + } val (lastUpdated, lastPlayers) = parsePayload(payload) val ratingsFilename = "${origin.name}-${ymd.format(lastUpdated)}.json" if (latestRatingsFile != null && latestRatingsFile.name == ratingsFilename) { @@ -87,15 +98,20 @@ abstract class RatingsHandler(val origin: RatingsManager.Ratings) { return players } - protected fun fetchPayload(): String { - val request = Request.Builder() - .url(url) - .build() + protected fun fetchPayload(): String? { + try { + val request = Request.Builder() + .url(url) + .build() - client.newCall(request).execute().use { response -> - if (!response.isSuccessful) throw Error("Could not fetch $origin ratings: unexpected code $response") - val contentType = response.headers["Content-Type"]?.toMediaType() - return response.body!!.source().readString(contentType?.charset() ?: defaultCharset()) + client.newCall(request).execute().use { response -> + if (!response.isSuccessful) throw IOException("Could not fetch $origin ratings: unexpected code $response") + val contentType = response.headers["Content-Type"]?.toMediaType() + return response.body!!.source().readString(contentType?.charset() ?: defaultCharset()) + } + } catch (ioe: IOException) { + logger.error("Could not refresh ${origin.name} ratings from ${url}", ioe) + return null } } open fun defaultCharset() = StandardCharsets.UTF_8 diff --git a/view-webapp/src/main/kotlin/org/jeudego/pairgoth/ratings/RatingsManager.kt b/view-webapp/src/main/kotlin/org/jeudego/pairgoth/ratings/RatingsManager.kt index 07075e6..46b607a 100644 --- a/view-webapp/src/main/kotlin/org/jeudego/pairgoth/ratings/RatingsManager.kt +++ b/view-webapp/src/main/kotlin/org/jeudego/pairgoth/ratings/RatingsManager.kt @@ -36,7 +36,7 @@ object RatingsManager: Runnable { ); } - fun activeMask() = ratingsHandlers.entries.filter { it.value.active }.map { it.key.flag }.reduce { a,b -> a or b } + fun activeMask() = ratingsHandlers.entries.filter { it.value.active && it.value.ready }.map { it.key.flag }.reduce { a,b -> a or b } val timer = Timer() lateinit var players: Json.MutableArray @@ -139,7 +139,7 @@ object RatingsManager: Runnable { val index = PlayerIndex() - public fun getRatingsDates() = ratingsHandlers.filter{ it.value.active }.map { + fun getRatingsDates() = ratingsHandlers.filter{ it.value.active && it.value.ready }.map { Pair(it.key.name.lowercase(), DateTimeFormatter.ISO_LOCAL_DATE.format(it.value.activeDate())) }.toMap() diff --git a/view-webapp/src/main/webapp/tour-registration.inc.html b/view-webapp/src/main/webapp/tour-registration.inc.html index 61584d7..cfbf572 100644 --- a/view-webapp/src/main/webapp/tour-registration.inc.html +++ b/view-webapp/src/main/webapp/tour-registration.inc.html @@ -112,7 +112,7 @@ *#
-
+
@@ -121,7 +121,7 @@
-
+