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 +22 -14
docker/entrypoint.sh
CHANGED
|
@@ -6,11 +6,9 @@ echo "Starting Dify services..."
|
|
| 6 |
# Database connection check based on type
|
| 7 |
if [ "${DATABASE_TYPE}" = "sqlite" ]; then
|
| 8 |
echo "Using SQLite database at ${SQLITE_PATH}"
|
| 9 |
-
# Ensure directory exists
|
| 10 |
mkdir -p $(dirname "${SQLITE_PATH}")
|
| 11 |
touch "${SQLITE_PATH}"
|
| 12 |
else
|
| 13 |
-
# Enhanced PostgreSQL connection check with timeout
|
| 14 |
check_postgres() {
|
| 15 |
local max_attempts=30
|
| 16 |
local attempt=1
|
|
@@ -26,7 +24,6 @@ else
|
|
| 26 |
return 1
|
| 27 |
}
|
| 28 |
|
| 29 |
-
# Try to connect with timeout
|
| 30 |
if ! check_postgres; then
|
| 31 |
echo "Failed to connect to PostgreSQL, falling back to SQLite..."
|
| 32 |
export DATABASE_TYPE="sqlite"
|
|
@@ -35,23 +32,27 @@ else
|
|
| 35 |
fi
|
| 36 |
fi
|
| 37 |
|
| 38 |
-
# Redis connection check function
|
| 39 |
check_redis() {
|
| 40 |
local max_attempts=30
|
| 41 |
local attempt=1
|
| 42 |
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
while [ $attempt -le $max_attempts ]; do
|
| 45 |
-
if
|
| 46 |
-
echo "
|
| 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))
|
|
@@ -60,6 +61,13 @@ check_redis() {
|
|
| 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
|
|
|
|
| 6 |
# Database connection check based on type
|
| 7 |
if [ "${DATABASE_TYPE}" = "sqlite" ]; then
|
| 8 |
echo "Using SQLite database at ${SQLITE_PATH}"
|
|
|
|
| 9 |
mkdir -p $(dirname "${SQLITE_PATH}")
|
| 10 |
touch "${SQLITE_PATH}"
|
| 11 |
else
|
|
|
|
| 12 |
check_postgres() {
|
| 13 |
local max_attempts=30
|
| 14 |
local attempt=1
|
|
|
|
| 24 |
return 1
|
| 25 |
}
|
| 26 |
|
|
|
|
| 27 |
if ! check_postgres; then
|
| 28 |
echo "Failed to connect to PostgreSQL, falling back to SQLite..."
|
| 29 |
export DATABASE_TYPE="sqlite"
|
|
|
|
| 32 |
fi
|
| 33 |
fi
|
| 34 |
|
| 35 |
+
# Redis connection check function with HF Spaces compatibility
|
| 36 |
check_redis() {
|
| 37 |
local max_attempts=30
|
| 38 |
local attempt=1
|
| 39 |
|
| 40 |
+
# For HF Spaces, start local Redis by default
|
| 41 |
+
if [ "${REDIS_TYPE}" = "local" ] || [ "${SPACE_ID}" != "" ]; then
|
| 42 |
+
echo "Starting local Redis server..."
|
| 43 |
+
redis-server --daemonize yes --port 6379 --requirepass "${REDIS_PASSWORD:-difyai123456}"
|
| 44 |
+
export REDIS_HOST=localhost
|
| 45 |
+
sleep 2
|
| 46 |
+
if redis-cli -h localhost -p 6379 -a "${REDIS_PASSWORD:-difyai123456}" ping > /dev/null 2>&1; then
|
| 47 |
+
echo "Local Redis server started successfully"
|
| 48 |
+
return 0
|
| 49 |
+
fi
|
| 50 |
+
fi
|
| 51 |
+
|
| 52 |
while [ $attempt -le $max_attempts ]; do
|
| 53 |
+
if redis-cli -h "${REDIS_HOST}" -p "${REDIS_PORT}" -a "${REDIS_PASSWORD}" ping > /dev/null 2>&1; then
|
| 54 |
+
echo "Redis is ready!"
|
|
|
|
|
|
|
| 55 |
return 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
fi
|
| 57 |
echo "Redis is unavailable (attempt $attempt/$max_attempts) - retrying..."
|
| 58 |
attempt=$((attempt + 1))
|
|
|
|
| 61 |
if [ $attempt -eq $max_attempts ]; then
|
| 62 |
echo "Falling back to local Redis..."
|
| 63 |
export REDIS_TYPE="local"
|
| 64 |
+
export REDIS_HOST=localhost
|
| 65 |
+
redis-server --daemonize yes --port 6379 --requirepass "${REDIS_PASSWORD:-difyai123456}"
|
| 66 |
+
sleep 2
|
| 67 |
+
if redis-cli -h localhost -p 6379 -a "${REDIS_PASSWORD:-difyai123456}" ping > /dev/null 2>&1; then
|
| 68 |
+
echo "Local Redis server started successfully"
|
| 69 |
+
return 0
|
| 70 |
+
fi
|
| 71 |
fi
|
| 72 |
done
|
| 73 |
return 1
|