Skip to content

Commit

Permalink
feat: abort update if error when fetching new data
Browse files Browse the repository at this point in the history
closes #3

Signed-off-by: Matt Gleich <[email protected]>
  • Loading branch information
gleich committed Aug 2, 2024
1 parent f68819f commit 7d5891b
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 15 deletions.
10 changes: 8 additions & 2 deletions internal/apis/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
6 changes: 3 additions & 3 deletions internal/apis/github/repos.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -68,5 +68,5 @@ func fetchPinnedRepos(client *githubv4.Client) []repository {
URL: fmt.Sprint(node.Repository.URL.URL),
})
}
return repositories
return repositories, nil
}
14 changes: 7 additions & 7 deletions internal/apis/steam/games.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -50,26 +50,26 @@ 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
err = json.Unmarshal(body, &ownedGames)
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 {
Expand All @@ -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()

Expand All @@ -110,5 +110,5 @@ func fetchRecentlyPlayedGames() []game {
})
}

return games
return games, nil
}
6 changes: 5 additions & 1 deletion internal/apis/steam/steam.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 7 additions & 2 deletions internal/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 7d5891b

Please sign in to comment.