#!/bin/bash set -e cd "$(dirname "$0")/.." HEALTH_URL="" # leer → nutzt docker inspect health status MAX_WAIT=180 # .env aus dem Projektroot laden (nur falls Variable nicht schon im Shell-Env gesetzt) [ -f .env ] && source .env MODEL="${QWEN3_TTS_MODEL:-Qwen/Qwen3-TTS-12Hz-1.7B-CustomVoice}" strip_ansi() { sed 's/\x1b\[[0-9;]*[mK]//g'; } t0=$SECONDS # --- Gegenseitigen Service stoppen --- printf "Clone-Container stoppt..." docker compose --profile clone stop qwen3-tts-clone >/dev/null 2>&1 || true docker compose --profile clone rm -f qwen3-tts-clone >/dev/null 2>&1 || true printf " [%ds] " $(( SECONDS - t0 )) # --- Container starten --- printf "TTS-Container startet..." docker compose up -d qwen3-tts &>/tmp/qwen3-tts-compose.log printf " [%ds]\n" $(( SECONDS - t0 )) printf " Modell %s\n\n" "$MODEL" # --- Log-Events im Hintergrund parsen und ausgeben --- docker logs -f qwen3-tts 2>&1 | strip_ansi | while IFS= read -r line; do case "$line" in *"Stage 0 engine launch started"*) printf " Stage 0 Talker LLM wird geladen\n" ;; *"Initializing a V1 LLM engine"*) printf " Stage 0 Engine initialisiert\n" ;; *"Loading weights"*) printf " Stage 0 Modell-Gewichte werden geladen...\n" ;; *"Loading model weights took"*) took=$(echo "$line" | grep -oP '\d+\.\d+s' | head -1) printf " Stage 0 Gewichte geladen (%s)\n" "$took" ;; *"Stage 1 engine launch started"*) printf " Stage 1 Code2Wav Decoder wird geladen\n" ;; *"Graph capturing finished"*) printf " CUDA Graphen kompiliert\n" ;; *"Application startup complete"*) printf " Server FastAPI bereit\n" ;; *"Uvicorn running on"*) printf " Server HTTP läuft auf Port 8091\n" ;; *"warmup] Server gesund"*) printf " Warmup Aufwärm-Request läuft (kompiliert/lädt Graphen)...\n" ;; *"warmup] fertig"*) took=$(echo "$line" | grep -oP '\d+s' | head -1) printf " Warmup Server ist heiß (%s)\n" "$took" ;; *"ERROR"*|*"ValueError"*) msg=$(echo "$line" | grep -oP '(ERROR|ValueError).*' | head -1) printf " [!] %s\n" "${msg:0:100}" ;; esac done & LOG_PID=$! # --- Health-Check-Schleife --- while (( SECONDS - t0 < MAX_WAIT )); do STATUS=$(docker inspect qwen3-tts --format='{{.State.Health.Status}}' 2>/dev/null || true) if [ "$STATUS" = "healthy" ]; then kill "$LOG_PID" 2>/dev/null wait "$LOG_PID" 2>/dev/null || true printf "\n✓ Bereit: API ws://localhost:8091 UI http://localhost:8092 [%ds]\n" $(( SECONDS - t0 )) exit 0 fi if [ "$STATUS" = "unhealthy" ]; then kill "$LOG_PID" 2>/dev/null printf "\n✗ Container unhealthy. Logs:\n" docker logs --tail=20 qwen3-tts 2>&1 | strip_ansi | grep -E "ERROR|ValueError|Exception" | head -5 exit 1 fi if ! docker ps --filter "name=qwen3-tts" --filter "status=running" -q | grep -q .; then kill "$LOG_PID" 2>/dev/null printf "\n✗ Container abgestürzt. Logs:\n" docker logs --tail=20 qwen3-tts 2>&1 | strip_ansi | grep -E "ERROR|ValueError|Exception" | head -5 exit 1 fi sleep 2 done kill "$LOG_PID" 2>/dev/null printf "\n✗ Timeout nach %ds. Logs: docker logs qwen3-tts\n" "$MAX_WAIT" >&2 exit 1