From fade83a61edf6e4ea67ce8e0d71bf2573685810b Mon Sep 17 00:00:00 2001 From: Mel <97147377+MelissaAutumn@users.noreply.github.com> Date: Wed, 22 May 2024 13:13:06 -0700 Subject: [PATCH] if $REDIS_USE_CLUSTER = True then connect to a redis cluster (#417) --- backend/.env.example | 2 ++ .../src/appointment/controller/calendar.py | 7 ++--- .../src/appointment/dependencies/database.py | 30 ++++++++++++++----- backend/src/appointment/routes/api.py | 4 +-- 4 files changed, 30 insertions(+), 13 deletions(-) diff --git a/backend/.env.example b/backend/.env.example index 541c15619..0949ad5d7 100644 --- a/backend/.env.example +++ b/backend/.env.example @@ -99,6 +99,8 @@ REDIS_DB=0 # No value = Python None REDIS_PASSWORD REDIS_USE_SSL=False +# Connect to a redis cluster instead of a single instance +REDIS_USE_CLUSTER=False # In minutes, the time a cached remote event will expire at. REDIS_EVENT_EXPIRE_TIME=15 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)