#!/bin/bash
# Parallele KI-Zusammenfassungen in Batches bis alles fertig

cd "$(dirname "$0")/.."
source .venv/bin/activate

LOG_FILE="data/ki_parallel_batches.log"
WORKERS=15
BATCH_SIZE=100
PAUSE_SECONDS=5

echo "=== KI-Parallel-Runner gestartet $(date) ===" | tee -a "$LOG_FILE"
echo "Workers: $WORKERS, Batch: $BATCH_SIZE" | tee -a "$LOG_FILE"

while true; do
    echo "" | tee -a "$LOG_FILE"
    echo "--- Starte Batch $(date +%H:%M:%S) ---" | tee -a "$LOG_FILE"
    
    python scripts/ki_parallel.py --workers $WORKERS --batch-size $BATCH_SIZE 2>&1 | tee -a "$LOG_FILE"
    EXIT_CODE=${PIPESTATUS[0]}
    
    if [ $EXIT_CODE -eq 0 ]; then
        echo "" | tee -a "$LOG_FILE"
        echo "=== ALLE FERTIG $(date) ===" | tee -a "$LOG_FILE"
        break
    fi
    
    echo "Pause ${PAUSE_SECONDS}s..." | tee -a "$LOG_FILE"
    sleep $PAUSE_SECONDS
done

echo "Runner beendet." | tee -a "$LOG_FILE"
