Fix redis setup (#11)

This commit is contained in:
Nikhil Rao 2022-11-21 15:32:51 -08:00 committed by GitHub
parent b49cc9fbb6
commit 1f817c637f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 24 deletions

View File

@ -59,7 +59,7 @@ class App(Base):
self.middleware.append(HydrateMiddleware())
# Set up the state manager.
self.state_manager.set(state=self.state)
self.state_manager.setup(state=self.state)
# Set up the API.
self.api = fastapi.FastAPI()

View File

@ -451,9 +451,6 @@ class StateUpdate(Base):
events: List[Event] = []
redis = None
class StateManager(Base):
"""A class to manage many client states."""
@ -466,16 +463,13 @@ class StateManager(Base):
# The token expiration time (s).
token_expiration: int = constants.TOKEN_EXPIRATION
def __init__(self, *args, **kwargs):
"""Initialize the state manager.
# The redis client to use.
redis: Any = None
Args:
*args: Args to pass to the base class.
**kwargs: Kwargs to pass to the base class.
"""
super().__init__(*args, **kwargs)
global redis
redis = utils.get_redis()
def setup(self, state: Type[State]):
"""Setup the state manager."""
self.state = state
self.redis = utils.get_redis()
def get_state(self, token: str) -> State:
"""Get the state for a token.
@ -486,8 +480,8 @@ class StateManager(Base):
Returns:
The state for the token.
"""
if redis is not None:
redis_state = redis.get(token)
if self.redis is not None:
redis_state = self.redis.get(token)
if redis_state is None:
self.set_state(token, self.state())
return self.get_state(token)
@ -504,6 +498,6 @@ class StateManager(Base):
token: The token to set the state for.
state: The state to set.
"""
if redis is None:
if self.redis is None:
return
redis.set(token, pickle.dumps(state), ex=self.token_expiration)
self.redis.set(token, pickle.dumps(state), ex=self.token_expiration)

View File

@ -860,12 +860,12 @@ def get_redis():
"""
try:
import redis
config = get_config()
if config.redis_url is None:
return None
redis_url, redis_port = config.redis_url.split(":")
print("Using redis at", config.redis_url)
return redis.Redis(host=redis_url, port=int(redis_port), db=0)
except:
return None
config = get_config()
if config.redis_url is None:
return None
redis_url, redis_port = config.redis_url.split(":")
print("Using redis at", config.redis_url)
return redis.Redis(host=redis_url, port=int(redis_port), db=0)