fix(dev): 等待 PostgreSQL 容器就绪
数据库初始化脚本在容器停止时主动启动,并在执行 psql 前等待 PostgreSQL 就绪;若容器提前退出或超过等待时间则返回明确错误。 验证:bash -n、ShellCheck、git diff --check 与 pnpm db:create 通过。
This commit is contained in:
@@ -23,6 +23,35 @@ fi
|
||||
PGUSER="${AI_GATEWAY_PG_USER:-easyai}"
|
||||
DB_NAME="${AI_GATEWAY_DATABASE_NAME:-easyai_ai_gateway}"
|
||||
|
||||
wait_for_postgres() {
|
||||
local status
|
||||
|
||||
status="$(docker inspect "$CONTAINER" --format '{{.State.Status}}')"
|
||||
if [[ "$status" != "running" ]]; then
|
||||
echo "[ai-gateway] starting PostgreSQL container: ${CONTAINER}"
|
||||
docker start "$CONTAINER" >/dev/null
|
||||
fi
|
||||
|
||||
for _ in {1..30}; do
|
||||
if docker exec "$CONTAINER" \
|
||||
pg_isready -U "$PGUSER" -d postgres >/dev/null 2>&1; then
|
||||
return 0
|
||||
fi
|
||||
|
||||
status="$(docker inspect "$CONTAINER" --format '{{.State.Status}}')"
|
||||
if [[ "$status" != "running" ]]; then
|
||||
echo "[ai-gateway] PostgreSQL container stopped before becoming ready: ${CONTAINER} (${status})" >&2
|
||||
return 1
|
||||
fi
|
||||
sleep 1
|
||||
done
|
||||
|
||||
echo "[ai-gateway] timed out waiting for PostgreSQL container: ${CONTAINER}" >&2
|
||||
return 1
|
||||
}
|
||||
|
||||
wait_for_postgres
|
||||
|
||||
version_num="$(
|
||||
docker exec "$CONTAINER" \
|
||||
psql -U "$PGUSER" -d postgres -tAc "SHOW server_version_num" \
|
||||
|
||||
Reference in New Issue
Block a user