#!/usr/bin/env python3 """Run mod-ui under pw-jack and keep its mod-host session active.""" import argparse import signal import subprocess import sys import time from pathlib import Path from mod_ui_websocket import ModUiWebSocketClient def run_service( launcher, pw_jack, host, port, websocket_path, timeout, popen=subprocess.Popen, ): process = popen([str(pw_jack), str(launcher)]) stopping = [False] def request_stop(_signum, _frame): stopping[0] = True old_term = signal.signal(signal.SIGTERM, request_stop) old_int = signal.signal(signal.SIGINT, request_stop) client = ModUiWebSocketClient(host, port, websocket_path, timeout=2) deadline = time.monotonic() + timeout try: while not stopping[0]: if process.poll() is not None: raise RuntimeError(f"mod-ui launcher exited with status {process.returncode}") try: client.connect() break except OSError: if time.monotonic() >= deadline: raise RuntimeError( f"mod-ui WebSocket did not become ready on {host}:{port}{websocket_path}" ) time.sleep(0.25) if not stopping[0]: print("Waiting for mod-ui to finish loading the current pedalboard") while not stopping[0]: if process.poll() is not None: raise RuntimeError(f"mod-ui launcher exited with status {process.returncode}") message = client.serve_once(timeout=0.5) if message and message.startswith("loading_end "): break if time.monotonic() >= deadline: raise RuntimeError("mod-ui did not finish loading a pedalboard before timeout") print("mod-ui pedalboard loaded; keeping its mod-host session active") while not stopping[0]: if process.poll() is not None: raise RuntimeError(f"mod-ui launcher exited with status {process.returncode}") client.serve_once(timeout=0.5) finally: signal.signal(signal.SIGTERM, old_term) signal.signal(signal.SIGINT, old_int) client.close() if process.poll() is None: process.terminate() try: process.wait(timeout=5) except subprocess.TimeoutExpired: process.kill() process.wait() def main(argv=None): root = Path(__file__).resolve().parents[1] parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--launcher", type=Path, required=True) parser.add_argument("--pw-jack", type=Path, default=Path("/usr/bin/pw-jack")) parser.add_argument("--host", default="127.0.0.1") parser.add_argument("--port", type=int, default=8888) parser.add_argument("--websocket-path", default="/websocket") parser.add_argument("--timeout", type=float, default=60) args = parser.parse_args(argv) if not args.launcher.is_file(): print(f"ERROR: mod-ui launcher is missing: {args.launcher}", file=sys.stderr) return 1 if not args.pw_jack.is_file(): print(f"ERROR: pw-jack is missing: {args.pw_jack}", file=sys.stderr) return 1 try: run_service( args.launcher, args.pw_jack, args.host, args.port, args.websocket_path, args.timeout, ) except (OSError, RuntimeError, ValueError) as error: print(f"ERROR: {error}", file=sys.stderr) return 1 return 0 if __name__ == "__main__": raise SystemExit(main())