fix(statistics): run recalculate independently

This commit is contained in:
MingxuanGame
2025-08-19 13:45:12 +00:00
parent 65bd7ca7d0
commit ce756c354b
3 changed files with 16 additions and 6 deletions

View File

@@ -537,7 +537,9 @@ def calculate_playtime(score: Score, beatmap_length: int) -> tuple[int, bool]:
+ (score.nsmall_tick_hit or 0)
)
total_obj = 0
for statistics, count in score.maximum_statistics.items():
for statistics, count in (
score.maximum_statistics.items() if score.maximum_statistics else {}
):
if not isinstance(statistics, HitResult):
statistics = HitResult(statistics)
if statistics.is_scorable():

View File

@@ -10,7 +10,6 @@ scheduler: AsyncIOScheduler | None = None
def init_scheduler():
global scheduler
scheduler = AsyncIOScheduler(timezone=UTC)
scheduler.start()
def get_scheduler() -> AsyncIOScheduler:
@@ -20,6 +19,12 @@ def get_scheduler() -> AsyncIOScheduler:
return scheduler # pyright: ignore[reportReturnType]
def start_scheduler():
global scheduler
if scheduler is not None:
scheduler.start()
def stop_scheduler():
global scheduler
if scheduler:

11
main.py
View File

@@ -1,5 +1,6 @@
from __future__ import annotations
import asyncio
from contextlib import asynccontextmanager
from datetime import datetime
import os
@@ -7,7 +8,7 @@ import os
from app.config import settings
from app.dependencies.database import engine, redis_client
from app.dependencies.fetcher import get_fetcher
from app.dependencies.scheduler import init_scheduler, stop_scheduler
from app.dependencies.scheduler import start_scheduler, stop_scheduler
from app.log import logger
from app.router import (
api_v1_router,
@@ -66,11 +67,9 @@ async def lifespan(app: FastAPI):
# on startup
await get_fetcher() # 初始化 fetcher
await init_geoip() # 初始化 GeoIP 数据库
if os.environ.get("RECALCULATE", "false").lower() == "true":
await recalculate()
await create_rx_statistics()
await calculate_user_rank(True)
init_scheduler()
start_scheduler()
schedule_geoip_updates() # 调度 GeoIP 定时更新任务
await daily_challenge_job()
await create_banchobot()
@@ -176,6 +175,10 @@ if settings.osu_web_client_secret == "your_osu_web_client_secret_here":
if __name__ == "__main__":
import uvicorn
if os.environ.get("RECALCULATE", "false").lower() == "true":
asyncio.run(recalculate())
exit()
uvicorn.run(
"main:app",
host=settings.host,