Download ops/keeper.py from dragonlimited/dragon-state: direct link, hf CLI and curl.
- Browser
- Download file 4.45 kB
-
https://huggingface.co/dragonlimited/dragon-state/resolve/main/ops/keeper.py
- Command line
-
hf download hf://dragonlimited/dragon-state/ops/keeper.py
-
curl -L -o keeper.py https://huggingface.co/dragonlimited/dragon-state/resolve/main/ops/keeper.py
4.45 kB
| """KEEPER - the process that must always be alive. | |
| Everything else is restartable; this is what restarts it. One job: make sure the services | |
| are running, and never die. | |
| WHY IT EXISTS | |
| The workplan manager died and nothing noticed. The box sat idle with 845,960 generated | |
| traces and 3.5M indexed chunks of work already done. A queue is useless if the thing | |
| draining it can vanish silently. | |
| SERVICES | |
| sync_state keeps state on the Hub, so a relaunch resumes | |
| cpu_lane continuous trace generation, never drains | |
| run_workplan admits GPU jobs by measured footprint | |
| The keeper is idempotent: running two copies is harmless because each service checks a | |
| pidfile/lock before starting. Restart is backed off so a crash loop cannot spin. | |
| """ | |
| from __future__ import annotations | |
| import json | |
| import os | |
| import pathlib | |
| import subprocess | |
| import time | |
| W = pathlib.Path("/home/marimo/dragon_fsds") | |
| TOKEN = os.environ.get("HF_TOKEN_BOOT") or os.environ.get("HF_TOKEN") | |
| LOG = W / "keeper.log" | |
| STATE = W / "keeper_state.json" | |
| SERVICES = [ | |
| ("sync_state.py", "sync_watch.log", {}), | |
| ("cpu_lane.py", "cpu_lane_log.txt", {}), | |
| ("run_workplan.py", "workplan_log.txt", {"WP_GPU_SLOTS": "3", "WP_HEAVY_MAX": "1", | |
| "WP_RESERVE": "12000"}), | |
| ] | |
| def log(s: str) -> None: | |
| line = time.strftime("%H:%M:%S") + " [keeper] " + s | |
| print(line, flush=True) | |
| with open(LOG, "a") as f: | |
| f.write(line + "\n") | |
| import sys as _sys | |
| _sys.path.insert(0, str(pathlib.Path(__file__).parent)) | |
| from procutil import is_running, running_table | |
| def script_alive(name: str) -> bool: | |
| """pgrep -f matched its own shell and reported every service alive. | |
| procutil reads /proc/<pid>/cmdline and cannot self-match.""" | |
| return is_running(name) | |
| def gpu_mib(): | |
| try: | |
| return int(subprocess.run(["bash", "-lc", | |
| "nvidia-smi --query-gpu=memory.used --format=csv,noheader,nounits"], | |
| capture_output=True, text=True, timeout=10).stdout.strip()) | |
| except Exception: | |
| return -1 | |
| log("keeper starting") | |
| fails = {n: 0 for n, _, _ in SERVICES} | |
| backoff = {n: 0.0 for n, _, _ in SERVICES} | |
| def _pids_for(script_name: str) -> list[int]: | |
| import os as _os | |
| out = [] | |
| for e in _os.listdir("/proc"): | |
| if not e.isdigit(): | |
| continue | |
| try: | |
| parts = [p for p in pathlib.Path(f"/proc/{e}/cmdline").read_bytes().split(b"\0") if p] | |
| except Exception: | |
| continue | |
| if len(parts) >= 2 and b"python" in pathlib.Path(parts[0].decode()).name.encode() \ | |
| and parts[1].decode("utf-8", "replace").endswith(script_name): | |
| out.append(int(e)) | |
| return sorted(out) | |
| def _dedupe(script_name: str) -> int: | |
| """Keep the oldest copy, kill the rest. Duplicates double VRAM for no throughput.""" | |
| pids = _pids_for(script_name) | |
| killed = 0 | |
| for pid in pids[1:]: | |
| try: | |
| os.kill(pid, 15) | |
| killed += 1 | |
| except Exception: | |
| pass | |
| if killed: | |
| log(f"deduped {script_name}: kept {pids[0]}, killed {killed}") | |
| return killed | |
| while True: | |
| for _name, _, _ in SERVICES: | |
| _dedupe(_name) | |
| act = [] | |
| for name, logfile, env_extra in SERVICES: | |
| if not (W / name).exists(): | |
| log(f"{name}: script absent"); continue | |
| if script_alive(name): | |
| act.append(name); continue | |
| # back off on repeat failures so a crash loop cannot spin | |
| now = time.time() | |
| if now < backoff[name]: | |
| continue | |
| fails[name] += 1 | |
| backoff[name] = now + min(30 * fails[name], 300) | |
| log(f"{name} is DOWN (failure {fails[name]}) - starting") | |
| env = dict(os.environ, HF_TOKEN_BOOT=TOKEN, **env_extra) | |
| subprocess.Popen(["python", str(W / name)], | |
| stdout=open(W / logfile, "a"), stderr=subprocess.STDOUT, | |
| env=env, start_new_session=True) | |
| time.sleep(5) | |
| act.append(name) | |
| mib = gpu_mib() | |
| STATE.write_text(json.dumps( | |
| {"at": time.time(), "services": act, "failures": fails, | |
| "vram_used_mib": mib, "vram_free_mib": 97887 - mib if mib >= 0 else None}, | |
| indent=2)) | |
| if mib > 91887: | |
| log(f"!! VRAM {mib} MiB above ceiling; the workplan manager should be holding " | |
| f"admission. If this persists, jobs are leaking.") | |
| time.sleep(30) | |