Skip to content

Commit

Permalink
if $REDIS_USE_CLUSTER = True then connect to a redis cluster (#417)
Browse files Browse the repository at this point in the history
  • Loading branch information
MelissaAutumn authored May 22, 2024
1 parent f6c9c01 commit fade83a
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
2 changes: 2 additions & 0 deletions backend/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions backend/src/appointment/controller/calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
30 changes: 23 additions & 7 deletions backend/src/appointment/dependencies/database.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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,
)
4 changes: 2 additions & 2 deletions backend/src/appointment/routes/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit fade83a

Please sign in to comment.