dify / docker /entrypoint.sh
Severian's picture
fix: improve database initialization and connection handling
8f2f82d
raw
history blame
1.88 kB
#!/bin/bash
set -e
echo "Starting Dify services..."
# Enhanced database connection check
check_postgres() {
pg_isready -h "${DB_HOST}" -p "${DB_PORT}" -U "${DB_USERNAME}" > /dev/null 2>&1
}
check_redis() {
redis-cli -h "${REDIS_HOST}" -p "${REDIS_PORT}" -a "${REDIS_PASSWORD}" ping > /dev/null 2>&1
}
echo "Waiting for PostgreSQL to be ready..."
until check_postgres; do
echo "PostgreSQL is unavailable (host: ${DB_HOST}, port: ${DB_PORT}) - retrying..."
sleep 5
done
echo "PostgreSQL is ready!"
echo "Waiting for Redis to be ready..."
until check_redis; do
echo "Redis is unavailable (host: ${REDIS_HOST}, port: ${REDIS_PORT}) - retrying..."
sleep 5
done
echo "Redis is ready!"
# 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