diff --git a/requirements.txt b/requirements.txt index 9a68306..4f3f884 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,5 @@ httpx==0.24.1 -fastapi~=0.101.1 +fastapi~=0.110.0 starlette uvicorn~=0.23.2 pydantic @@ -8,6 +8,6 @@ coloredlogs pytz apscheduler aiofiles==23.2.1 -jinja2==3.1.2 +jinja2==3.1.3 beautifulsoup4 lxml diff --git a/src/app.py b/src/app.py index b2356cd..141ef07 100644 --- a/src/app.py +++ b/src/app.py @@ -4,6 +4,7 @@ from starlette.middleware.trustedhost import TrustedHostMiddleware from .env import DOMAIN, DEBUG from .route import get_routes from .route.base import UserAgentMiddleware +from .services.scheduler import register_scheduler app = FastAPI(docs_url=None, redoc_url=None, openapi_url=None) app.add_middleware( @@ -15,3 +16,4 @@ app.add_middleware( if not DEBUG: app.add_middleware(UserAgentMiddleware) get_routes() +register_scheduler(app) diff --git a/src/services/scheduler.py b/src/services/scheduler.py index 148c970..fe65fb8 100644 --- a/src/services/scheduler.py +++ b/src/services/scheduler.py @@ -1,5 +1,10 @@ from apscheduler.schedulers.asyncio import AsyncIOScheduler +from fastapi import FastAPI scheduler = AsyncIOScheduler(timezone="Asia/ShangHai") -if not scheduler.running: - scheduler.start() + + +def register_scheduler(app: "FastAPI"): + @app.on_event("startup") + async def start_event(): + scheduler.start()