1
0
mirror of https://github.com/ijaric/voice_assistant.git synced 2025-05-24 22:43:26 +00:00

Запуск через lifespan

This commit is contained in:
Григорич 2023-09-27 17:24:06 +03:00
parent 0cc0b7f0bc
commit 37f3bce194
2 changed files with 10 additions and 11 deletions

View File

@ -1,5 +1,6 @@
import logging import logging
import logging.config as logging_config import logging.config as logging_config
from contextlib import asynccontextmanager
import fastapi import fastapi
@ -18,6 +19,13 @@ class Application:
self.logger = logging.getLogger(__name__) self.logger = logging.getLogger(__name__)
self.producer = None self.producer = None
@asynccontextmanager
async def lifespan(self, app: fastapi.FastAPI):
self.logger.info("Starting server")
yield
# Clean up the ML models and release the resources
self.logger.info("Shutting down server")
def setup_application(self, app: fastapi.FastAPI) -> fastapi.FastAPI: def setup_application(self, app: fastapi.FastAPI) -> fastapi.FastAPI:
logger.info("Initializing application") logger.info("Initializing application")
@ -58,17 +66,10 @@ class Application:
docs_url="/api/openapi", docs_url="/api/openapi",
openapi_url="/api/openapi.json", openapi_url="/api/openapi.json",
default_response_class=fastapi.responses.ORJSONResponse, default_response_class=fastapi.responses.ORJSONResponse,
lifespan=self.lifespan,
) )
app = self.setup_application(app) app = self.setup_application(app)
@app.on_event("startup")
async def startup_event():
self.logger.info("Starting server")
@app.on_event("shutdown")
async def shutdown_event():
self.logger.info("Shutting down server")
app.state.settings = self.settings app.state.settings = self.settings
return app return app

View File

@ -21,6 +21,4 @@ class Settings(pydantic_settings.BaseSettings):
settings = Settings() # todo Вынести в инициализацию settings = Settings() # todo Вынести в инициализацию
logging_config.dictConfig( logging_config.dictConfig(app_split_settings.get_logging_config(**settings.logger.model_dump()))
app_split_settings.get_logging_config(**settings.logger.model_dump())
)