fix(worker): crash cooldown passes time.Time not time.Duration to pq

Passing time.Duration (int64 nanoseconds) as $2 in NOW() + $2 caused
PostgreSQL to interpret the nanosecond value as seconds, setting
cooldown_until to year ~59066 instead of +30 minutes.

Fix: pre-compute time.Now().Add(CrashCooldownDuration) and pass the
resulting time.Time — pq encodes it as a proper timestamptz.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
jedarden 2026-04-24 18:07:29 -04:00
parent 4f45670066
commit 992fa1d573

View file

@ -524,18 +524,23 @@ func updateCrashStrikes(ctx context.Context, tx *sql.Tx, crashedBots map[string]
return nil
}
cooldownUntil := time.Now().Add(CrashCooldownDuration)
for botID, crashed := range crashedBots {
if crashed {
// Increment strikes; if threshold reached, set cooldown
// Increment strikes; if threshold reached, set cooldown.
// Pass cooldownUntil as time.Time so pq encodes it as a proper
// timestamptz — passing time.Duration directly sends raw nanoseconds
// which PostgreSQL interprets as seconds, resulting in a ~57000-year cooldown.
_, err := tx.ExecContext(ctx, `
UPDATE bots
SET crash_strikes = crash_strikes + 1,
cooldown_until = CASE
WHEN crash_strikes + 1 >= $1 THEN NOW() + $2
WHEN crash_strikes + 1 >= $1 THEN $2
ELSE cooldown_until
END
WHERE bot_id = $3
`, MaxCrashStrikes, CrashCooldownDuration, botID)
`, MaxCrashStrikes, cooldownUntil, botID)
if err != nil {
return fmt.Errorf("failed to increment crash strikes for %s: %w", botID, err)
}