mirror of
https://github.com/davegallant/rfd-fyi.git
synced 2025-08-06 07:13:39 +00:00
Refresh topics every 60 seconds in the background
This commit is contained in:
@@ -82,20 +82,18 @@ func (a *App) listTopics(w http.ResponseWriter, r *http.Request) {
|
|||||||
ctx, span := tracer.Start(ctx, "list-topics")
|
ctx, span := tracer.Start(ctx, "list-topics")
|
||||||
defer span.End()
|
defer span.End()
|
||||||
|
|
||||||
if time.Since(a.LastRefresh).Minutes() > 1 {
|
|
||||||
a.refreshTopics()
|
|
||||||
} else {
|
|
||||||
log.Debug().Msg("Topics cache has not expired. Using existing.")
|
|
||||||
}
|
|
||||||
respondWithJSON(w, http.StatusOK, a.CurrentTopics)
|
respondWithJSON(w, http.StatusOK, a.CurrentTopics)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *App) refreshTopics() {
|
func (a *App) refreshTopics() {
|
||||||
latestTopics := a.getDeals(9, 1, 6)
|
for {
|
||||||
latestTopics = a.updateScores(latestTopics)
|
latestTopics := a.getDeals(9, 1, 6)
|
||||||
log.Debug().Msg("Refreshing topics")
|
latestTopics = a.updateScores(latestTopics)
|
||||||
a.CurrentTopics = latestTopics
|
log.Debug().Msg("Refreshing topics")
|
||||||
a.LastRefresh = time.Now()
|
a.CurrentTopics = latestTopics
|
||||||
|
a.LastRefresh = time.Now()
|
||||||
|
time.Sleep(60 * time.Second)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *App) updateScores(t []Topic) []Topic {
|
func (a *App) updateScores(t []Topic) []Topic {
|
||||||
|
@@ -35,5 +35,6 @@ func main() {
|
|||||||
|
|
||||||
a.Initialize()
|
a.Initialize()
|
||||||
|
|
||||||
|
go a.refreshTopics()
|
||||||
a.Run(httpPort)
|
a.Run(httpPort)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user