Skip to content
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
12 changes: 12 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ jobs:
backend:
name: Backend linting & testing
runs-on: ubuntu-latest
services:
redis:
image: redis:7-alpine
ports:
- 6379:6379
options: >-
--health-cmd "redis-cli ping"
--health-interval 10s
--health-timeout 5s
--health-retries 5
defaults:
run:
working-directory: backend
Expand All @@ -56,6 +66,8 @@ jobs:
- name: lint code
run: uv run --active ruff check .
- name: run tests
env:
REDIS_TEST_URL: redis://localhost:6379/15
run: |
mkdir -p ../coverage
uv run pytest --cov=app --cov-report=term-missing --cov-report=xml:../coverage/backend-coverage.xml
Expand Down
5 changes: 4 additions & 1 deletion backend/.env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
SUPABASE_URL=http://127.0.0.1:54321
DATABASE_URL=postgresql+asyncpg://postgres:postgres@127.0.0.1:54322/postgres
ENVIRONMENT=development
LOG_LEVEL=INFO
CORS_ORIGINS=https://app.example.com,https://admin.example.com
REDIS_URL=redis://127.0.0.1:6379
REDIS_URL=redis://127.0.0.1:6379
# regis test url, keep as is for local testing
REDIS_TEST_URL=redis://localhost:6379/15
1 change: 1 addition & 0 deletions backend/app/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Settings(BaseSettings):
# app name
APP_NAME: str = "WebMUN API"
ENVIRONMENT: str = "development"
LOG_LEVEL: str = "INFO"

# list of origins by separated commas (e.g., "app.com,app.xyz")
CORS_ORIGINS: str = ""
Expand Down
4 changes: 2 additions & 2 deletions backend/app/core/dep.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ def get_session_engine(connection: HTTPConnection) -> SessionEngine:
return connection.app.state.session_engine


def get_logger() -> logging.Logger:
return logging.getLogger("uvicorn.error")
def get_logger(connection: HTTPConnection) -> logging.Logger:
return connection.app.state.logger


def get_session_store(connection: HTTPConnection) -> Redis:
Expand Down
17 changes: 17 additions & 0 deletions backend/app/core/logging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import logging


def configure_logging(level: str) -> logging.Logger:
"""Configure the application logger once during startup."""
logger = logging.getLogger("webmun")
logger.setLevel(level.upper())
logger.propagate = False

if not logger.handlers:
handler = logging.StreamHandler()
handler.setFormatter(
logging.Formatter("%(asctime)s %(levelname)s %(name)s: %(message)s")
)
logger.addHandler(handler)

return logger
33 changes: 28 additions & 5 deletions backend/app/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from contextlib import asynccontextmanager
import asyncio
from contextlib import asynccontextmanager, suppress

import redis.asyncio as redis
from fastapi import FastAPI, Request
Expand All @@ -10,9 +11,11 @@
from app.core.config import get_settings
from app.core.database import create_db
from app.core.exceptions import AppException
from app.core.logging import configure_logging
from app.core.openapi import add_websocket_message_schemas
from app.session.engine import SessionEngine
from app.session.manager import ConnectionManager
from app.session.store import subscriber_worker
from app.session.views import router as session_router

settings = get_settings()
Expand All @@ -21,21 +24,41 @@
@asynccontextmanager
async def lifespan(app: FastAPI):
# startup phase
logger = configure_logging(settings.LOG_LEVEL)
app.state.logger = logger
engine, session_factory = create_db(settings)
app.state.db_engine = engine
app.state.db_session_factory = session_factory

app.state.session_engine = SessionEngine()
app.state.connection_manager = ConnectionManager()

