Skip to content

Commit

Permalink
Merge pull request #8 from TranslatorSRI/redis_fixes
Browse files Browse the repository at this point in the history
Redis fixes
  • Loading branch information
maximusunc authored Jul 31, 2023
2 parents 5c58857 + 6cd6091 commit 352b544
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 20 deletions.
19 changes: 19 additions & 0 deletions app/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from typing import Optional

from pydantic import BaseSettings, AnyUrl


class Settings(BaseSettings):
openapi_server_url: Optional[AnyUrl]
openapi_server_maturity: str = "development"
openapi_server_location: str = "RENCI"
trapi_version: str = "1.4.0"
redis_host: str = "localhost"
redis_port: int = 6379
redis_password: str = "supersecretpassword"

class Config:
env_file = ".env"


settings = Settings()
14 changes: 8 additions & 6 deletions app/ordering_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@
import redis
from tqdm import tqdm

from .config import settings
from .clinical_evidence.compute_clinical_evidence import compute_clinical_evidence

REDIS_PSWD = os.getenv("REDIS_PSWD", "supersecretpassword")
redis_pool = redis.ConnectionPool(
host=settings.redis_host,
port=settings.redis_port,
db=0,
password=settings.redis_password,
)


def get_confidence(result, message, logger):
Expand Down Expand Up @@ -43,11 +49,7 @@ def get_novelty(result, message, logger):

def get_ordering_components(message, logger):
logger.debug(f"Computing scores for {len(message['results'])} results")
db_conn = redis.Redis(
host="0.0.0.0",
port=6379,
password=REDIS_PSWD,
)
db_conn = redis.Redis(connection_pool=redis_pool)
for result_index, result in enumerate(tqdm(message.get("results") or [])):
clinical_evidence_score = get_clinical_evidence(
result,
Expand Down
36 changes: 23 additions & 13 deletions app/server.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import logging
import redis
import traceback
import os

from fastapi import Body, BackgroundTasks
from fastapi import Body, BackgroundTasks, HTTPException, status
from fastapi.responses import JSONResponse
import httpx
from starlette.middleware.cors import CORSMiddleware
from uuid import uuid4

from reasoner_pydantic import AsyncQuery, AsyncQueryResponse, Response, Query

from .config import settings
from .logger import setup_logger, get_logger
from .trapi import TRAPI
from .ordering_components import get_ordering_components
Expand All @@ -20,7 +21,7 @@

openapi_args = dict(
title="Answer Appraiser",
version="0.2.1",
version="0.2.2",
terms_of_service="",
translator_component="Utility",
translator_teams=["Standards Reference Implementation Team"],
Expand All @@ -33,21 +34,16 @@
},
)

OPENAPI_SERVER_URL = os.getenv("OPENAPI_SERVER_URL")
OPENAPI_SERVER_MATURITY = os.getenv("OPENAPI_SERVER_MATURITY", "development")
OPENAPI_SERVER_LOCATION = os.getenv("OPENAPI_SERVER_LOCATION", "RENCI")
TRAPI_VERSION = os.getenv("TRAPI_VERSION", "1.4.0")

if OPENAPI_SERVER_URL:
if settings.openapi_server_url:
openapi_args["servers"] = [
{
"url": OPENAPI_SERVER_URL,
"x-maturity": OPENAPI_SERVER_MATURITY,
"x-location": OPENAPI_SERVER_LOCATION,
"url": settings.openapi_server_url,
"x-maturity": settings.openapi_server_maturity,
"x-location": settings.openapi_server_location,
},
]

openapi_args["trapi"] = TRAPI_VERSION
openapi_args["trapi"] = settings.trapi_version

APP = TRAPI(**openapi_args)

Expand Down Expand Up @@ -183,3 +179,17 @@ async def sync_get_appraisal(query: Query = Body(..., example=EXAMPLE)):
logger.error(f"Something went wrong while appraising: {traceback.format_exc()}")
logger.info("Done appraising")
return Response(message=message)


@APP.get("/redis_ready")
def check_redis_readiness():
"""Check if redis is started and ready to accept connections"""
try:
r = redis.Redis(
host=settings.redis_host,
port=settings.redis_port,
password=settings.redis_password,
)
r.keys()
except Exception:
raise HTTPException(status_code=status.HTTP_503_SERVICE_UNAVAILABLE)
2 changes: 2 additions & 0 deletions requirements-lock.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
anyio==3.7.0
asgiref==3.7.2
async-timeout==4.0.2
certifi==2023.5.7
click==7.1.2
exceptiongroup==1.1.1
Expand All @@ -15,6 +16,7 @@ pydantic==1.10.9
python-dotenv==1.0.0
PyYAML==6.0
reasoner-pydantic==4.0.8
redis==4.6.0
sniffio==1.3.0
starlette==0.17.1
tqdm==4.65.0
Expand Down
4 changes: 3 additions & 1 deletion requirements-test-lock.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
async-timeout==4.0.2
attrs==23.1.0
certifi==2023.5.7
chardet==4.0.0
coverage==7.2.7
fakeredis==2.17.0
idna==2.10
iniconfig==2.0.0
fakeredis==2.17.0
packaging==23.1
pluggy==0.13.1
py==1.11.0
pytest==6.2.2
pytest-asyncio==0.16.0
pytest-cov==2.11.1
redis==4.6.0
requests==2.25.1
sortedcontainers==2.4.0
toml==0.10.2
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ gunicorn==20.1.0
httpx==0.24.1
numpy==1.25.1
reasoner-pydantic==4.0.8
redis==4.6.0
tqdm==4.65.0
uvicorn==0.13.3

0 comments on commit 352b544

Please sign in to comment.