askrobots / dbbasic-pipe
Public
Two-way flow control for Unix pipes: a downstream command tells the upstream one how much it needs, like a SQL optimizer.
cli.py · 2.4 KB · 89 lines
| 1 | #!/usr/bin/env python3 |
| 2 | """ |
| 3 | Main CLI entry point for dbbasic-pipe. |
| 4 | |
| 5 | This is the smart-pipe wrapper that enables coordination. |
| 6 | """ |
| 7 | |
| 8 | import os |
| 9 | import sys |
| 10 | import subprocess |
| 11 | import signal |
| 12 | import time |
| 13 | |
| 14 | |
| 15 | def main(): |
| 16 | """Main entry point for dbbasic-pipe command.""" |
| 17 | if len(sys.argv) < 2: |
| 18 | print("Usage: dbbasic-pipe <command>", file=sys.stderr) |
| 19 | print("", file=sys.stderr) |
| 20 | print("Example:", file=sys.stderr) |
| 21 | print(' dbbasic-pipe bash -c "pcat data.json | pfilter \'age > 18\' | plimit 10"', file=sys.stderr) |
| 22 | sys.exit(1) |
| 23 | |
| 24 | # Find coordinator in same directory as this script |
| 25 | script_dir = os.path.dirname(os.path.abspath(__file__)) |
| 26 | coordinator_path = os.path.join(script_dir, "coordinator.py") |
| 27 | |
| 28 | # Create unique socket path |
| 29 | pid = os.getpid() |
| 30 | ctrl_sock = f"/tmp/pipe-ctrl-{pid}.sock" |
| 31 | |
| 32 | # Set environment variables for child processes |
| 33 | env = os.environ.copy() |
| 34 | env["PIPE_CONTROL_SOCKET"] = ctrl_sock |
| 35 | env["PIPE_SESSION_ID"] = str(pid) |
| 36 | |
| 37 | # Start coordinator |
| 38 | coordinator_proc = subprocess.Popen( |
| 39 | [sys.executable, coordinator_path, ctrl_sock], |
| 40 | env=env, |
| 41 | stderr=subprocess.PIPE |
| 42 | ) |
| 43 | |
| 44 | # Wait for socket to be ready |
| 45 | for _ in range(50): |
| 46 | if os.path.exists(ctrl_sock): |
| 47 | break |
| 48 | time.sleep(0.01) |
| 49 | else: |
| 50 | print(f"[dbbasic-pipe] Error: Failed to start coordinator", file=sys.stderr) |
| 51 | coordinator_proc.terminate() |
| 52 | sys.exit(1) |
| 53 | |
| 54 | print(f"[dbbasic-pipe] Coordinator ready (PID: {coordinator_proc.pid})", file=sys.stderr) |
| 55 | |
| 56 | # Setup cleanup handler |
| 57 | def cleanup(signum=None, frame=None): |
| 58 | print(f"[dbbasic-pipe] Cleaning up...", file=sys.stderr) |
| 59 | coordinator_proc.terminate() |
| 60 | try: |
| 61 | coordinator_proc.wait(timeout=1) |
| 62 | except subprocess.TimeoutExpired: |
| 63 | coordinator_proc.kill() |
| 64 | try: |
| 65 | os.unlink(ctrl_sock) |
| 66 | except FileNotFoundError: |
| 67 | pass |
| 68 | |
| 69 | # Register signal handlers |
| 70 | signal.signal(signal.SIGINT, cleanup) |
| 71 | signal.signal(signal.SIGTERM, cleanup) |
| 72 | |
| 73 | try: |
| 74 | # Run the actual pipeline |
| 75 | result = subprocess.run(sys.argv[1:], env=env) |
| 76 | exit_code = result.returncode |
| 77 | except KeyboardInterrupt: |
| 78 | exit_code = 130 |
| 79 | except Exception as e: |
| 80 | print(f"[dbbasic-pipe] Error: {e}", file=sys.stderr) |
| 81 | exit_code = 1 |
| 82 | finally: |
| 83 | cleanup() |
| 84 | |
| 85 | sys.exit(exit_code) |
| 86 | |
| 87 | |
| 88 | if __name__ == "__main__": |
| 89 | main() |