fix: improve Redis and PostgreSQL connection handling
Browse files- Add proper error messages for connection failures
- Implement local Redis fallback mechanism
- Fix variable scoping in Redis check function
- Add detailed logging for connection attempts
- Ensure proper exit codes on failures
- docker/entrypoint.sh +31 -29
docker/entrypoint.sh
CHANGED
@@ -35,40 +35,42 @@ else
|
|
35 |
fi
|
36 |
fi
|
37 |
|
|
|
38 |
check_redis() {
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
}
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
sleep 2
|
47 |
-
return 0
|
48 |
-
else
|
49 |
-
redis-cli -h "${REDIS_HOST}" -p "${REDIS_PORT}" -a "${REDIS_PASSWORD}" ping > /dev/null 2>&1
|
50 |
-
fi
|
51 |
}
|
52 |
|
53 |
-
echo "Waiting for Redis to be ready..."
|
54 |
-
local max_attempts=30
|
55 |
-
local attempt=1
|
56 |
-
|
57 |
-
while [ $attempt -le $max_attempts ]; do
|
58 |
-
if check_redis; then
|
59 |
-
echo "Redis is ready!"
|
60 |
-
break
|
61 |
-
fi
|
62 |
-
echo "Redis is unavailable (attempt $attempt/$max_attempts) - retrying..."
|
63 |
-
attempt=$((attempt + 1))
|
64 |
-
sleep 5
|
65 |
-
|
66 |
-
if [ $attempt -eq $max_attempts ]; then
|
67 |
-
echo "Falling back to local Redis..."
|
68 |
-
export REDIS_TYPE="local"
|
69 |
-
fi
|
70 |
-
done
|
71 |
-
|
72 |
# Initialize database if needed
|
73 |
cd /app/api
|
74 |
if [ ! -f ".db_initialized" ]; then
|
|
|
35 |
fi
|
36 |
fi
|
37 |
|
38 |
+
# Redis connection check function
|
39 |
check_redis() {
|
40 |
+
local max_attempts=30
|
41 |
+
local attempt=1
|
42 |
+
|
43 |
+
echo "Waiting for Redis to be ready..."
|
44 |
+
while [ $attempt -le $max_attempts ]; do
|
45 |
+
if [ "${REDIS_TYPE}" = "local" ]; then
|
46 |
+
echo "Starting local Redis server..."
|
47 |
+
redis-server --daemonize yes --requirepass "${REDIS_PASSWORD}"
|
48 |
+
sleep 2
|
49 |
+
return 0
|
50 |
+
else
|
51 |
+
if redis-cli -h "${REDIS_HOST}" -p "${REDIS_PORT}" -a "${REDIS_PASSWORD}" ping > /dev/null 2>&1; then
|
52 |
+
echo "Redis is ready!"
|
53 |
+
return 0
|
54 |
+
fi
|
55 |
+
fi
|
56 |
+
echo "Redis is unavailable (attempt $attempt/$max_attempts) - retrying..."
|
57 |
+
attempt=$((attempt + 1))
|
58 |
+
sleep 5
|
59 |
+
|
60 |
+
if [ $attempt -eq $max_attempts ]; then
|
61 |
+
echo "Falling back to local Redis..."
|
62 |
+
export REDIS_TYPE="local"
|
63 |
+
fi
|
64 |
+
done
|
65 |
+
return 1
|
66 |
}
|
67 |
|
68 |
+
# Try to connect to Redis
|
69 |
+
check_redis || {
|
70 |
+
echo "Failed to connect to Redis after all attempts"
|
71 |
+
exit 1
|
|
|
|
|
|
|
|
|
|
|
72 |
}
|
73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
# Initialize database if needed
|
75 |
cd /app/api
|
76 |
if [ ! -f ".db_initialized" ]; then
|