app.state.redis = redis.from_url(
redis_client = redis.from_url(
settings.REDIS_URL.get_secret_value(),
)
await redis_client.ping()

app.state.redis = redis_client
logger.info("Connected to Redis")
task = asyncio.create_task(
subscriber_worker(
client=redis_client,
connection_manager=app.state.connection_manager,
logger=logger,
)
)

try:
yield

yield
finally:
task.cancel()
with suppress(asyncio.CancelledError):
await task

await app.state.redis.close()
await engine.dispose()
await app.state.redis.aclose()
await engine.dispose()
logger.info("Application shutdown complete")


app = FastAPI(
Expand Down
2 changes: 1 addition & 1 deletion backend/app/session/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def count_present_delegations(self, session_id: int) -> int:

async def broadcast_message(self, session_id: int, message: ServerSessionMessage):
"""Sends current state to all clients in the room"""
for connection in self.active_connections[session_id]:
for connection in self.active_connections.get(session_id, {}):
await connection.send_json(message.model_dump(mode="json"))

async def send_message(
Expand Down
50 changes: 31 additions & 19 deletions backend/app/session/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from .manager import ConnectionManager
from .models import (
DelegationContext,
DispatchOutcome,
RollCallContext,
SessionActor,
SessionLiveState,
Expand Down Expand Up @@ -58,11 +59,11 @@ async def build_actor(
if role == enums.SessionRole.DELEGATE:
if delegation_id is None:
raise ActorResolutionError("needs delegate id")
state = await store.get_state(session=redis, session_id=session_id)
if state is None:
res = await store.get_outcome(client=redis, session_id=session_id)
if res is None:
raise ActorResolutionError("session not found")

delegation = state.delegations.get(delegation_id)
delegation = res.state.delegations.get(delegation_id)
if delegation is None:
raise ActorResolutionError("delegation not found")

Expand Down Expand Up @@ -148,9 +149,8 @@ async def activate_session(

await session.commit()

await store.save_state(session=redis, state=live_state)

manager.active_connections.setdefault(committee_session_id, {})
await store.save_outcome(client=redis, result=DispatchOutcome(state=live_state))


async def pause_session(
Expand All @@ -177,11 +177,11 @@ async def prepare_session_connect(
redis: Redis,
) -> SessionActor:
"""Service that prepares for session connect.
Primarily used as a fallback in case the SessionLiveState is not in manager
Used as a fallback if redis/backend does not have the session_id
"""
live_state = await store.get_state(session=redis, session_id=committee_session_id)
res = await store.get_outcome(client=redis, session_id=committee_session_id)

if live_state is None:
if res is None:
stored_state = await repository.get_session_info(session, committee_session_id)
if stored_state is None:
raise SessionFetchError("Could not fetch session info")
Expand All @@ -194,7 +194,9 @@ async def prepare_session_connect(
live_state = SessionLiveState.model_validate(stored_state.state_snapshot)

# put SessionLiveState on Cache
live_state = await store.save_state(session=redis, state=live_state)
await store.save_outcome(client=redis, result=DispatchOutcome(state=live_state))

if manager.active_connections.get(committee_session_id) is None:
manager.active_connections.setdefault(committee_session_id, {})

actor = await build_actor(
Expand All @@ -216,14 +218,24 @@ async def handle_client_messages(
data: schemas.EventMessage,
redis: Redis,
):
event = data.event
state = await store.get_state(session=redis, session_id=session_id)

logger.info(event) # Debugging
lock = redis.lock(
f"webmun:session:{session_id}:lock",
timeout=5,
blocking_timeout=2,
)

try:
result = engine.dispatch(state, event, actor)
await store.save_state(session=redis, state=state)
return result
except EventRejectedError as exc:
return exc
# Lock for calculating in FSM without another worker overriding
async with lock:
event = data.event
outcome = await store.get_outcome(client=redis, session_id=session_id)
if outcome is None:
raise NotFoundError("Session state has not been found")

logger.info("Processing %s for session %s", event.type, session_id)

try:
result = engine.dispatch(outcome.state, event, actor)
await store.save_publish_outcome(client=redis, result=result)
return result
except EventRejectedError as exc:
return exc
70 changes: 62 additions & 8 deletions backend/app/session/store.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,73 @@
import asyncio
import logging

import redis.asyncio as redis

from app.session.models import SessionLiveState
from app.session.manager import ConnectionManager
from app.session.models import DispatchOutcome
from app.session.schemas import DispatchResultMessage

SESSION_STATE_CHANNEL = "webmun:session:updates"


def state_key(session_id: int) -> str:
return f"webmun:session:{session_id}"

async def get_state(session: redis.Redis, session_id: int) -> SessionLiveState | None:
res = await session.get(f"webmun:session:{session_id}")

async def get_outcome(client: redis.Redis, session_id: int) -> DispatchOutcome | None:
"""Get the current state for a session"""
res = await client.get(state_key(session_id))
if res is None:
return None

return SessionLiveState.model_validate_json(res)
return DispatchOutcome.model_validate_json(res)


async def save_outcome(client: redis.Redis, result: DispatchOutcome) -> None:
"""Only save outcome in the client cache"""
await client.set(state_key(result.state.session_id), result.model_dump_json())


async def save_publish_outcome(client: redis.Redis, result: DispatchOutcome) -> None:
"""Save outcome and publish to the channel"""
serialized_outcome = result.model_dump_json()
async with client.pipeline() as pipe:
pipe.set(state_key(result.state.session_id), serialized_outcome)
pipe.publish(SESSION_STATE_CHANNEL, result.model_dump_json())
await pipe.execute()


async def delete_outcome(client: redis.Redis, session_id: int) -> None:
await client.delete(state_key(session_id))

async def save_state(session: redis.Redis, state: SessionLiveState) -> None:
await session.set(f"webmun:session:{state.session_id}", state.model_dump_json())

async def subscriber_worker(
client: redis.Redis,
connection_manager: ConnectionManager,
logger: logging.Logger,
ready: asyncio.Event | None = None,
) -> None:
async with client.pubsub() as pubsub:
try:
await pubsub.subscribe(SESSION_STATE_CHANNEL)
logger.info("Subscribed to Redis channel %s", SESSION_STATE_CHANNEL)
if ready is not None:
ready.set()

async def delete_state(session: redis.Redis, session_id: int) -> None:
await session.delete(f"webmun:session:{session_id}")
async for message in pubsub.listen():
if message and message["type"] == "message":
try:
outcome = DispatchOutcome.model_validate_json(message["data"])
result = DispatchResultMessage(
state=outcome.state, effect=outcome.effect
)
await connection_manager.broadcast_message(
session_id=outcome.state.session_id, message=result
)
except Exception:
logger.exception("Failed to relay Redis session update")
except asyncio.CancelledError:
raise
except Exception:
logger.exception("Redis Pub/Sub worker stopped")
raise
Loading
Loading