Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove deprecated lifecycle and consolidate startup actions #3311

Merged
merged 4 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 0 additions & 10 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 0 additions & 2 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down
3 changes: 0 additions & 3 deletions docker/entry.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
70 changes: 46 additions & 24 deletions mealie/app.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -39,12 +42,55 @@
- [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)
Expand Down Expand Up @@ -103,30 +149,6 @@ def api_routers():
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",
Expand Down
2 changes: 1 addition & 1 deletion mealie/db/init_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

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:
Expand Down