From 458b0cfc32cb2a19383086dbe34d343301943ad3 Mon Sep 17 00:00:00 2001 From: neil Date: Mon, 2 Nov 2026 05:48:05 -0800 Subject: [PATCH] Setup: show progress while stopping the running services After confirming, setup stops the old services and waits up to 30s for their ports to free, with no output. Show a spinner with elapsed seconds immediately (a plain line when not on a terminal), ending in a check mark. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01QaE1hpvyZTJixEbUUMj9Ca --- llmhost/cli.py | 54 +++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 47 insertions(+), 7 deletions(-) diff --git a/llmhost/cli.py b/llmhost/cli.py index 1db7269..ece6e64 100644 --- a/llmhost/cli.py +++ b/llmhost/cli.py @@ -6,6 +6,7 @@ import shutil import socket import subprocess import sys +import threading import time import urllib.request @@ -19,6 +20,44 @@ def say(msg=""): print(msg, flush=True) +class Spinner: + """Shows that a slow step is running: a spinner with elapsed seconds on a terminal, + a plain line otherwise. Ends with a check mark and how long it took.""" + FRAMES = "⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏" + + def __init__(self, msg): + self.msg = msg + self.tty = sys.stdout.isatty() + self._stop = threading.Event() + + def _spin(self): + i = 0 + while not self._stop.wait(0.1): + sys.stdout.write(f"\r{self.FRAMES[i % len(self.FRAMES)]} {self.msg}… {time.time() - self.t0:.0f}s ") + sys.stdout.flush() + i += 1 + + def __enter__(self): + self.t0 = time.time() + if self.tty: + sys.stdout.write(f"\n{self.FRAMES[0]} {self.msg}…") + sys.stdout.flush() + self._thread = threading.Thread(target=self._spin, daemon=True) + self._thread.start() + else: + say(f"\n{self.msg}…") + return self + + def __exit__(self, *exc): + if self.tty: + self._stop.set() + self._thread.join() + mark = "✗" if exc[0] else "✓" + sys.stdout.write(f"\r\033[K{mark} {self.msg} ({time.time() - self.t0:.0f}s)\n") + sys.stdout.flush() + return False + + def backend_module(name): if name == "vllm": from .backends import vllm @@ -102,13 +141,14 @@ def cmd_setup(a): # MLX needs two ports: the proxy, and mlx_lm.server on the next one. api_span = 2 if p["backend"] == "mlx" else 1 if not a.no_start: - for name in service.NAMES: - service.stop(name) - # Stopping returns before the old processes have let go of their ports (launchd - # especially), so wait for our own ports to close before checking what's free. - if old.get("api_port"): - ours = list(range(old["api_port"], old["api_port"] + api_span)) + [old.get("web_port", 8080)] - wait_ports_free(ours, timeout=30) + with Spinner("Stopping llm-host's running services"): + for name in service.NAMES: + service.stop(name) + # Stopping returns before the old processes have let go of their ports (launchd + # especially), so wait for our own ports to close before checking what's free. + if old.get("api_port"): + ours = list(range(old["api_port"], old["api_port"] + api_span)) + [old.get("web_port", 8080)] + wait_ports_free(ours, timeout=30) # Ports saved by an earlier setup are pinned: clients are configured with them, so a # busy saved port is an error, never a reason to move. Only a first install auto-picks. api_port = choose_port("API", a.api_port, old.get("api_port", 8000), api_span, a.no_start,