"""One-off helper: zabij wszystkie running `bulk_rescrape_hqporner` Python procesy
w containerze. `pkill`/`kill` nie są dostępne w obrazie, więc używamy `os.kill`
po wyszukaniu PIDów w /proc.
"""
import glob
import os
import signal

self_pid = os.getpid()
killed: list[int] = []
found: list[tuple[int, str]] = []
for p in glob.glob("/proc/[0-9]*/cmdline"):
    try:
        with open(p, "rb") as f:
            raw = f.read()
    except Exception:
        continue
    cmd = raw.replace(b"\x00", b" ").decode("utf-8", errors="ignore")
    if "bulk_rescrape_hqporner" in cmd:
        try:
            pid = int(p.split("/")[2])
        except ValueError:
            continue
        if pid == self_pid:
            continue
        found.append((pid, cmd[:120]))

print(f"found {len(found)} bulk_rescrape processes:")
for pid, cmd in found:
    print(f"  pid={pid} cmd={cmd}")
    try:
        os.kill(pid, signal.SIGTERM)
        killed.append(pid)
    except ProcessLookupError:
        pass
    except Exception as e:
        print(f"  fail kill {pid}: {e}")

print(f"killed: {killed}")
