|
| 1 | +import time |
| 2 | +import requests |
| 3 | + |
| 4 | + |
| 5 | +def update_peer(endpoint, model, ipv4, port): |
| 6 | + peer = { |
| 7 | + "service": [ |
| 8 | + { |
| 9 | + "name": "llm", |
| 10 | + "status": "online", |
| 11 | + "hardware": [], |
| 12 | + "host": ipv4, |
| 13 | + "port": port, |
| 14 | + "identity_group": [f"model={model}"], |
| 15 | + } |
| 16 | + ] |
| 17 | + } |
| 18 | + res = requests.post(endpoint + "/v1/dnt/_node", json=peer) |
| 19 | + print(res.text) |
| 20 | + |
| 21 | + |
| 22 | +def health_check(args): |
| 23 | + local_address = f"http://{args.local_ip}:{args.service_port}/health" |
| 24 | + is_healthy = False |
| 25 | + while not is_healthy: |
| 26 | + try: |
| 27 | + print(f"Checking health of service at {local_address}", flush=True) |
| 28 | + res = requests.get(local_address, timeout=5) |
| 29 | + print(f"Service health check response: {res.status_code}", flush=True) |
| 30 | + if res.status_code == 200: |
| 31 | + is_healthy = True |
| 32 | + else: |
| 33 | + print(f"Service not ready yet, waiting for 5 seconds", flush=True) |
| 34 | + time.sleep(5) |
| 35 | + except Exception as e: |
| 36 | + print(f"Service not ready yet, waiting for 5 seconds", flush=True) |
| 37 | + time.sleep(5) |
| 38 | + return is_healthy |
| 39 | + |
| 40 | + |
| 41 | +def register(args): |
| 42 | + print(f"Registering service with config: {args}") |
| 43 | + if health_check(args): |
| 44 | + update_peer(args.ocf_addr, args.model_name, args.local_ip, args.service_port) |
| 45 | + else: |
| 46 | + print("Service is not healthy") |
| 47 | + |
| 48 | + |
| 49 | +if __name__ == "__main__": |
| 50 | + import argparse |
| 51 | + |
| 52 | + parser = argparse.ArgumentParser( |
| 53 | + description="Register local service to OCF network" |
| 54 | + ) |
| 55 | + parser.add_argument("--model-name", type=str, help="Model Name") |
| 56 | + parser.add_argument("--ocf-addr", type=str, default="http://localhost:8092") |
| 57 | + parser.add_argument("--local-ip", type=str, default="localhost") |
| 58 | + parser.add_argument("--service-port", type=str, default="8000") |
| 59 | + register(parser.parse_args()) |
0 commit comments