|
| 1 | +#!/usr/bin/env python3 |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +import time |
| 5 | +from pathlib import Path |
| 6 | +from typing import TYPE_CHECKING |
| 7 | + |
| 8 | +from psutil import Popen |
| 9 | + |
| 10 | +import helpers |
| 11 | + |
| 12 | +if TYPE_CHECKING: |
| 13 | + from typing_extensions import TypedDict |
| 14 | + |
| 15 | + class StatusResponse(TypedDict): |
| 16 | + is_running: bool |
| 17 | + version: str | None |
| 18 | + |
| 19 | + |
| 20 | +CURRENT_TVL_VERSION = "tvl-2.3" |
| 21 | + |
| 22 | +TROPIC_SERVER: Popen | None = None |
| 23 | +VERSION_RUNNING: str | None = None |
| 24 | + |
| 25 | +LOG_COLOR = "yellow" |
| 26 | +ROOT_DIR = Path(__file__).resolve().parent.parent |
| 27 | +TROPIC_MODEL_DIR = ROOT_DIR / "tropic_model" |
| 28 | +START_SCRIPT = TROPIC_MODEL_DIR / "start-emulator.sh" |
| 29 | + |
| 30 | + |
| 31 | +def log(text: str, color: str = LOG_COLOR) -> None: |
| 32 | + helpers.log(f"TROPIC_MODEL: {text}", color) |
| 33 | + |
| 34 | + |
| 35 | +def is_running() -> bool: |
| 36 | + """Check if tropic model server process is running""" |
| 37 | + if TROPIC_SERVER is None: |
| 38 | + return False |
| 39 | + return TROPIC_SERVER.is_running() |
| 40 | + |
| 41 | + |
| 42 | +def get_status() -> "StatusResponse": |
| 43 | + """Return status dict with running state and version""" |
| 44 | + return {"is_running": is_running(), "version": VERSION_RUNNING} |
| 45 | + |
| 46 | + |
| 47 | +def start(output_to_logfile: bool = True) -> None: |
| 48 | + """Start the Tropic Square model server""" |
| 49 | + log("Starting") |
| 50 | + global TROPIC_SERVER |
| 51 | + global VERSION_RUNNING |
| 52 | + |
| 53 | + # In case the server was killed outside of the stop() function |
| 54 | + # (for example manually by the user), we need to reflect the situation |
| 55 | + if TROPIC_SERVER is not None and not is_running(): |
| 56 | + log("Tropic server was probably killed manually, resetting local state") |
| 57 | + stop() |
| 58 | + |
| 59 | + if TROPIC_SERVER is not None: |
| 60 | + log( |
| 61 | + "WARNING: Tropic model server is already running, not spawning a new one", |
| 62 | + "red", |
| 63 | + ) |
| 64 | + return |
| 65 | + |
| 66 | + # Verify the start script exists |
| 67 | + if not START_SCRIPT.exists(): |
| 68 | + raise RuntimeError( |
| 69 | + f"Tropic model start script does not exist at {START_SCRIPT}" |
| 70 | + ) |
| 71 | + |
| 72 | + # Build command to run the start script |
| 73 | + command_list = ["bash", str(START_SCRIPT)] |
| 74 | + |
| 75 | + # Spawn the process, optionally redirecting output to logfile |
| 76 | + if output_to_logfile: |
| 77 | + log_file = open(helpers.TROPIC_MODEL_LOG, "a") |
| 78 | + log(f"All tropic model output redirected to {helpers.TROPIC_MODEL_LOG}") |
| 79 | + TROPIC_SERVER = Popen(command_list, stdout=log_file, stderr=log_file) |
| 80 | + else: |
| 81 | + TROPIC_SERVER = Popen(command_list) |
| 82 | + |
| 83 | + log(f"Tropic server spawned: {TROPIC_SERVER}. CMD: {TROPIC_SERVER.cmdline()}") |
| 84 | + |
| 85 | + # Verifying if the server is really running |
| 86 | + time.sleep(1.0) |
| 87 | + if not TROPIC_SERVER.is_running(): |
| 88 | + TROPIC_SERVER = None |
| 89 | + raise RuntimeError("Tropic model server is unable to run!") |
| 90 | + |
| 91 | + VERSION_RUNNING = CURRENT_TVL_VERSION |
| 92 | + log("Tropic model server started successfully") |
| 93 | + |
| 94 | + |
| 95 | +def stop() -> None: |
| 96 | + """Stop the Tropic Square model server""" |
| 97 | + log("Stopping") |
| 98 | + global TROPIC_SERVER |
| 99 | + global VERSION_RUNNING |
| 100 | + |
| 101 | + if TROPIC_SERVER is None: |
| 102 | + log("WARNING: Attempting to stop tropic server, but it is not running", "red") |
| 103 | + else: |
| 104 | + try: |
| 105 | + TROPIC_SERVER.terminate() |
| 106 | + TROPIC_SERVER.wait(timeout=5) |
| 107 | + except Exception as e: |
| 108 | + log(f"Termination failed: {repr(e)}, killing process.", "yellow") |
| 109 | + TROPIC_SERVER.kill() |
| 110 | + TROPIC_SERVER.wait() |
| 111 | + |
| 112 | + # Ensuring all child processes are cleaned up |
| 113 | + for child in TROPIC_SERVER.children(recursive=True): |
| 114 | + log(f"Killing child process {child.pid}") |
| 115 | + child.kill() |
| 116 | + child.wait() |
| 117 | + |
| 118 | + TROPIC_SERVER = None |
| 119 | + VERSION_RUNNING = None |
| 120 | + log("Tropic model server stopped") |
0 commit comments