()
| 256 | } |
| 257 | |
| 258 | func (t *toolsUpdater) loop() { |
| 259 | defer t.Stop() |
| 260 | |
| 261 | // add some jitter. When spinning up multiple entities, we add |
| 262 | // jitter to prevent stampeeding herd. |
| 263 | randInt, err := rand.Int(rand.Reader, big.NewInt(3000)) |
| 264 | if err != nil { |
| 265 | randInt = big.NewInt(0) |
| 266 | } |
| 267 | t.sleepWithCancel(time.Duration(randInt.Int64()) * time.Millisecond) |
| 268 | |
| 269 | // add some jitter |
| 270 | timerJitter, err := rand.Int(rand.Reader, big.NewInt(120)) |
| 271 | if err != nil { |
| 272 | timerJitter = big.NewInt(0) |
| 273 | } |
| 274 | timer := time.NewTicker(1*time.Minute + time.Duration(timerJitter.Int64())*time.Second) |
| 275 | defer timer.Stop() |
| 276 | |
| 277 | reset: |
| 278 | now := time.Now().UTC() |
| 279 | if now.After(t.lastUpdate.Add(time.Duration(githubToolsUpdateDeadline) * time.Minute)) { |
| 280 | slog.DebugContext(t.ctx, "last update after deadline", "last_update", t.lastUpdate, "deadline", t.lastUpdate.Add(time.Duration(githubToolsUpdateDeadline)*time.Minute)) |
| 281 | if err := t.updateTools(); err != nil { |
| 282 | slog.ErrorContext(t.ctx, "updating tools", "error", err) |
| 283 | t.addStatusEvent(fmt.Sprintf("failed to update tools: %q", err), params.EventError) |
| 284 | } else { |
| 285 | // Tools are usually valid for 1 hour. |
| 286 | t.lastUpdate = time.Now().UTC() |
| 287 | t.addStatusEvent("successfully updated tools", params.EventInfo) |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | for { |
| 292 | t.mux.Lock() |
| 293 | if t.reset == nil { |
| 294 | t.reset = make(chan struct{}) |
| 295 | } |
| 296 | t.mux.Unlock() |
| 297 | |
| 298 | select { |
| 299 | case <-t.quit: |
| 300 | slog.DebugContext(t.ctx, "stopping tools updater") |
| 301 | return |
| 302 | case <-timer.C: |
| 303 | now := time.Now().UTC() |
| 304 | if !now.After(t.lastUpdate.Add(time.Duration(githubToolsUpdateDeadline) * time.Minute)) { |
| 305 | continue |
| 306 | } |
| 307 | slog.DebugContext(t.ctx, "updating tools") |
| 308 | if err := t.updateTools(); err != nil { |
| 309 | slog.ErrorContext(t.ctx, "updating tools", "error", err) |
| 310 | t.addStatusEvent(fmt.Sprintf("failed to update tools: %q", err), params.EventError) |
| 311 | } else { |
| 312 | // Tools are usually valid for 1 hour. |
| 313 | t.lastUpdate = time.Now().UTC() |
| 314 | t.addStatusEvent("successfully updated tools", params.EventInfo) |
| 315 | } |
no test coverage detected