Skip to content

store db reader/writer dependencies in starlette app.state #3

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

Merged
merged 4 commits into from
Aug 12, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions stac_api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from . import settings
from .resources import mgmt, collection, conformance, item
from .utils import dependencies


app = FastAPI()
Expand All @@ -18,18 +17,18 @@
@app.on_event("startup")
async def on_startup():
"""Create database engines and sessions on startup"""
dependencies.ENGINE_READER = create_engine(settings.SQLALCHEMY_DATABASE_READER)
dependencies.ENGINE_WRITER = create_engine(settings.SQLALCHEMY_DATABASE_WRITER)
dependencies.DB_READER = sessionmaker(
autocommit=False, autoflush=False, bind=dependencies.ENGINE_READER
app.state.ENGINE_READER = create_engine(settings.SQLALCHEMY_DATABASE_READER)
app.state.ENGINE_WRITER = create_engine(settings.SQLALCHEMY_DATABASE_WRITER)
app.state.DB_READER = sessionmaker(
autocommit=False, autoflush=False, bind=app.state.ENGINE_READER
)
dependencies.DB_WRITER = sessionmaker(
autocommit=False, autoflush=False, bind=dependencies.ENGINE_WRITER
app.state.DB_WRITER = sessionmaker(
autocommit=False, autoflush=False, bind=app.state.ENGINE_WRITER
)


@app.on_event("shutdown")
async def on_shutdown():
"""Dispose of database engines and sessions on app shutdown"""
dependencies.ENGINE_READER.dispose()
dependencies.ENGINE_WRITER.dispose()
app.state.ENGINE_READER.dispose()
app.state.ENGINE_WRITER.dispose()
25 changes: 5 additions & 20 deletions stac_api/utils/dependencies.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
from dataclasses import dataclass
from typing import Callable, List, Optional
from typing import Callable, List

from sqlalchemy.engine import Engine
from sqlalchemy.orm import Session
from starlette.requests import Request


ENGINE_READER: Optional[Engine] = None
ENGINE_WRITER: Optional[Engine] = None
DB_READER: Optional[Session] = None
DB_WRITER: Optional[Session] = None


@dataclass
class DatabaseConnectionError(Exception):
message: str
Expand All @@ -32,27 +25,19 @@ def _parse(request: Request):
return _parse


def database_reader_factory() -> Session:
def database_reader_factory(request: Request) -> Session:
"""Instantiate the database reader session"""
try:
if not DB_READER:
raise DatabaseConnectionError(
message="Database engine has not been created"
)
db = DB_READER()
db = request.app.state.DB_READER()
yield db
finally:
db.close()


def database_writer_factory() -> Session:
def database_writer_factory(request: Request) -> Session:
"""Instantiate the database writer session"""
try:
if not DB_WRITER:
raise DatabaseConnectionError(
message="Database engine has not been created"
)
db = DB_WRITER()
db = request.app.state.DB_WRITER()
yield db
finally:
db.close()