Skip to content

Commit ebbd2e2

Browse files
committed
better forecast loop
1 parent 79e7aa1 commit ebbd2e2

File tree

1 file changed

+15
-22
lines changed

1 file changed

+15
-22
lines changed

internal/weather/weather.go

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -176,37 +176,30 @@ func addWeatherLocation(ctx context.Context, location config.WeatherLocation, wi
176176
}
177177
}
178178

179+
var forecastCh <-chan time.Time
180+
if withForecast && forecastTicker != nil {
181+
forecastCh = forecastTicker.C
182+
}
179183
for {
180184
select {
181185
case <-ctx.Done():
182186
log.Debug("Stopping weather updates for", "name", w.Name)
183187
return
184188
case <-weatherTicker.C:
185189
log.Debug("Getting weather for", "name", w.Name)
186-
newWeather, newWeatherErr := w.updateWeather(ctx)
187-
if newWeatherErr != nil {
188-
log.Error("Failed to update weather", "name", w.Name, "error", newWeatherErr)
189-
continue
190+
if newWeather, err := w.updateWeather(ctx); err != nil {
191+
log.Error("Failed to update weather", "name", w.Name, "error", err)
192+
} else {
193+
weatherDataStore.Store(strings.ToLower(w.Name), newWeather)
194+
log.Debug("Retrieved weather for", "name", w.Name)
190195
}
191-
weatherDataStore.Store(strings.ToLower(w.Name), newWeather)
192-
log.Debug("Retrieved weather for", "name", w.Name)
193-
default:
194-
if withForecast && forecastTicker != nil {
195-
select {
196-
case <-forecastTicker.C:
197-
log.Debug("Getting forecast for", "name", w.Name)
198-
newForecast, newForecastErr := w.updateForecast(ctx)
199-
if newForecastErr != nil {
200-
log.Error("Failed to update forecast", "name", w.Name, "error", newForecastErr)
201-
continue
202-
}
203-
weatherDataStore.Store(strings.ToLower(w.Name), newForecast)
204-
log.Debug("Retrieved forecast for", "name", w.Name)
205-
default:
206-
time.Sleep(100 * time.Millisecond)
207-
}
196+
case <-forecastCh:
197+
log.Debug("Getting forecast for", "name", w.Name)
198+
if newForecast, err := w.updateForecast(ctx); err != nil {
199+
log.Error("Failed to update forecast", "name", w.Name, "error", err)
208200
} else {
209-
time.Sleep(100 * time.Millisecond)
201+
weatherDataStore.Store(strings.ToLower(w.Name), newForecast)
202+
log.Debug("Retrieved forecast for", "name", w.Name)
210203
}
211204
}
212205
}

0 commit comments

Comments
 (0)