Spaces:
Running
Running
Monteeq CI commited on
Commit Β·
cbf7ff4
1
Parent(s): 5835322
π Deploy from GitHub @ d1845b57f56a8989dfcc774c599949d84f663a9d
Browse filesTriggered by: Smasduq
Message: refactor: optimize Redis connection settings for resource efficiency
Adjusted Redis connection parameters across multiple components to align with the Redis Cloud free tier limitations. Updated max connections in RedisManager, worker configuration, and connection pool settings to ensure efficient resource usage and prevent connection issues. This change enhances stability and performance of the application.
- app/core/redis.py +6 -3
- app/worker.py +7 -6
- main.py +6 -1
- start.sh +1 -1
app/core/redis.py
CHANGED
|
@@ -28,23 +28,26 @@ class RedisManager:
|
|
| 28 |
"""Initializes the Redis client and validates the connection."""
|
| 29 |
try:
|
| 30 |
# Use connection pooling for efficiency and stability
|
|
|
|
|
|
|
| 31 |
pool = redis.ConnectionPool.from_url(
|
| 32 |
REDIS_URL,
|
| 33 |
decode_responses=True,
|
| 34 |
socket_timeout=5,
|
| 35 |
socket_connect_timeout=5,
|
| 36 |
retry_on_timeout=True,
|
| 37 |
-
max_connections=
|
|
|
|
| 38 |
)
|
| 39 |
self._client = redis.StrictRedis(connection_pool=pool)
|
| 40 |
|
| 41 |
# Simple health check to ensure Redis is responsive
|
| 42 |
self._client.ping()
|
| 43 |
self.last_error = None
|
| 44 |
-
logger.info(f"Successfully connected to Redis
|
| 45 |
except Exception as e:
|
| 46 |
self.last_error = f"{type(e).__name__}: {str(e)}"
|
| 47 |
-
logger.error(f"FATAL: Could not connect to Redis
|
| 48 |
# We don't raise here to allow the app to start,
|
| 49 |
# but specific features requiring Redis will fail gracefully later.
|
| 50 |
self._client = None
|
|
|
|
| 28 |
"""Initializes the Redis client and validates the connection."""
|
| 29 |
try:
|
| 30 |
# Use connection pooling for efficiency and stability
|
| 31 |
+
# Keep this low β Redis Cloud free tier allows ~30 clients total
|
| 32 |
+
# across backend, Celery, beat, cache, and the video service.
|
| 33 |
pool = redis.ConnectionPool.from_url(
|
| 34 |
REDIS_URL,
|
| 35 |
decode_responses=True,
|
| 36 |
socket_timeout=5,
|
| 37 |
socket_connect_timeout=5,
|
| 38 |
retry_on_timeout=True,
|
| 39 |
+
max_connections=5,
|
| 40 |
+
health_check_interval=30,
|
| 41 |
)
|
| 42 |
self._client = redis.StrictRedis(connection_pool=pool)
|
| 43 |
|
| 44 |
# Simple health check to ensure Redis is responsive
|
| 45 |
self._client.ping()
|
| 46 |
self.last_error = None
|
| 47 |
+
logger.info(f"Successfully connected to Redis at {REDIS_URL[:32]}...")
|
| 48 |
except Exception as e:
|
| 49 |
self.last_error = f"{type(e).__name__}: {str(e)}"
|
| 50 |
+
logger.error(f"FATAL: Could not connect to Redis. Error: {e}")
|
| 51 |
# We don't raise here to allow the app to start,
|
| 52 |
# but specific features requiring Redis will fail gracefully later.
|
| 53 |
self._client = None
|
app/worker.py
CHANGED
|
@@ -28,14 +28,15 @@ celery_app.conf.update(
|
|
| 28 |
enable_utc=True,
|
| 29 |
task_track_started=True,
|
| 30 |
task_time_limit=3600,
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
| 36 |
broker_transport_options={
|
| 37 |
'visibility_timeout': 3600,
|
| 38 |
-
'max_connections':
|
| 39 |
},
|
| 40 |
beat_schedule={
|
| 41 |
# Morning push notifications β daily 08:00 UTC
|
|
|
|
| 28 |
enable_utc=True,
|
| 29 |
task_track_started=True,
|
| 30 |
task_time_limit=3600,
|
| 31 |
+
# Stay well under Redis Cloud free-tier ~30 concurrent clients
|
| 32 |
+
broker_pool_limit=2,
|
| 33 |
+
redis_max_connections=5,
|
| 34 |
+
# solo keeps Redis client count low (also required on Windows)
|
| 35 |
+
worker_pool="solo",
|
| 36 |
+
worker_concurrency=1,
|
| 37 |
broker_transport_options={
|
| 38 |
'visibility_timeout': 3600,
|
| 39 |
+
'max_connections': 2,
|
| 40 |
},
|
| 41 |
beat_schedule={
|
| 42 |
# Morning push notifications β daily 08:00 UTC
|
main.py
CHANGED
|
@@ -161,7 +161,12 @@ async def websocket_chat(websocket: WebSocket, token: str = Query(...)):
|
|
| 161 |
@app.on_event("startup")
|
| 162 |
async def startup():
|
| 163 |
from app.core.config import REDIS_URL
|
| 164 |
-
redis = aioredis.from_url(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
FastAPICache.init(RedisBackend(redis), prefix="fastapi-cache")
|
| 166 |
|
| 167 |
@app.get("/", tags=["System"])
|
|
|
|
| 161 |
@app.on_event("startup")
|
| 162 |
async def startup():
|
| 163 |
from app.core.config import REDIS_URL
|
| 164 |
+
redis = aioredis.from_url(
|
| 165 |
+
REDIS_URL,
|
| 166 |
+
encoding="utf8",
|
| 167 |
+
decode_responses=True,
|
| 168 |
+
max_connections=5,
|
| 169 |
+
)
|
| 170 |
FastAPICache.init(RedisBackend(redis), prefix="fastapi-cache")
|
| 171 |
|
| 172 |
@app.get("/", tags=["System"])
|
start.sh
CHANGED
|
@@ -4,7 +4,7 @@ set -e
|
|
| 4 |
echo "Starting Celery worker in background..."
|
| 5 |
celery -A app.worker.celery_app worker \
|
| 6 |
--loglevel=info \
|
| 7 |
-
--concurrency=
|
| 8 |
--pool=solo &
|
| 9 |
|
| 10 |
echo "Starting Celery beat scheduler in background..."
|
|
|
|
| 4 |
echo "Starting Celery worker in background..."
|
| 5 |
celery -A app.worker.celery_app worker \
|
| 6 |
--loglevel=info \
|
| 7 |
+
--concurrency=1 \
|
| 8 |
--pool=solo &
|
| 9 |
|
| 10 |
echo "Starting Celery beat scheduler in background..."
|