Skip to content

Commit

Permalink
Changes after review
Browse files Browse the repository at this point in the history
  • Loading branch information
anders-kiaer committed Oct 24, 2023
1 parent f798d15 commit f2febab
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
21 changes: 16 additions & 5 deletions backend/src/backend/primary/user_session_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,38 @@
from starlette.responses import StreamingResponse
from starlette.background import BackgroundTask

from src import config
from src.services.utils.authenticated_user import AuthenticatedUser

LOCALHOST_DEVELOPMENT = os.environ.get("UVICORN_RELOAD") == "true"


class _RedisUserJobs:
def __init__(self) -> None:
# redis.Redis does not yet have namespace support - https://github.com/redis/redis-py/issues/12 - need to prefix manually.
self._redis_client = redis.Redis.from_url(config.REDIS_URL, decode_responses=True)

def get_job_name(self, user_id: str) -> Optional[str]:
return self._redis_client.get("user-job-name:" + user_id)

def set_job_name(self, user_id: str, job_name: str) -> None:
self._redis_client.set("user-job-name:" + user_id, job_name)


class RadixJobScheduler:
"""Utility class to help with spawning Radix jobs on demand,
and provide correct URL to communicate with running Radix jobs"""

def __init__(self, name: str, port: int) -> None:
self._name = name
self._port = port

# redis.Redis does not yet have namespace support - https://github.com/redis/redis-py/issues/12 - need to prefix manually.
self._redis_client = redis.Redis(host="redis-user-session", port=6379, decode_responses=True)
self._redis_user_jobs = _RedisUserJobs()

def _get_job_name(self, user_id: str) -> Optional[str]:
return self._redis_client.get("user-job-name-" + user_id)
return self._redis_user_jobs.get_job_name(user_id)

def _set_job_name(self, user_id: str, job_name: str) -> None:
self._redis_client.set("user-job-name-" + user_id, job_name)
self._redis_user_jobs.set_job_name(user_id, job_name)

async def _active_running_job(self, user_id: str) -> bool:
"""Returns true if there already is a running job for logged in user."""
Expand Down
2 changes: 1 addition & 1 deletion backend/src/backend/shared_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ def add_shared_middlewares(app: FastAPI) -> None:
paths_redirected_to_login=paths_redirected_to_login,
)

session_store = RedisStore(config.REDIS_URL, prefix="user-auth-")
session_store = RedisStore(config.REDIS_URL, prefix="user-auth:")
app.add_middleware(SessionMiddleware, store=session_store)

0 comments on commit f2febab

Please sign in to comment.