diff --git a/tiled/server/app.py b/tiled/server/app.py index b995e2fbf..1da155e22 100644 --- a/tiled/server/app.py +++ b/tiled/server/app.py @@ -5,6 +5,7 @@ import secrets import sys import urllib.parse +from contextlib import asynccontextmanager from functools import lru_cache, partial from pathlib import Path from typing import List @@ -195,7 +196,14 @@ def build_app( ) # If we reach here, the no configuration problems were found. - app = FastAPI() + @asynccontextmanager + async def lifespan(app: FastAPI): + "Manage lifespan events for each event loop that the app runs in" + await startup_event() + yield + await shutdown_event() + + app = FastAPI(lifespan=lifespan) if SHARE_TILED_PATH: # If the distribution includes static assets, serve UI routes. @@ -465,7 +473,6 @@ def override_get_settings(): settings.database_uri = settings.database_uri or "sqlite+aiosqlite://" return settings - @app.on_event("startup") async def startup_event(): from .. import __version__ @@ -671,7 +678,6 @@ async def purge_expired_sessions_and_api_keys(): asyncio.create_task(purge_expired_sessions_and_api_keys()) ) - @app.on_event("shutdown") async def shutdown_event(): # Run shutdown tasks collected from trees (adapters). for task in tasks.get("shutdown", []):