fix: scheduler

This commit is contained in:
xtaodada 2024-03-11 20:06:59 +08:00
parent ad981468a4
commit 2dfbb66dd1
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659
3 changed files with 11 additions and 4 deletions

View File

@ -1,5 +1,5 @@
httpx==0.24.1 httpx==0.24.1
fastapi~=0.101.1 fastapi~=0.110.0
starlette starlette
uvicorn~=0.23.2 uvicorn~=0.23.2
pydantic pydantic
@ -8,6 +8,6 @@ coloredlogs
pytz pytz
apscheduler apscheduler
aiofiles==23.2.1 aiofiles==23.2.1
jinja2==3.1.2 jinja2==3.1.3
beautifulsoup4 beautifulsoup4
lxml lxml

View File

@ -4,6 +4,7 @@ from starlette.middleware.trustedhost import TrustedHostMiddleware
from .env import DOMAIN, DEBUG from .env import DOMAIN, DEBUG
from .route import get_routes from .route import get_routes
from .route.base import UserAgentMiddleware from .route.base import UserAgentMiddleware
from .services.scheduler import register_scheduler
app = FastAPI(docs_url=None, redoc_url=None, openapi_url=None) app = FastAPI(docs_url=None, redoc_url=None, openapi_url=None)
app.add_middleware( app.add_middleware(
@ -15,3 +16,4 @@ app.add_middleware(
if not DEBUG: if not DEBUG:
app.add_middleware(UserAgentMiddleware) app.add_middleware(UserAgentMiddleware)
get_routes() get_routes()
register_scheduler(app)

View File

@ -1,5 +1,10 @@
from apscheduler.schedulers.asyncio import AsyncIOScheduler from apscheduler.schedulers.asyncio import AsyncIOScheduler
from fastapi import FastAPI
scheduler = AsyncIOScheduler(timezone="Asia/ShangHai") 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()