From 7d5891b75e3d91a6376edb9d159dec2b00cc711e Mon Sep 17 00:00:00 2001 From: Matt Gleich Date: Thu, 1 Aug 2024 22:45:10 -0400 Subject: [PATCH] feat: abort update if error when fetching new data closes #3 Signed-off-by: Matt Gleich --- internal/apis/github/github.go | 10 ++++++++-- internal/apis/github/repos.go | 6 +++--- internal/apis/steam/games.go | 14 +++++++------- internal/apis/steam/steam.go | 6 +++++- internal/cache/cache.go | 9 +++++++-- 5 files changed, 30 insertions(+), 15 deletions(-) diff --git a/internal/apis/github/github.go b/internal/apis/github/github.go index 6ed482b..f7995b0 100644 --- a/internal/apis/github/github.go +++ b/internal/apis/github/github.go @@ -18,8 +18,14 @@ func Setup(router *chi.Mux) { ) githubHttpClient := oauth2.NewClient(context.Background(), githubTokenSource) githubClient := githubv4.NewClient(githubHttpClient) - githubCache := cache.NewCache("github", fetchPinnedRepos(githubClient)) + + pinnedRepos, err := fetchPinnedRepos(githubClient) + if err != nil { + lumber.Fatal(err, "fetching initial pinned repos failed") + } + + githubCache := cache.NewCache("github", pinnedRepos) router.Get("/github/cache", githubCache.ServeHTTP()) - go githubCache.StartPeriodicUpdate(func() []repository { return fetchPinnedRepos(githubClient) }, 2*time.Minute) + go githubCache.StartPeriodicUpdate(func() ([]repository, error) { return fetchPinnedRepos(githubClient) }, 2*time.Minute) lumber.Success("setup github cache") } diff --git a/internal/apis/github/repos.go b/internal/apis/github/repos.go index c3a18d8..84446a5 100644 --- a/internal/apis/github/repos.go +++ b/internal/apis/github/repos.go @@ -46,12 +46,12 @@ type repository struct { URL string `json:"url"` } -func fetchPinnedRepos(client *githubv4.Client) []repository { +func fetchPinnedRepos(client *githubv4.Client) ([]repository, error) { var query pinnedItemsQuery err := client.Query(context.Background(), &query, nil) if err != nil { lumber.Error(err, "querying github's graphql API failed") - return nil + return nil, err } var repositories []repository @@ -68,5 +68,5 @@ func fetchPinnedRepos(client *githubv4.Client) []repository { URL: fmt.Sprint(node.Repository.URL.URL), }) } - return repositories + return repositories, nil } diff --git a/internal/apis/steam/games.go b/internal/apis/steam/games.go index 8c19b64..6e12a86 100644 --- a/internal/apis/steam/games.go +++ b/internal/apis/steam/games.go @@ -40,7 +40,7 @@ type game struct { Achievements *[]achievement `json:"achievements"` } -func fetchRecentlyPlayedGames() []game { +func fetchRecentlyPlayedGames() ([]game, error) { params := url.Values{ "key": {secrets.SECRETS.SteamKey}, "steamid": {secrets.SECRETS.SteamID}, @@ -50,18 +50,18 @@ func fetchRecentlyPlayedGames() []game { resp, err := http.Get("https://api.steampowered.com/IPlayerService/GetOwnedGames/v1?" + params.Encode()) if err != nil { lumber.Error(err, "sending request for owned games failed") - return nil + return nil, err } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { lumber.Error(err, "reading response body for owned games failed") - return nil + return nil, err } if resp.StatusCode != http.StatusOK { lumber.ErrorMsg(resp.StatusCode, "when trying to get owned games", string(body)) - return nil + return nil, err } var ownedGames ownedGamesResponse @@ -69,7 +69,7 @@ func fetchRecentlyPlayedGames() []game { if err != nil { lumber.Error(err, "failed to parse json for owned games") lumber.Debug("body:", string(body)) - return nil + return nil, err } sort.Slice(ownedGames.Response.Games, func(i, j int) bool { @@ -83,7 +83,7 @@ func fetchRecentlyPlayedGames() []game { libraryImageResponse, err := http.Get(libraryURL) if err != nil { lumber.Error(err, "getting library image for", g.Name, "failed") - return nil + return nil, err } defer libraryImageResponse.Body.Close() @@ -110,5 +110,5 @@ func fetchRecentlyPlayedGames() []game { }) } - return games + return games, nil } diff --git a/internal/apis/steam/steam.go b/internal/apis/steam/steam.go index 17dd689..48458ef 100644 --- a/internal/apis/steam/steam.go +++ b/internal/apis/steam/steam.go @@ -9,7 +9,11 @@ import ( ) func Setup(router *chi.Mux) { - games := fetchRecentlyPlayedGames() + games, err := fetchRecentlyPlayedGames() + if err != nil { + lumber.Fatal(err, "initial fetch of games failed") + } + steamCache := cache.NewCache("steam", games) router.Get("/steam/cache", steamCache.ServeHTTP()) go steamCache.StartPeriodicUpdate(fetchRecentlyPlayedGames, 5*time.Minute) diff --git a/internal/cache/cache.go b/internal/cache/cache.go index 06355d7..2de921b 100644 --- a/internal/cache/cache.go +++ b/internal/cache/cache.go @@ -94,10 +94,15 @@ func (c *Cache[T]) Update(data T) { } } -func (c *Cache[T]) StartPeriodicUpdate(updateFunc func() T, interval time.Duration) { +func (c *Cache[T]) StartPeriodicUpdate(updateFunc func() (T, error), interval time.Duration) { ticker := time.NewTicker(interval) defer ticker.Stop() for range ticker.C { - c.Update(updateFunc()) + data, err := updateFunc() + if err != nil { + lumber.ErrorMsg("updating cache", c.Name, "failed") + continue + } + c.Update(data) } }