lamhieu commited on
Commit
c54a701
·
1 Parent(s): 9001620

fix: disconnect redis to force reconnect in next time

Browse files
Files changed (1) hide show
  1. lightweight_embeddings/analytics.py +12 -1
lightweight_embeddings/analytics.py CHANGED
@@ -1,10 +1,15 @@
 
1
  import asyncio
2
  import redis.asyncio as redis
 
3
  from datetime import datetime
4
  from collections import defaultdict
5
  from typing import Dict
6
 
7
 
 
 
 
8
  class Analytics:
9
  def __init__(self, redis_url: str, sync_interval: int = 60):
10
  """
@@ -105,6 +110,7 @@ class Analytics:
105
  await pipeline.execute()
106
  self.local_buffer["access"].clear() # Clear access buffer after sync
107
  self.local_buffer["tokens"].clear() # Clear tokens buffer after sync
 
108
 
109
  async def _start_sync_task(self):
110
  """
@@ -112,4 +118,9 @@ class Analytics:
112
  """
113
  while True:
114
  await asyncio.sleep(self.sync_interval)
115
- await self._sync_to_redis()
 
 
 
 
 
 
1
+ import logging
2
  import asyncio
3
  import redis.asyncio as redis
4
+ import redis.exceptions
5
  from datetime import datetime
6
  from collections import defaultdict
7
  from typing import Dict
8
 
9
 
10
+ logger = logging.getLogger(__name__)
11
+
12
+
13
  class Analytics:
14
  def __init__(self, redis_url: str, sync_interval: int = 60):
15
  """
 
110
  await pipeline.execute()
111
  self.local_buffer["access"].clear() # Clear access buffer after sync
112
  self.local_buffer["tokens"].clear() # Clear tokens buffer after sync
113
+ logger.info("Synced analytics data to Redis.")
114
 
115
  async def _start_sync_task(self):
116
  """
 
118
  """
119
  while True:
120
  await asyncio.sleep(self.sync_interval)
121
+ try:
122
+ await self._sync_to_redis()
123
+ except redis.exceptions.ConnectionError as e:
124
+ logger.error("Redis connection error: %s", e)
125
+ self.pool.disconnect() # force reconnect on next request
126
+ await asyncio.sleep(5)