Skip to content

Commit

Permalink
add redis in startup event hook
Browse files Browse the repository at this point in the history
  • Loading branch information
russbiggs committed Sep 15, 2023
1 parent b20ceba commit f5ec281
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
36 changes: 19 additions & 17 deletions openaq_api/openaq_api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,22 +108,6 @@ def render(self, content: Any) -> bytes:

redis_client = None # initialize for generalize_schema.py

if settings.RATE_LIMITING:
logger.debug("Connecting to redis")
from redis.asyncio.cluster import RedisCluster

try:
redis_client = RedisCluster(
host=settings.REDIS_HOST,
port=settings.REDIS_PORT,
decode_responses=True,
socket_timeout=5,
)
app.state.redis_client = redis_client
except Exception as e:
logging.error(InfrastructureErrorLog(detail=f"failed to connect to redis: {e}"))
logger.debug("Redis connected")


app.add_middleware(
CORSMiddleware,
Expand Down Expand Up @@ -208,7 +192,24 @@ async def startup_event():
logger.debug("initializing connection pool")
app.state.pool = await db_pool(None)
logger.debug("Connection pool established")

if not hasattr(app.state, "redis_client"):
if settings.RATE_LIMITING:
logger.debug("Connecting to redis")
from redis.asyncio.cluster import RedisCluster

try:
redis_client = RedisCluster(
host=settings.REDIS_HOST,
port=settings.REDIS_PORT,
decode_responses=True,
socket_timeout=5,
)
app.state.redis_client = redis_client
except Exception as e:
logging.error(
InfrastructureErrorLog(detail=f"failed to connect to redis: {e}")
)
logger.debug("Redis connected")
if hasattr(app.state, "counter"):
app.state.counter += 1
else:
Expand Down Expand Up @@ -245,6 +246,7 @@ def pong():
def favico():
return RedirectResponse("https://openaq.org/assets/graphics/meta/favicon.png")


# v3
app.include_router(locations.router)
app.include_router(parameters.router)
Expand Down
1 change: 1 addition & 0 deletions openaq_api/openaq_api/routers/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ async def check_email(request: Request):

@router.get("/verify/{verification_code}")
async def verify(request: Request, verification_code: str, db: DB = Depends()):
print(f"\n\n\nAPPP: { getattr(request.app.state, 'redis_client')}\n\n")
query = """
SELECT
users.users_id, users.is_active, users.expires_on, entities.full_name, users.email_address
Expand Down

0 comments on commit f5ec281

Please sign in to comment.