feat: implement AI gateway phase one runtime
This commit is contained in:
@@ -1,6 +1,50 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||
|
||||
stop_stale_api_processes() {
|
||||
local api_port="${HTTP_ADDR:-:8088}"
|
||||
api_port="${api_port##*:}"
|
||||
local api_cwd="${PROJECT_ROOT}/apps/api"
|
||||
|
||||
if command -v pgrep >/dev/null 2>&1; then
|
||||
while read -r pid; do
|
||||
[[ -z "$pid" || "$pid" == "$$" ]] && continue
|
||||
local cwd
|
||||
cwd="$(lsof -a -p "$pid" -d cwd -Fn 2>/dev/null | awk '/^n/ {print substr($0, 2); exit}')"
|
||||
if [[ "$cwd" == "$api_cwd" ]]; then
|
||||
echo "[ai-gateway] stopping stale go-watch process: ${pid}"
|
||||
kill "$pid" 2>/dev/null || true
|
||||
fi
|
||||
done < <(pgrep -f "go-watch\\.mjs -- go run ./cmd/gateway" || true)
|
||||
fi
|
||||
|
||||
if ! command -v lsof >/dev/null 2>&1; then
|
||||
return
|
||||
fi
|
||||
|
||||
local pids
|
||||
pids="$(lsof -tiTCP:"$api_port" -sTCP:LISTEN 2>/dev/null || true)"
|
||||
[[ -z "$pids" ]] && return
|
||||
|
||||
while read -r pid; do
|
||||
[[ -z "$pid" ]] && continue
|
||||
local cwd command
|
||||
cwd="$(lsof -a -p "$pid" -d cwd -Fn 2>/dev/null | awk '/^n/ {print substr($0, 2); exit}')"
|
||||
command="$(ps -p "$pid" -o command= 2>/dev/null || true)"
|
||||
if [[ "$cwd" == "$api_cwd" || "$command" == *"easyai-ai-gateway"* ]]; then
|
||||
echo "[ai-gateway] freeing API port ${api_port}, stopping stale process: ${pid}"
|
||||
kill "$pid" 2>/dev/null || true
|
||||
else
|
||||
echo "[ai-gateway] port ${api_port} is used by an unrelated process: ${pid} ${command}" >&2
|
||||
exit 1
|
||||
fi
|
||||
done <<< "$pids"
|
||||
|
||||
sleep 0.5
|
||||
}
|
||||
|
||||
if [[ -z "${AI_GATEWAY_PG_CONTAINER:-}" ]]; then
|
||||
if docker inspect easyai-pgvector >/dev/null 2>&1; then
|
||||
export AI_GATEWAY_PG_CONTAINER="easyai-pgvector"
|
||||
@@ -26,4 +70,5 @@ echo "[ai-gateway] using database: ${AI_GATEWAY_DATABASE_URL}"
|
||||
|
||||
scripts/create-database.sh
|
||||
pnpm nx run api:migrate
|
||||
stop_stale_api_processes
|
||||
exec pnpm nx run-many -t dev -p api web --parallel=2
|
||||
|
||||
Reference in New Issue
Block a user