#!/usr/bin/env python3
"""Poll until sf_delivery_watchdog / its pull_orders child are gone."""
import subprocess, time, sys

DEADLINE = time.time() + 1500  # 25 min
PATTERNS = ["sf_delivery_watchdog"]
while time.time() < DEADLINE:
    alive = []
    for p in PATTERNS:
        r = subprocess.run(["pgrep", "-f", p], capture_output=True, text=True)
        if r.returncode == 0 and r.stdout.strip():
            alive.append((p, r.stdout.strip().replace("\n", ",")))
    if not alive:
        print("WATCHDOG_GONE", time.strftime("%H:%M:%S"))
        sys.exit(0)
    print("waiting", alive, time.strftime("%H:%M:%S"), flush=True)
    time.sleep(15)
print("WATCHDOG_TIMEOUT", time.strftime("%H:%M:%S"))
sys.exit(1)
