CodeReview/backend/docker-entrypoint.sh

62 lines
1.5 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
set -e
echo "🚀 DeepAudit 后端启动中..."
# 等待 PostgreSQL 就绪
echo "⏳ 等待数据库连接..."
max_retries=30
retry_count=0
while [ $retry_count -lt $max_retries ]; do
if .venv/bin/python -c "
import asyncio
from sqlalchemy.ext.asyncio import create_async_engine
import os
async def check_db():
engine = create_async_engine(os.environ.get('DATABASE_URL', ''))
try:
async with engine.connect() as conn:
await conn.execute(text('SELECT 1'))
return True
except Exception:
return False
finally:
await engine.dispose()
from sqlalchemy import text
exit(0 if asyncio.run(check_db()) else 1)
" 2>/dev/null; then
echo "✅ 数据库连接成功"
break
fi
retry_count=$((retry_count + 1))
echo " 重试 $retry_count/$max_retries..."
sleep 2
done
if [ $retry_count -eq $max_retries ]; then
echo "❌ 无法连接到数据库,请检查 DATABASE_URL 配置"
exit 1
fi
# 运行数据库迁移
echo "📦 执行数据库迁移..."
.venv/bin/alembic upgrade head
echo "✅ 数据库迁移完成"
# 启动 gunicorn (多工作进程模式)
# 默认开启 4 个 worker显著提高并发处理能力
echo "🌐 启动 API 服务 (Gunicorn + 4 Workers)..."
exec .venv/bin/gunicorn app.main:app \
--workers 4 \
--worker-class uvicorn.workers.UvicornWorker \
--bind 0.0.0.0:8000 \
--timeout 1800 \
--access-logfile - \
--error-logfile -