Skip to content

Commit

Permalink
feat: do not update data at if it fails to fetch it when booting
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Gleich <[email protected]>
  • Loading branch information
gleich committed Jan 8, 2025
1 parent 59d024b commit 548950e
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 10 deletions.
4 changes: 2 additions & 2 deletions internal/apis/applemusic/applemusic.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ func cacheUpdate() (cacheData, error) {
func Setup(mux *http.ServeMux) {
data, err := cacheUpdate()
if err != nil {
lumber.Fatal(err, "initial fetch of cache data failed")
lumber.Error(err, "initial fetch of cache data failed")
}

applemusicCache := cache.New("applemusic", data)
applemusicCache := cache.New("applemusic", data, err == nil)
mux.HandleFunc("GET /applemusic", serveHTTP(applemusicCache))
mux.HandleFunc("GET /applemusic/playlists/{id}", playlistEndpoint(applemusicCache))
go applemusicCache.UpdatePeriodically(cacheUpdate, 30*time.Second)
Expand Down
4 changes: 2 additions & 2 deletions internal/apis/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ func Setup(mux *http.ServeMux) {

pinnedRepos, err := fetchPinnedRepos(githubClient)
if err != nil {
lumber.Fatal(err, "fetching initial pinned repos failed")
lumber.Error(err, "fetching initial pinned repos failed")
}

githubCache := cache.New("github", pinnedRepos)
githubCache := cache.New("github", pinnedRepos, err == nil)
mux.HandleFunc("GET /github", githubCache.ServeHTTP)
go githubCache.UpdatePeriodically(
func() ([]repository, error) { return fetchPinnedRepos(githubClient) },
Expand Down
4 changes: 2 additions & 2 deletions internal/apis/steam/steam.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (
func Setup(mux *http.ServeMux) {
games, err := fetchRecentlyPlayedGames()
if err != nil {
lumber.Fatal(err, "initial fetch of games failed")
lumber.Error(err, "initial fetch of games failed")
}

steamCache := cache.New("steam", games)
steamCache := cache.New("steam", games, err == nil)
mux.HandleFunc("GET /steam", steamCache.ServeHTTP)
go steamCache.UpdatePeriodically(fetchRecentlyPlayedGames, 5*time.Minute)
lumber.Done("setup steam cache")
Expand Down
5 changes: 3 additions & 2 deletions internal/apis/strava/strava.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ func Setup(mux *http.ServeMux) {
}
stravaActivities, err := fetchActivities(*minioClient, stravaTokens)
if err != nil {
lumber.ErrorMsg("failed to load initial data for strava cache; not updating")
lumber.Error(err, "failed to load initial data for strava cache; not updating")
}
stravaCache := cache.New("strava", stravaActivities)
stravaCache := cache.New("strava", stravaActivities, err == nil)

mux.HandleFunc("GET /strava", stravaCache.ServeHTTP)
mux.HandleFunc("POST /strava/event", eventRoute(stravaCache, *minioClient, stravaTokens))
mux.HandleFunc("GET /strava/event", challengeRoute)
Expand Down
6 changes: 4 additions & 2 deletions internal/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@ type Cache[T any] struct {
filePath string
}

func New[T any](name string, data T) *Cache[T] {
func New[T any](name string, data T, update bool) *Cache[T] {
cache := Cache[T]{
name: name,
Updated: time.Now(),
filePath: filepath.Join(secrets.SECRETS.CacheFolder, fmt.Sprintf("%s.json", name)),
}
cache.loadFromFile()
cache.Update(data)
if update {
cache.Update(data)
}
return &cache
}

Expand Down

0 comments on commit 548950e

Please sign in to comment.