Files
AI_A4000/video_worker/center_dispatch/scripts/start.sh
2026-04-07 18:01:32 +08:00

81 lines
2.5 KiB
Bash

#!/usr/bin/env bash
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
PROJECT_DIR="${ROOT_DIR}/center_dispatch"
cd "$PROJECT_DIR"
if [ ! -f .env ]; then
cp .env.example .env
echo "[INFO] .env created from .env.example, please set GIT_REPO_URL/OSS config."
fi
if ! command -v docker >/dev/null 2>&1; then
echo "[ERROR] docker not found"
exit 1
fi
if ! docker compose version >/dev/null 2>&1; then
echo "[ERROR] docker compose not available"
exit 1
fi
CONTAINER_NAME="video-worker-center-dispatch"
EXISTING_ID="$(docker ps -aq -f "name=^${CONTAINER_NAME}$" || true)"
if [ -n "$EXISTING_ID" ]; then
echo "[WARN] found existing container ${CONTAINER_NAME} (${EXISTING_ID}), removing it"
docker rm -f "$EXISTING_ID" >/dev/null 2>&1 || true
fi
PORT=$(grep '^EDGE_DISPATCH_PORT=' .env | tail -n1 | cut -d'=' -f2- || true)
PORT="${PORT:-8060}"
# AUTO_PULL_LATEST=true requires GIT_REPO_URL. If empty, fallback to local source build.
AUTO_PULL="$(grep '^AUTO_PULL_LATEST=' .env | tail -n1 | cut -d'=' -f2- || true)"
AUTO_PULL="${AUTO_PULL:-true}"
GIT_REPO_URL_VAL="$(grep '^GIT_REPO_URL=' .env | tail -n1 | cut -d'=' -f2- || true)"
if [ "$AUTO_PULL" = "true" ] && [ -z "$GIT_REPO_URL_VAL" ]; then
if [ -d "${ROOT_DIR}/.git" ]; then
REPO_FROM_GIT="$(git -C "$ROOT_DIR" config --get remote.origin.url || true)"
else
REPO_FROM_GIT=""
fi
if [ -n "$REPO_FROM_GIT" ]; then
echo "[WARN] GIT_REPO_URL empty; use git remote origin: $REPO_FROM_GIT"
export GIT_REPO_URL="$REPO_FROM_GIT"
else
echo "[WARN] GIT_REPO_URL empty; fallback AUTO_PULL_LATEST=false (build from local source)"
export AUTO_PULL_LATEST=false
fi
fi
docker compose up -d --build
sleep 2
if ! docker ps --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}$"; then
echo "[ERROR] container ${CONTAINER_NAME} is not running"
echo "[INFO] container status:"
docker ps -a --filter "name=^${CONTAINER_NAME}$" --format 'table {{.Names}}\t{{.Status}}\t{{.Ports}}'
echo "[INFO] recent logs:"
docker logs --tail 200 "${CONTAINER_NAME}" || true
exit 1
fi
if command -v curl >/dev/null 2>&1; then
HEALTH_OK=false
for _ in $(seq 1 30); do
if curl -fsS "http://127.0.0.1:${PORT}/health" >/dev/null 2>&1; then
HEALTH_OK=true
break
fi
sleep 1
done
if [ "$HEALTH_OK" = false ]; then
echo "[WARN] container is running but health endpoint is still not ready after 30s: http://127.0.0.1:${PORT}/health"
fi
fi
echo "[OK] center dispatch started"
echo "[INFO] health: curl http://127.0.0.1:${PORT}/health"