Skip to content

Commit

Permalink
refactor: add worker to app lifespan to avoid running when importing
Browse files Browse the repository at this point in the history
  • Loading branch information
zietzm committed Aug 29, 2024
1 parent 72a1645 commit db89864
Show file tree
Hide file tree
Showing 5 changed files with 444 additions and 27 deletions.
9 changes: 8 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ authors = [
{ name = "zietzm", email = "michael.zietz@gmail.com" }
]
dependencies = [
"fastapi>=0.111.0",
"fastapi[standard]>=0.111.0",
"boto3>=1.34.145",
"botocore>=1.34.145",
"pydantic>=2.8.2",
Expand Down Expand Up @@ -50,3 +50,10 @@ packages = ["src/webgwas_backend"]

[tool.ruff.lint.pydocstyle]
convention = "google"

[tool.uv]
dev-dependencies = [
"pytest>=8.3.2",
"ruff>=0.6.2",
"httpx>=0.27.2",
]
19 changes: 17 additions & 2 deletions requirements-dev.lock
Original file line number Diff line number Diff line change
Expand Up @@ -53,28 +53,36 @@ idna==3.7
# via anyio
# via email-validator
# via httpx
igwas @ git+https://github.com/tatonetti-lab/indirect-gwas@e0546ff0fe39624cfab9bd02cec80dcdd14e5af7
# via webgwas
iniconfig==2.0.0
# via pytest
jinja2==3.1.4
# via fastapi
jmespath==1.0.1
# via boto3
# via botocore
joblib==1.4.2
# via scikit-learn
markdown-it-py==3.0.0
# via rich
markupsafe==2.1.5
# via jinja2
mdurl==0.1.2
# via markdown-it-py
numpy==2.0.1
numpy==2.1.0
# via igwas
# via pandas
# via pyarrow
# via scikit-learn
# via scipy
# via webgwas
orjson==3.10.6
# via fastapi
packaging==24.1
# via pytest
pandas==2.2.2
# via igwas
# via webgwas
# via webgwas-backend
pluggy==1.5.0
Expand Down Expand Up @@ -114,6 +122,10 @@ rich==13.7.1
ruff==0.6.2
s3transfer==0.10.2
# via boto3
scikit-learn==1.5.1
# via webgwas-backend
scipy==1.14.1
# via scikit-learn
shellingham==1.5.4
# via typer
six==1.16.0
Expand All @@ -127,6 +139,8 @@ sqlmodel==0.0.21
# via webgwas-backend
starlette==0.37.2
# via fastapi
threadpoolctl==3.5.0
# via scikit-learn
typer==0.12.3
# via fastapi-cli
# via webgwas-backend
Expand All @@ -148,9 +162,10 @@ uvloop==0.19.0
# via uvicorn
watchfiles==0.22.0
# via uvicorn
webgwas @ git+https://github.com/zietzm/webgwas.git@bb627b570e507222accd2fdf08a896565835db0d
webgwas @ git+https://github.com/zietzm/webgwas.git@fa93c0bbb02ab92c03af5975bbb36770f7c70445
# via webgwas-backend
websockets==12.0
# via uvicorn
zstandard==0.23.0
# via igwas
# via webgwas-backend
19 changes: 17 additions & 2 deletions requirements.lock
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,32 @@ idna==3.7
# via anyio
# via email-validator
# via httpx
igwas @ git+https://github.com/tatonetti-lab/indirect-gwas@e0546ff0fe39624cfab9bd02cec80dcdd14e5af7
# via webgwas
jinja2==3.1.4
# via fastapi
jmespath==1.0.1
# via boto3
# via botocore
joblib==1.4.2
# via scikit-learn
markdown-it-py==3.0.0
# via rich
markupsafe==2.1.5
# via jinja2
mdurl==0.1.2
# via markdown-it-py
numpy==2.0.1
numpy==2.1.0
# via igwas
# via pandas
# via pyarrow
# via scikit-learn
# via scipy
# via webgwas
orjson==3.10.6
# via fastapi
pandas==2.2.2
# via igwas
# via webgwas
# via webgwas-backend
polars==1.3.0
Expand Down Expand Up @@ -106,6 +114,10 @@ rich==13.7.1
# via webgwas-backend
s3transfer==0.10.2
# via boto3
scikit-learn==1.5.1
# via webgwas-backend
scipy==1.14.1
# via scikit-learn
shellingham==1.5.4
# via typer
six==1.16.0
Expand All @@ -119,6 +131,8 @@ sqlmodel==0.0.21
# via webgwas-backend
starlette==0.37.2
# via fastapi
threadpoolctl==3.5.0
# via scikit-learn
typer==0.12.3
# via fastapi-cli
# via webgwas-backend
Expand All @@ -140,9 +154,10 @@ uvloop==0.19.0
# via uvicorn
watchfiles==0.22.0
# via uvicorn
webgwas @ git+https://github.com/zietzm/webgwas.git@bb627b570e507222accd2fdf08a896565835db0d
webgwas @ git+https://github.com/zietzm/webgwas.git@fa93c0bbb02ab92c03af5975bbb36770f7c70445
# via webgwas-backend
websockets==12.0
# via uvicorn
zstandard==0.23.0
# via igwas
# via webgwas-backend
26 changes: 21 additions & 5 deletions src/webgwas_backend/main.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import logging
from collections.abc import Generator
from contextlib import asynccontextmanager
from functools import lru_cache
from typing import Annotated

import webgwas.phenotype_definitions
from fastapi import Depends, FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from sqlalchemy import Engine
from sqlmodel import Session, select
from webgwas.phenotype_definitions import Field, KnowledgeBase

Expand All @@ -27,22 +30,35 @@
from webgwas_backend.worker import Worker

logger = logging.getLogger("uvicorn")
logger.setLevel(logging.INFO)
logger.setLevel(logging.DEBUG)

init_db()
worker_group = Worker(settings)

worker: Worker | None = None


@asynccontextmanager
async def lifespan(app: FastAPI): # noqa: ARG001
global worker
worker = Worker(settings)
yield


@lru_cache(maxsize=1)
def get_worker() -> Worker:
return worker_group
return Worker(settings)


def get_engine() -> Engine:
return engine


def get_session() -> Generator[Session]:
def get_session(engine: Annotated[Engine, Depends(get_engine)]) -> Generator[Session]:
with Session(engine) as session:
yield session


app = FastAPI()
app = FastAPI(lifespan=lifespan)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
Expand Down
Loading

0 comments on commit db89864

Please sign in to comment.