diff --git a/.vscode/tasks.json b/.vscode/tasks.json index f73e066457a6..1e0eafe61b2f 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -24,16 +24,6 @@ }, "problemMatcher": [] }, - { - "label": "Init Database", - "command": "poetry run python mealie/db/init_db.py", - "type": "shell", - "presentation": { - "reveal": "always", - "group": "groupA" - }, - "problemMatcher": [] - }, { "label": "Dev: Start Frontend", "command": "task ui", diff --git a/Taskfile.yml b/Taskfile.yml index a1f67ff17a38..6df84dd94d17 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -132,7 +132,6 @@ tasks: py: desc: runs the backend server cmds: - - poetry run python mealie/db/init_db.py - poetry run python mealie/app.py py:postgres: @@ -145,7 +144,6 @@ tasks: POSTGRES_PORT: 5432 POSTGRES_DB: mealie cmds: - - poetry run python mealie/db/init_db.py - poetry run python mealie/app.py ui:build: diff --git a/docker/entry.sh b/docker/entry.sh index 5bc077afc4da..976f4b8c5114 100644 --- a/docker/entry.sh +++ b/docker/entry.sh @@ -33,9 +33,6 @@ init() { # Activate our virtual environment here . /opt/pysetup/.venv/bin/activate - - # Initialize Database Prerun - poetry run python /app/mealie/db/init_db.py } change_user diff --git a/mealie/app.py b/mealie/app.py index d9705e563321..63434b445ecf 100644 --- a/mealie/app.py +++ b/mealie/app.py @@ -1,3 +1,6 @@ +from collections.abc import AsyncGenerator +from contextlib import asynccontextmanager + import uvicorn from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware @@ -39,12 +42,55 @@ community members. If you'd like to file an issue, please use the - [Beta](https://demo.mealie.io) """ +logger = get_logger() + + +@asynccontextmanager +async def lifespan_fn(_: FastAPI) -> AsyncGenerator[None, None]: + """ + lifespan_fn controls the startup and shutdown of the FastAPI Application. + This function is called when the FastAPI application starts and stops. + + See FastAPI documentation for more information: + - https://fastapi.tiangolo.com/advanced/events/ + """ + logger.info("start: database initialization") + import mealie.db.init_db as init_db + + init_db.main() + logger.info("end: database initialization") + + await start_scheduler() + + logger.info("-----SYSTEM STARTUP-----") + logger.info("------APP SETTINGS------") + logger.info( + settings.model_dump_json( + indent=4, + exclude={ + "SECRET", + "SFTP_PASSWORD", + "SFTP_USERNAME", + "DB_URL", # replace by DB_URL_PUBLIC for logs + "DB_PROVIDER", + "SMTP_USER", + "SMTP_PASSWORD", + }, + ) + ) + + yield + + logger.info("-----SYSTEM SHUTDOWN----- \n") + + app = FastAPI( title="Mealie", description=description, version=APP_VERSION, docs_url=settings.DOCS_URL, redoc_url=settings.REDOC_URL, + lifespan=lifespan_fn, ) app.add_middleware(GZipMiddleware, minimum_size=1000) @@ -103,30 +149,6 @@ for route in app.routes: route.tags = list(set(route.tags)) -@app.on_event("startup") -async def system_startup(): - logger = get_logger() - - await start_scheduler() - - logger.info("-----SYSTEM STARTUP----- \n") - logger.info("------APP SETTINGS------") - logger.info( - settings.model_dump_json( - indent=4, - exclude={ - "SECRET", - "SFTP_PASSWORD", - "SFTP_USERNAME", - "DB_URL", # replace by DB_URL_PUBLIC for logs - "DB_PROVIDER", - "SMTP_USER", - "SMTP_PASSWORD", - }, - ) - ) - - def main(): uvicorn.run( "app:app", diff --git a/mealie/db/init_db.py b/mealie/db/init_db.py index 6f94ee962023..472e183fe505 100644 --- a/mealie/db/init_db.py +++ b/mealie/db/init_db.py @@ -22,7 +22,7 @@ from mealie.services.group_services.group_service import GroupService PROJECT_DIR = Path(__file__).parent.parent.parent -logger = root_logger.get_logger("init_db") +logger = root_logger.get_logger() def init_db(db: AllRepositories) -> None: