"""This module defines the lifespan manager for the FastAPI application."""importasyncioimportloggingfromcontextlibimportasynccontextmanagerfromfastapiimportFastAPIfromlean_server.configimportConfigfromlean_server.database.proofimportProofDatabasefromlean_server.manager.proof_managerimportProofManagerlogger=logging.getLogger(__name__)
[docs]defget_lifespan(*,config:Config):""" Returns an asynchronous context manager for the application's lifespan. This function sets up resources on startup and cleans them up on shutdown. It initializes the database, proof manager, and other application state. Args: config: The application configuration. Returns: An async context manager function. """@asynccontextmanagerasyncdeflifespan(app:FastAPI):""" The actual lifespan context manager. Args: app: The FastAPI application instance. """# Startuplogger.info("Starting Lean Server")app.state.lean_semaphore=asyncio.Semaphore(config.lean.concurrency)app.state.background_tasks=set()app.state.config=config# Initialize databaseapp.state.proof_database=ProofDatabase(database_path=config.sqlite.database_path,timeout=config.sqlite.timeout,)awaitapp.state.proof_database.create_table()# Initialize proof managerapp.state.proof_manager=ProofManager(proof_database=app.state.proof_database,lean_semaphore=app.state.lean_semaphore,background_tasks=app.state.background_tasks,)yield# Shutdownlogger.info("Lean Server is shutting down")returnlifespan