Peter Mutwiri commited on
Commit
50fc282
Β·
1 Parent(s): 6b15a3d

aded safe redis decode

Browse files
Files changed (1) hide show
  1. app/main.py +7 -2
app/main.py CHANGED
@@ -35,6 +35,11 @@ logging.basicConfig(
35
  )
36
  logger = logging.getLogger(__name__)
37
 
 
 
 
 
 
38
  # ─── Lifespan Management ───────────────────────────────────────────────────────
39
  @asynccontextmanager
40
  async def lifespan(app: FastAPI):
@@ -91,7 +96,7 @@ async def lifespan(app: FastAPI):
91
  try:
92
  active_orgs = redis.keys("entity:*")
93
  for key in active_orgs:
94
- key_parts = key.decode().split(":")
95
  if len(key_parts) >= 3:
96
  org_id, source_id = key_parts[1], key_parts[2]
97
  stream_key = f"stream:analytics:{org_id}:{source_id}"
@@ -274,7 +279,7 @@ async def continuous_kpi_refresh():
274
 
275
  active_keys = redis.keys("entity:*")
276
  for key in active_keys:
277
- key_parts = key.decode().split(":")
278
  if len(key_parts) >= 3:
279
  org_id, source_id = key_parts[1], key_parts[2]
280
 
 
35
  )
36
  logger = logging.getLogger(__name__)
37
 
38
+ def safe_redis_decode(value):
39
+ """Safely decode Redis values that might be bytes or str"""
40
+ if isinstance(value, bytes):
41
+ return value.decode('utf-8')
42
+ return value
43
  # ─── Lifespan Management ───────────────────────────────────────────────────────
44
  @asynccontextmanager
45
  async def lifespan(app: FastAPI):
 
96
  try:
97
  active_orgs = redis.keys("entity:*")
98
  for key in active_orgs:
99
+ key_parts = safe_redis_decode(key).split(":")
100
  if len(key_parts) >= 3:
101
  org_id, source_id = key_parts[1], key_parts[2]
102
  stream_key = f"stream:analytics:{org_id}:{source_id}"
 
279
 
280
  active_keys = redis.keys("entity:*")
281
  for key in active_keys:
282
+ key_parts = safe_redis_decode(key).split(":")
283
  if len(key_parts) >= 3:
284
  org_id, source_id = key_parts[1], key_parts[2]
285