zc2 0.0.29

P2P compute broker with credit-based billing, WAL, and broker mesh support
#!/usr/bin/env python3
"""
Trigger a single /execute request that lasts ~30 seconds (worker sleeps 30s).
Watch the execution flow meanwhile with `watch -n 1 zc workers localhost:9000`.

Requires: ZAKURO_API_KEY and ZAKURO_API_URL (default http://localhost:9000).
"""

import os
import sys
import time
import urllib.request
import urllib.error
import json

def main():
    base = os.environ.get("ZAKURO_API_URL", "http://localhost:9000").rstrip("/")
    key = os.environ.get("ZAKURO_API_KEY")
    if not key:
        print("Set ZAKURO_API_KEY to run this script.", file=sys.stderr)
        sys.exit(1)

    url = f"{base}/execute"
    body = json.dumps({"n": 4, "sleep": 30}).encode("utf-8")
    req = urllib.request.Request(
        url,
        data=body,
        method="POST",
        headers={
            "Content-Type": "application/json",
            "Authorization": f"Bearer {key}",
            "X-Zakuro-Requirements": json.dumps({
                "strategy": "round_robin",
                "estimated_duration_secs": 30,
            }),
        },
    )

    print("Sending one /execute request (worker will sleep 30s)...")
    t0 = time.perf_counter()
    try:
        with urllib.request.urlopen(req, timeout=65) as resp:
            data = resp.read()
            elapsed = time.perf_counter() - t0
            print(f"OK in {elapsed:.1f}s")
            try:
                j = json.loads(data.decode("utf-8"))
                print(json.dumps(j, indent=2))
            except Exception:
                print(data[:500].decode("utf-8", errors="replace"))
    except urllib.error.HTTPError as e:
        elapsed = time.perf_counter() - t0
        body = e.read()[:500]
        try:
            body_str = body.decode("utf-8", errors="replace")
        except Exception:
            body_str = repr(body)
        print(f"HTTP {e.code} after {elapsed:.1f}s: {body_str}", file=sys.stderr)
        sys.exit(1)
    except Exception as e:
        print(f"Error: {e}", file=sys.stderr)
        sys.exit(1)

if __name__ == "__main__":
    main()