diff --git a/backend/src/appointment/controller/calendar.py b/backend/src/appointment/controller/calendar.py index 4b981ed64..77ac7a451 100644 --- a/backend/src/appointment/controller/calendar.py +++ b/backend/src/appointment/controller/calendar.py @@ -4,13 +4,12 @@ """ import json import logging -import uuid import zoneinfo import os import caldav.lib.error import requests -from redis import Redis +from redis import Redis, RedisCluster from caldav import DAVClient from fastapi import BackgroundTasks from google.oauth2.credentials import Credentials @@ -29,11 +28,11 @@ class BaseConnector: - redis_instance: Redis | None + redis_instance: Redis | RedisCluster | None subscriber_id: int calendar_id: int - def __init__(self, subscriber_id: int, calendar_id: int | None, redis_instance: Redis | None = None): + def __init__(self, subscriber_id: int, calendar_id: int | None, redis_instance: Redis | RedisCluster | None = None): self.redis_instance = redis_instance self.subscriber_id = subscriber_id self.calendar_id = calendar_id diff --git a/backend/src/appointment/dependencies/database.py b/backend/src/appointment/dependencies/database.py index 6f6f435dc..67d5ecc62 100644 --- a/backend/src/appointment/dependencies/database.py +++ b/backend/src/appointment/dependencies/database.py @@ -1,6 +1,6 @@ import os -from redis import Redis +from redis import Redis, RedisCluster from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker @@ -29,17 +29,33 @@ def get_db(): db.close() -def get_redis() -> Redis | None: +def get_redis() -> Redis | RedisCluster | None: """Retrieves a redis instance or None if redis isn't available.""" # TODO: Create pool and simply grab instance? if os.getenv('REDIS_URL') is None: return None + host = os.getenv('REDIS_URL') + port = int(os.getenv('REDIS_PORT')) + db = os.getenv('REDIS_DB') + password = os.getenv('REDIS_PASSWORD') + ssl = os.getenv('REDIS_USE_SSL') + + if os.getenv('REDIS_USE_CLUSTER'): + return RedisCluster( + host=host, + port=port, + db=db, + password=password, + ssl=ssl, + decode_responses=True, + ) + return Redis( - host=os.getenv('REDIS_URL'), - port=os.getenv('REDIS_PORT'), - db=os.getenv('REDIS_DB'), - password=os.getenv('REDIS_PASSWORD'), - ssl=os.getenv('REDIS_USE_SSL'), + host=host, + port=port, + db=db, + password=password, + ssl=ssl, decode_responses=True, ) diff --git a/backend/src/appointment/routes/api.py b/backend/src/appointment/routes/api.py index 0e00a7351..8db06f425 100644 --- a/backend/src/appointment/routes/api.py +++ b/backend/src/appointment/routes/api.py @@ -5,7 +5,7 @@ import requests.exceptions import validators -from redis import Redis +from redis import Redis, RedisCluster from requests import HTTPError from sentry_sdk import capture_exception from sqlalchemy.exc import SQLAlchemyError @@ -316,7 +316,7 @@ def read_remote_events( db: Session = Depends(get_db), google_client: GoogleClient = Depends(get_google_client), subscriber: Subscriber = Depends(get_subscriber), - redis_instance: Redis | None = Depends(get_redis), + redis_instance: Redis | RedisCluster | None = Depends(get_redis), ): """endpoint to get events in a given date range from a remote calendar""" db_calendar = repo.calendar.get(db, calendar_id=id)