Skip to content

Commit

Permalink
fix: merge conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
Sujit committed Oct 14, 2024
2 parents f0bbe76 + c5dc954 commit bdc17fe
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 35 deletions.
4 changes: 3 additions & 1 deletion src/backend/app/db/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@

async def get_db_connection_pool() -> AsyncConnectionPool:
"""Get the connection pool for psycopg."""
return AsyncConnectionPool(conninfo=settings.DTM_DB_URL.unicode_string())
pool = AsyncConnectionPool(conninfo=settings.DTM_DB_URL.unicode_string())
await pool.open() # Explicitly open the pool
return pool


async def get_db(request: Request) -> AsyncGenerator[Connection, None]:
Expand Down
23 changes: 9 additions & 14 deletions src/backend/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@
from fastapi.middleware.cors import CORSMiddleware
from fastapi.templating import Jinja2Templates
from fastapi.responses import RedirectResponse, JSONResponse

from psycopg_pool import AsyncConnectionPool
from app.config import settings
from app.projects import project_routes
from app.drones import drone_routes
from app.waypoints import waypoint_routes
from app.users import user_routes
from app.tasks import task_routes
from app.db.database import get_db_connection_pool


root = os.path.dirname(os.path.abspath(__file__))
Expand Down Expand Up @@ -105,23 +104,19 @@ def get_application() -> FastAPI:


@asynccontextmanager
async def lifespan(
app: FastAPI,
):
async def lifespan(app: FastAPI):
"""FastAPI startup/shutdown event."""
log.debug("Starting up FastAPI server.")

db_pool = await get_db_connection_pool()
await db_pool.open()
# Create a pooled db connection and make available in app state
# NOTE we can access 'request.app.state.db_pool' in endpoints
app.state.db_pool = db_pool

yield
async with AsyncConnectionPool(
conninfo=settings.DTM_DB_URL.unicode_string()
) as db_pool:
# The pool is now used within the context manager
app.state.db_pool = db_pool
yield # FastAPI will run the application here

# Shutdown events
# Pool will be closed automatically when the context manager exits
log.debug("Shutting down FastAPI server.")
await app.state.db_pool.close()


api = get_application()
Expand Down
44 changes: 25 additions & 19 deletions src/backend/pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/backend/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ dependencies = [
"GDAL==3.6.2",
"aiosmtplib>=3.0.1",
"python-slugify>=8.0.4",
"drone-flightplan==0.3.1rc4",
"psycopg2>=2.9.9",
"pyodm>=1.5.11",
"asgiref>=3.8.1",
"drone-flightplan>=0.3.1",
]
requires-python = ">=3.11"
license = {text = "GPL-3.0-only"}
Expand Down

0 comments on commit bdc17fe

Please sign in to comment.