File size: 3,315 Bytes
a4bef0b 12e3afc 46a883c 53d8b27 46a883c 8a398ad 7d9cc10 8f2f82d 7d9cc10 8f2f82d 7d9cc10 6612613 53d8b27 a4bef0b 53d8b27 8f2f82d 53d8b27 8f2f82d 53d8b27 8f2f82d e3a6f0b a4bef0b 12e3afc a4bef0b 12e3afc 070b654 12e3afc 070b654 a4bef0b 12e3afc e3a6f0b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
#!/bin/bash
set -e
echo "Starting Dify services..."
# Database connection check based on type
if [ "${DATABASE_TYPE}" = "sqlite" ]; then
echo "Using SQLite database at ${SQLITE_PATH}"
# Ensure directory exists
mkdir -p $(dirname "${SQLITE_PATH}")
touch "${SQLITE_PATH}"
else
# Enhanced PostgreSQL connection check with timeout
check_postgres() {
local max_attempts=30
local attempt=1
while [ $attempt -le $max_attempts ]; do
echo "Checking PostgreSQL connection to ${DB_HOST}:${DB_PORT} (attempt $attempt/$max_attempts)..."
if pg_isready -h "${DB_HOST}" -p "${DB_PORT}" -U "${DB_USERNAME}" -d "${DB_DATABASE}"; then
return 0
fi
attempt=$((attempt + 1))
sleep 5
done
return 1
}
# Try to connect with timeout
if ! check_postgres; then
echo "Failed to connect to PostgreSQL, falling back to SQLite..."
export DATABASE_TYPE="sqlite"
mkdir -p $(dirname "${SQLITE_PATH}")
touch "${SQLITE_PATH}"
fi
fi
# Redis connection check function
check_redis() {
local max_attempts=30
local attempt=1
echo "Waiting for Redis to be ready..."
while [ $attempt -le $max_attempts ]; do
if [ "${REDIS_TYPE}" = "local" ]; then
echo "Starting local Redis server..."
redis-server --daemonize yes --requirepass "${REDIS_PASSWORD}"
sleep 2
return 0
else
if redis-cli -h "${REDIS_HOST}" -p "${REDIS_PORT}" -a "${REDIS_PASSWORD}" ping > /dev/null 2>&1; then
echo "Redis is ready!"
return 0
fi
fi
echo "Redis is unavailable (attempt $attempt/$max_attempts) - retrying..."
attempt=$((attempt + 1))
sleep 5
if [ $attempt -eq $max_attempts ]; then
echo "Falling back to local Redis..."
export REDIS_TYPE="local"
fi
done
return 1
}
# Try to connect to Redis
check_redis || {
echo "Failed to connect to Redis after all attempts"
exit 1
}
# Initialize database if needed
cd /app/api
if [ ! -f ".db_initialized" ]; then
echo "Running database migrations..."
flask db upgrade
if [ $? -eq 0 ]; then
touch .db_initialized
echo "Database initialization completed successfully"
else
echo "Database initialization failed"
exit 1
fi
fi
# Start services
echo "Starting API server on port 7860..."
gunicorn --bind 0.0.0.0:7860 \
--workers 1 \
--worker-class gevent \
--timeout 200 \
--preload \
app:app &
# Start Next.js web server
cd /app/web
echo "Starting Next.js server on port 3000..."
# Ensure standalone directory exists
if [ ! -d ".next/standalone" ]; then
echo "Error: Next.js standalone build not found"
exit 1
fi
# Copy static files if they exist
if [ -d ".next/static" ]; then
mkdir -p .next/standalone/.next
cp -r .next/static .next/standalone/.next/
fi
# Copy public files if they exist
if [ -d "public" ]; then
cp -r public .next/standalone/
fi
cd .next/standalone
echo "Starting Next.js standalone server..."
NODE_ENV=production \
NEXT_TELEMETRY_DISABLED=1 \
node server.js &
# Wait for both processes
wait |