|
| 1 | +"""The Docker STEMMUS_SCOPE model process wrapper.""" |
| 2 | +import os |
| 3 | +import socket as pysocket |
| 4 | +import warnings |
| 5 | +from time import sleep |
| 6 | +from typing import Any |
| 7 | +from PyStemmusScope.bmi.docker_utils import check_tags |
| 8 | +from PyStemmusScope.bmi.docker_utils import find_image |
| 9 | +from PyStemmusScope.bmi.docker_utils import make_docker_vols_binds |
| 10 | +from PyStemmusScope.bmi.utils import MATLAB_ERROR |
| 11 | +from PyStemmusScope.bmi.utils import PROCESS_FINALIZED |
| 12 | +from PyStemmusScope.bmi.utils import PROCESS_READY |
| 13 | +from PyStemmusScope.bmi.utils import MatlabError |
| 14 | +from PyStemmusScope.config_io import read_config |
| 15 | + |
| 16 | + |
| 17 | +try: |
| 18 | + import docker |
| 19 | +except ImportError: |
| 20 | + docker = None |
| 21 | + |
| 22 | + |
| 23 | +def _model_is_ready(socket: Any, client: Any, container_id: Any) -> None: |
| 24 | + return _wait_for_model(PROCESS_READY, socket, client, container_id) |
| 25 | + |
| 26 | + |
| 27 | +def _model_is_finalized(socket: Any, client: Any, container_id: Any) -> None: |
| 28 | + return _wait_for_model(PROCESS_FINALIZED, socket, client, container_id) |
| 29 | + |
| 30 | + |
| 31 | +def _wait_for_model(phrase: bytes, socket: Any, client: Any, container_id: Any) -> None: |
| 32 | + """Wait for the model to be ready to receive (more) commands, or is finalized.""" |
| 33 | + output = b"" |
| 34 | + |
| 35 | + while phrase not in output: |
| 36 | + try: |
| 37 | + data = socket.read(1) |
| 38 | + except TimeoutError as err: |
| 39 | + client.stop(container_id) |
| 40 | + logs = client.logs(container_id).decode("utf-8") |
| 41 | + msg = ( |
| 42 | + f"Container connection timed out '{container_id['Id']}'." |
| 43 | + f"\nPlease inspect logs:\n{logs}" |
| 44 | + ) |
| 45 | + raise TimeoutError(msg) from err |
| 46 | + |
| 47 | + if data is None: |
| 48 | + msg = "Could not read data from socket. Docker container might be dead." |
| 49 | + raise ConnectionError(msg) |
| 50 | + else: |
| 51 | + output += bytes(data) |
| 52 | + |
| 53 | + if MATLAB_ERROR in output: |
| 54 | + client.stop(container_id) |
| 55 | + logs = client.logs(container_id).decode("utf-8") |
| 56 | + msg = ( |
| 57 | + f"Error in container '{container_id['Id']}'.\n" |
| 58 | + f"Please inspect logs:\n{logs}" |
| 59 | + ) |
| 60 | + raise MatlabError(msg) |
| 61 | + |
| 62 | + |
| 63 | +def _attach_socket(client, container_id) -> Any: |
| 64 | + """Attach a socket to a container and add a timeout to it.""" |
| 65 | + connection_timeout = 30 # seconds |
| 66 | + |
| 67 | + socket = client.attach_socket(container_id, {"stdin": 1, "stdout": 1, "stream": 1}) |
| 68 | + if isinstance(socket, pysocket.SocketIO): |
| 69 | + socket._sock.settimeout(connection_timeout) # type: ignore |
| 70 | + else: |
| 71 | + warnings.warn( |
| 72 | + message=( |
| 73 | + "Unknown socket type found. This might cause issues with the Docker" |
| 74 | + " connection. \nPlease report this to the developers in an issue " |
| 75 | + "on: https://github.com/EcoExtreML/STEMMUS_SCOPE_Processing/issues" |
| 76 | + ), |
| 77 | + stacklevel=1, |
| 78 | + ) |
| 79 | + return socket |
| 80 | + |
| 81 | + |
| 82 | +class StemmusScopeDocker: |
| 83 | + """Communicate with a STEMMUS_SCOPE Docker container.""" |
| 84 | + |
| 85 | + # Default image, can be overridden with config: |
| 86 | + compatible_tags = ("1.5.0",) |
| 87 | + |
| 88 | + _process_ready_phrase = b"Select BMI mode:" |
| 89 | + _process_finalized_phrase = b"Finished clean up." |
| 90 | + |
| 91 | + def __init__(self, cfg_file: str): |
| 92 | + """Create the Docker container..""" |
| 93 | + self.cfg_file = cfg_file |
| 94 | + config = read_config(cfg_file) |
| 95 | + |
| 96 | + self.image = config["DockerImage"] |
| 97 | + find_image(self.image) |
| 98 | + check_tags(self.image, self.compatible_tags) |
| 99 | + |
| 100 | + self.client = docker.APIClient() |
| 101 | + |
| 102 | + vols, binds = make_docker_vols_binds(cfg_file) |
| 103 | + self.container_id = self.client.create_container( |
| 104 | + self.image, |
| 105 | + stdin_open=True, |
| 106 | + tty=True, |
| 107 | + detach=True, |
| 108 | + user=f"{os.getuid()}:{os.getgid()}", # ensure correct user for writing files. |
| 109 | + volumes=vols, |
| 110 | + host_config=self.client.create_host_config(binds=binds), |
| 111 | + ) |
| 112 | + |
| 113 | + self.running = False |
| 114 | + |
| 115 | + def _wait_for_model(self) -> None: |
| 116 | + """Wait for the model to be ready to receive (more) commands.""" |
| 117 | + _model_is_ready(self.socket, self.client, self.container_id) |
| 118 | + |
| 119 | + def is_alive(self) -> bool: |
| 120 | + """Return if the process is alive.""" |
| 121 | + return self.running |
| 122 | + |
| 123 | + def initialize(self) -> None: |
| 124 | + """Initialize the model and wait for it to be ready.""" |
| 125 | + if self.is_alive(): |
| 126 | + self.client.stop(self.container_id) |
| 127 | + |
| 128 | + self.client.start(self.container_id) |
| 129 | + self.socket = _attach_socket(self.client, self.container_id) |
| 130 | + |
| 131 | + self._wait_for_model() |
| 132 | + os.write( |
| 133 | + self.socket.fileno(), |
| 134 | + bytes(f'initialize "{self.cfg_file}"\n', encoding="utf-8"), |
| 135 | + ) |
| 136 | + self._wait_for_model() |
| 137 | + |
| 138 | + self.running = True |
| 139 | + |
| 140 | + def update(self) -> None: |
| 141 | + """Update the model and wait for it to be ready.""" |
| 142 | + if self.is_alive(): |
| 143 | + os.write(self.socket.fileno(), b"update\n") |
| 144 | + self._wait_for_model() |
| 145 | + else: |
| 146 | + msg = "Docker container is not alive. Please restart the model." |
| 147 | + raise ConnectionError(msg) |
| 148 | + |
| 149 | + def finalize(self) -> None: |
| 150 | + """Finalize the model.""" |
| 151 | + if self.is_alive(): |
| 152 | + os.write(self.socket.fileno(), b"finalize\n") |
| 153 | + _model_is_finalized( |
| 154 | + self.socket, |
| 155 | + self.client, |
| 156 | + self.container_id, |
| 157 | + ) |
| 158 | + sleep(0.5) # Ensure the container can stop cleanly. |
| 159 | + self.client.stop(self.container_id) |
| 160 | + self.running = False |
| 161 | + self.client.remove_container(self.container_id, v=True) |
| 162 | + else: |
| 163 | + pass |
0 commit comments