Skip to content

Commit

Permalink
Merge pull request #243 from damongolding/fix/weather-resilience
Browse files Browse the repository at this point in the history
Fix/weather resilience
  • Loading branch information
damongolding authored Dec 20, 2024
2 parents 057c279 + 37c5f04 commit 7fef620
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
1 change: 1 addition & 0 deletions internal/routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
)

const (
maxWeatherRetries = 3
maxRedirects = 10
redirectCountHeader = "X-Redirect-Count"
)
Expand Down
33 changes: 22 additions & 11 deletions internal/routes/routes_weather.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package routes
import (
"net/http"
"strings"
"time"

"github.com/charmbracelet/log"
"github.com/damongolding/immich-kiosk/internal/config"
Expand All @@ -26,36 +27,46 @@ func Weather(baseConfig *config.Config) echo.HandlerFunc {

requestID := requestData.RequestID

weatherLocation := c.QueryParam("weather")
locationName := c.QueryParam("weather")

log.Debug(
requestID,
"method", c.Request().Method,
"path", c.Request().URL.String(),
"location", weatherLocation,
"location", locationName,
)

if weatherLocation == "" {
if locationName == "" {
if !baseConfig.HasWeatherDefault {
log.Warn("No weather location provided and no default set")
return c.NoContent(http.StatusNoContent)
}
for _, loc := range baseConfig.WeatherLocations {
if loc.Default {
weatherLocation = loc.Name
locationName = loc.Name
break
}
}
log.Debug("Using default weather location", "location", weatherLocation)
log.Debug("Using default weather location", "location", locationName)
}

weatherData := weather.CurrentWeather(weatherLocation)
if !strings.EqualFold(weatherData.Name, weatherLocation) || len(weatherData.Data) == 0 {
log.Error("missing weather location data", "location", weatherData.Name)
return c.NoContent(http.StatusNoContent)
}
var weatherLocation weather.WeatherLocation

return Render(c, http.StatusOK, partials.WeatherLocation(weatherData))
for attempts := 0; attempts < maxWeatherRetries; attempts++ {
weatherLocation = weather.CurrentWeather(locationName)
if !strings.EqualFold(weatherLocation.Name, locationName) || len(weatherLocation.Data) == 0 {
log.Warn("weather data fetch attempt failed",
"attempt", attempts+1,
"location", locationName)
time.Sleep(time.Duration(1<<attempts) * time.Second)
continue
}
return Render(c, http.StatusOK, partials.WeatherLocation(weatherLocation))
}

log.Error("failed to fetch weather data after all attempts",
"location", locationName,
"received_name", weatherLocation.Name)
return c.NoContent(http.StatusNoContent)
}
}
2 changes: 1 addition & 1 deletion taskfile.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
version: "3"
env:
VERSION: 0.14.6
VERSION: 0.14.7

includes:
frontend:
Expand Down

0 comments on commit 7fef620

Please sign in to comment.