From c7ebe2850145ba158805894f108e2615a92e8b88 Mon Sep 17 00:00:00 2001 From: jholdstock Date: Mon, 18 Sep 2023 19:23:25 +0100 Subject: [PATCH] webapi: Try cache update before returning error. Rather than immediately returning an error if the cache is not initialized, attempt to initialize it first. Without this change there is only an attempt to initialize the cache once per minute. --- internal/webapi/middleware.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/internal/webapi/middleware.go b/internal/webapi/middleware.go index 304236b..a0b2e89 100644 --- a/internal/webapi/middleware.go +++ b/internal/webapi/middleware.go @@ -98,11 +98,14 @@ func (w *WebAPI) withSession(store *sessions.CookieStore) gin.HandlerFunc { // Server Error. func (w *WebAPI) requireWebCache(c *gin.Context) { if !w.cache.initialized() { - const str = "Cache is not yet initialized" - w.log.Errorf(str) - c.String(http.StatusInternalServerError, str) - c.Abort() - return + // Try to initialize it now. + err := w.cache.update() + if err != nil { + w.log.Errorf("Failed to initialize cache: %v", err) + c.String(http.StatusInternalServerError, "Cache is not initialized") + c.Abort() + return + } } c.Set(cacheKey, w.cache.getData())