79 lines
2.5 KiB
Python
79 lines
2.5 KiB
Python
"""Foreground local browser check; stop every owned service before returning."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import signal
|
|
import subprocess
|
|
import sys
|
|
import time
|
|
import urllib.error
|
|
import urllib.request
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
OUT = ROOT / ".local-evidence/frames-document/browser"
|
|
|
|
|
|
def main():
|
|
OUT.mkdir(parents=True, exist_ok=True)
|
|
# Refuse to reuse another operator's service or port.
|
|
import socket
|
|
|
|
for port in (8247, 5173):
|
|
with socket.socket() as sock:
|
|
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
|
sock.bind(("127.0.0.1", port))
|
|
processes = []
|
|
logs = []
|
|
try:
|
|
commands = [
|
|
([sys.executable, str(ROOT / "e2e/workflow-document-server.py")], ROOT),
|
|
(
|
|
["node", "node_modules/vite/bin/vite.js", "--host", "127.0.0.1"],
|
|
ROOT / "calliope-web",
|
|
),
|
|
]
|
|
for i, (command, cwd) in enumerate(commands):
|
|
log = (OUT / f"service-{i}.log").open("w")
|
|
logs.append(log)
|
|
processes.append(
|
|
subprocess.Popen(command, cwd=cwd, stdout=log, stderr=log, start_new_session=True)
|
|
)
|
|
for url in (
|
|
"http://127.0.0.1:8247/api/workflows",
|
|
"http://127.0.0.1:5173/connections",
|
|
):
|
|
deadline = time.monotonic() + 30
|
|
while True:
|
|
if any(p.poll() is not None for p in processes):
|
|
raise RuntimeError("Local service exited; inspect service logs")
|
|
try:
|
|
with urllib.request.urlopen(url, timeout=1) as response:
|
|
if response.status == 200:
|
|
break
|
|
except (urllib.error.URLError, TimeoutError):
|
|
pass
|
|
if time.monotonic() > deadline:
|
|
raise TimeoutError(url)
|
|
time.sleep(0.2)
|
|
names = ["frames"]
|
|
for name in names:
|
|
subprocess.run(["node", f"e2e/{name}-document.mjs"], cwd=ROOT, check=True)
|
|
finally:
|
|
for p in processes:
|
|
if p.poll() is None:
|
|
os.killpg(p.pid, signal.SIGTERM)
|
|
try:
|
|
p.wait(timeout=10)
|
|
except subprocess.TimeoutExpired:
|
|
os.killpg(p.pid, signal.SIGKILL)
|
|
p.wait(timeout=5)
|
|
for log in logs:
|
|
log.close()
|
|
print("All owned browser services stopped.")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|