Skip to content

Commit

Permalink
Merge branch 'main' into pointer-up-bug
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenthoms authored Oct 24, 2023
2 parents 5d7d2d1 + 98ccb70 commit aa9b983
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 27 deletions.
31 changes: 25 additions & 6 deletions backend/src/backend/primary/user_session_proxy.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,50 @@
import os
import asyncio
from typing import Any
from typing import Any, Optional

import httpx
import redis
from starlette.requests import Request
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
self._existing_job_names = redis.Redis(host="redis-user-session-state", port=6380, decode_responses=True)
self._redis_user_jobs = _RedisUserJobs()

def _get_job_name(self, user_id: str) -> Optional[str]:
return self._redis_user_jobs.get_job_name(user_id)

def _set_job_name(self, user_id: str, job_name: str) -> None:
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."""

existing_job_name = self._existing_job_names.get(user_id)
existing_job_name = self._get_job_name(user_id)
if not existing_job_name:
return False
if LOCALHOST_DEVELOPMENT:
Expand Down Expand Up @@ -53,7 +72,7 @@ async def _create_new_job(self, user_id: str) -> None:
same name."""

if LOCALHOST_DEVELOPMENT:
self._existing_job_names.set(user_id, self._name)
self._set_job_name(user_id, self._name)
else:
print(f"Requesting new user session container for user {user_id}.")
async with httpx.AsyncClient() as client:
Expand All @@ -74,7 +93,7 @@ async def _create_new_job(self, user_id: str) -> None:
}
},
)
self._existing_job_names.set(user_id, res.json()["name"])
self._set_job_name(user_id, res.json()["name"])

while not await self._active_running_job(user_id):
# It takes a couple of seconds before Radix job uvicorn process has
Expand All @@ -87,7 +106,7 @@ async def get_base_url(self, user_id: str) -> str:
if not await self._active_running_job(user_id):
await self._create_new_job(user_id)

job_name = self._existing_job_names.get(user_id)
job_name = self._get_job_name(user_id)

return f"http://{job_name}:{self._port}"

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)
session_store = RedisStore(config.REDIS_URL, prefix="user-auth:")
app.add_middleware(SessionMiddleware, store=session_store)
2 changes: 1 addition & 1 deletion backend/src/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@

print(f"{RESOURCE_SCOPES_DICT=}")

REDIS_URL = "redis://redis-login-state:6379"
REDIS_URL = "redis://redis-user-session:6379"
SESSION_STORAGE = "redis"
2 changes: 1 addition & 1 deletion docker-compose-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ services:
volumes:
- ./backend/src:/home/appuser/backend/src

redis:
redis-user-session:
image: "bitnami/redis:6.2.10@sha256:bd42fcdab5959ce2b21b6ea8410d4b3ee87ecb2e320260326ec731ecfcffbd0e"
expose:
- "6379"
Expand Down
10 changes: 1 addition & 9 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,7 @@ services:
volumes:
- ./backend/src:/home/appuser/backend/src

redis-user-session-state:
image: "bitnami/redis:6.2.10@sha256:bd42fcdab5959ce2b21b6ea8410d4b3ee87ecb2e320260326ec731ecfcffbd0e"
expose:
- "6380"
environment:
- ALLOW_EMPTY_PASSWORD=yes
- REDIS_PORT_NUMBER=6380

redis-login-state:
redis-user-session:
image: "bitnami/redis:6.2.10@sha256:bd42fcdab5959ce2b21b6ea8410d4b3ee87ecb2e320260326ec731ecfcffbd0e"
expose:
- "6379"
Expand Down
10 changes: 1 addition & 9 deletions radixconfig.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,13 @@ spec:
variables:
UVICORN_PORT: 5000
UVICORN_ENTRYPOINT: src.backend.primary.main:app
- name: redis-login-state
- name: redis-user-session
image: bitnami/redis:6.2.10@sha256:bd42fcdab5959ce2b21b6ea8410d4b3ee87ecb2e320260326ec731ecfcffbd0e
ports:
- name: http
port: 6379
variables:
ALLOW_EMPTY_PASSWORD: yes
- name: redis-user-session-state
image: bitnami/redis:6.2.10@sha256:bd42fcdab5959ce2b21b6ea8410d4b3ee87ecb2e320260326ec731ecfcffbd0e
ports:
- name: http
port: 6380
variables:
ALLOW_EMPTY_PASSWORD: yes
REDIS_PORT_NUMBER: 6380

jobs:
- name: backend-user-session
Expand Down

0 comments on commit aa9b983

Please sign in to comment.