fix periodic activity-check algorithm silently losing player activity
All checks were successful
CI/CD Pipeline / build-and-deploy (push) Successful in 38s

Errors from Lichess (timeouts/5xx/invalid tokens) were being disguised
as "no activity" (HTTP 200, games_count/puzzles_in_period=0), causing
the bot to silently advance its checkpoint past real, undetected
activity. Puzzle-fetch failures weren't counted as errors at all, and
the periodic task died permanently after 5 consecutive errors with no
way to recover short of a manual restart. /setperiod also unconditionally
reset the checkpoint, dropping the window between the last check and
the command.

- API now returns success=false/502 on real errors instead of masking
  them as zero activity (models.py, stats_service.py, main.py)
- Puzzle-fetch errors are now treated the same as game-fetch errors:
  retry the same window instead of reporting "no puzzles"
- Notification delivery failures no longer silently advance the
  checkpoint
- Replaced the hard 5-error kill switch with capped backoff that keeps
  retrying indefinitely, plus an admin-bot notification if a player's
  monitoring has been failing for a prolonged period (~2h+)
- /setperiod only clears the checkpoint when disabling notifications,
  preserving continuity when a period is just changed

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
vrubelroman 2026-07-02 19:29:06 +00:00
parent 75e62b57a1
commit 4a783225af
4 changed files with 118 additions and 58 deletions

View file

@ -699,6 +699,7 @@ class StatsService:
logger.error(f"Ошибка при получении статистики игр за период: {e}")
return GamesOfPeriodResponse(
message=f"Ошибка при получении статистики: {str(e)}",
success=False,
username=username,
period_start=since_timestamp,
period_end=until_timestamp,
@ -765,6 +766,7 @@ class StatsService:
if activities is None:
return PuzzleOfPeriodResponse(
message="Неверный токен авторизации или доступ запрещен",
success=False,
period_start=since_ms,
period_end=until_ms,
max_puzzles=max_puzzles,
@ -796,6 +798,7 @@ class StatsService:
logger.error(f"Ошибка при получении статистики решения задач за период: {e}")
return PuzzleOfPeriodResponse(
message=f"Ошибка при получении статистики: {str(e)}",
success=False,
period_start=since_ms,
period_end=until_ms,
max_puzzles=max_puzzles,