fix: improve database connection handling in entrypoint
Browse files- Add timeout-based PostgreSQL connection check
- Implement max retry attempts (30 attempts, 5s between each)
- Add detailed connection attempt logging
- Maintain compatibility with existing Redis checks
- Exit gracefully if database connection fails after max attempts
References:
docker/entrypoint.sh (lines 1-33)
docker/docker-compose.yaml (lines 331-346)
- docker/entrypoint.sh +19 -14
docker/entrypoint.sh
CHANGED
@@ -3,27 +3,32 @@ set -e
|
|
3 |
|
4 |
echo "Starting Dify services..."
|
5 |
|
6 |
-
# Enhanced database connection check
|
7 |
check_postgres() {
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
-d "${DB_DATABASE}"
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
}
|
15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
check_redis() {
|
17 |
redis-cli -h "${REDIS_HOST}" -p "${REDIS_PORT}" -a "${REDIS_PASSWORD}" ping > /dev/null 2>&1
|
18 |
}
|
19 |
|
20 |
-
echo "Waiting for PostgreSQL to be ready..."
|
21 |
-
until check_postgres; do
|
22 |
-
echo "PostgreSQL is unavailable (host: ${DB_HOST}, port: ${DB_PORT}) - retrying..."
|
23 |
-
sleep 5
|
24 |
-
done
|
25 |
-
echo "PostgreSQL is ready!"
|
26 |
-
|
27 |
echo "Waiting for Redis to be ready..."
|
28 |
until check_redis; do
|
29 |
echo "Redis is unavailable (host: ${REDIS_HOST}, port: ${REDIS_PORT}) - retrying..."
|
|
|
3 |
|
4 |
echo "Starting Dify services..."
|
5 |
|
6 |
+
# Enhanced database connection check with timeout
|
7 |
check_postgres() {
|
8 |
+
local max_attempts=30
|
9 |
+
local attempt=1
|
10 |
+
|
11 |
+
while [ $attempt -le $max_attempts ]; do
|
12 |
+
echo "Checking PostgreSQL connection to ${DB_HOST}:${DB_PORT} (attempt $attempt/$max_attempts)..."
|
13 |
+
if pg_isready -h "${DB_HOST}" -p "${DB_PORT}" -U "${DB_USERNAME}" -d "${DB_DATABASE}"; then
|
14 |
+
return 0
|
15 |
+
fi
|
16 |
+
attempt=$((attempt + 1))
|
17 |
+
sleep 5
|
18 |
+
done
|
19 |
+
return 1
|
20 |
}
|
21 |
|
22 |
+
# Try to connect with timeout
|
23 |
+
if ! check_postgres; then
|
24 |
+
echo "Failed to connect to PostgreSQL after multiple attempts. Exiting."
|
25 |
+
exit 1
|
26 |
+
fi
|
27 |
+
|
28 |
check_redis() {
|
29 |
redis-cli -h "${REDIS_HOST}" -p "${REDIS_PORT}" -a "${REDIS_PASSWORD}" ping > /dev/null 2>&1
|
30 |
}
|
31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
echo "Waiting for Redis to be ready..."
|
33 |
until check_redis; do
|
34 |
echo "Redis is unavailable (host: ${REDIS_HOST}, port: ${REDIS_PORT}) - retrying..."
|