Severian commited on
Commit
46a883c
·
1 Parent(s): 08efa7b

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

Files changed (1) hide show
  1. docker/entrypoint.sh +29 -19
docker/entrypoint.sh CHANGED
@@ -3,26 +3,36 @@ set -e
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() {
 
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() {