#!/bin/bash

# Orchestrator loop for BID 7491 (Lifecell — Mcube 2.0)
# Uses --skip-ingest because records are pushed via the Call Sync tab (10.40.180.35/mcube_cl1)

cd /home/aiteam/pcaa-dev/dashboard-backend

LOCKFILE="/tmp/orchestrator_loop_7491.lock"

if [ -f "$LOCKFILE" ]; then
    PID=$(cat "$LOCKFILE")
    if ps -p $PID > /dev/null 2>&1; then
        echo "Error: Orchestrator Loop for BID 7491 is already running (PID: $PID)."
        exit 1
    fi
fi

echo $$ > "$LOCKFILE"
trap "rm -f $LOCKFILE" EXIT

echo "Starting Orchestrator Loop for BID 7491. Running every 5 minutes."

while true; do
    echo "--- [$(date)] Executing orchestrate_pipeline.py for BID 7491 ---"

    python3 orchestrate_pipeline.py --bid 7491 --skip-ingest \
        --transcribe-limit 20 \
        --analyze-limit 20

    echo "--- [$(date)] Finished iteration. Sleeping for 5 minutes ---"
    sleep 300
done
