feat: implement database fallback mechanism
Browse files- Add SQLite fallback support
- Enhance PostgreSQL connection checks with timeout
- Improve error handling and logging
- Add directory creation for SQLite database
- docker/entrypoint.sh +29 -19
docker/entrypoint.sh
CHANGED
|
@@ -3,26 +3,36 @@ set -e
|
|
| 3 |
|
| 4 |
echo "Starting Dify services..."
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
attempt
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
# Try to connect with timeout
|
| 23 |
-
if ! check_postgres; then
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
| 26 |
fi
|
| 27 |
|
| 28 |
check_redis() {
|
|
|
|
| 3 |
|
| 4 |
echo "Starting Dify services..."
|
| 5 |
|
| 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
|
| 17 |
+
|
| 18 |
+
while [ $attempt -le $max_attempts ]; do
|
| 19 |
+
echo "Checking PostgreSQL connection to ${DB_HOST}:${DB_PORT} (attempt $attempt/$max_attempts)..."
|
| 20 |
+
if pg_isready -h "${DB_HOST}" -p "${DB_PORT}" -U "${DB_USERNAME}" -d "${DB_DATABASE}"; then
|
| 21 |
+
return 0
|
| 22 |
+
fi
|
| 23 |
+
attempt=$((attempt + 1))
|
| 24 |
+
sleep 5
|
| 25 |
+
done
|
| 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"
|
| 33 |
+
mkdir -p $(dirname "${SQLITE_PATH}")
|
| 34 |
+
touch "${SQLITE_PATH}"
|
| 35 |
+
fi
|
| 36 |
fi
|
| 37 |
|
| 38 |
check_redis() {
|