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

@ -555,7 +555,12 @@ async def get_games_of_period(
since_seconds = since // 1000
until_seconds = until // 1000
result = await stats_service.get_games_of_period(username, since_seconds, until_seconds, rated_only)
if not result.success:
# Реальная ошибка при обращении к Lichess — не маскируем её под "0 игр"
raise HTTPException(status_code=502, detail=result.message)
return result
except HTTPException:
raise
except Exception as e:
logger.error(f"Ошибка в endpoint get_games_of_period: {e}")
raise HTTPException(status_code=500, detail=f"Внутренняя ошибка сервера: {str(e)}")
@ -695,7 +700,12 @@ async def get_puzzle_of_period(
try:
result = await stats_service.get_puzzle_of_period(token, since, until, max)
if not result.success:
# Реальная ошибка при обращении к Lichess — не маскируем её под "0 пазлов"
raise HTTPException(status_code=502, detail=result.message)
return result
except HTTPException:
raise
except Exception as e:
logger.error(f"Ошибка в endpoint get_puzzle_of_period: {e}")
raise HTTPException(status_code=500, detail=f"Внутренняя ошибка сервера: {str(e)}")