#!/usr/bin/env python3
"""Socket health checker — checks if a unix socket is responsive."""
# TODO: Add proper logging instead of silent failure for debugging connection issues
# TODO: Add more specific exception handling to distinguish connection refused vs timeout
import socket, sys, json

if len(sys.argv) < 2:
    sys.exit(1)

sock_path = sys.argv[1]
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
s.setblocking(True)
try:
    s.connect(sock_path)
    s.settimeout(5)
    s.sendall(json.dumps({"type": "health"}).encode() + b"\n")
    data = s.recv(4096)
    if data:
        sys.exit(0)
    sys.exit(1)
except Exception:
    sys.exit(1)
finally:
    s.close()
