(self, msg: dict[str, Any])
| 184 | } |
| 185 | |
| 186 | def handle(self, msg: dict[str, Any]) -> dict[str, Any]: |
| 187 | cmd = str(msg.get("cmd", "")).strip() |
| 188 | |
| 189 | if cmd == "ping": |
| 190 | return {"ok": True, "data": {"alive": True, "managed": len(self._managed)}} |
| 191 | |
| 192 | if cmd == "start_instance": |
| 193 | data = self._start_instance(msg) |
| 194 | return {"ok": True, "data": data} |
| 195 | |
| 196 | if cmd == "stop_pid": |
| 197 | pid = int(msg.get("pid", -1)) |
| 198 | timeout_seconds = float(msg.get("timeout_seconds", 10.0)) |
| 199 | if pid <= 0: |
| 200 | return {"ok": False, "error": "invalid pid"} |
| 201 | stopped = self._terminate_pid(pid, timeout_seconds=timeout_seconds) |
| 202 | return {"ok": bool(stopped), "data": {"pid": pid, "stopped": bool(stopped)}} |
| 203 | |
| 204 | if cmd == "shutdown": |
| 205 | self._running = False |
| 206 | return {"ok": True, "data": {"shutting_down": True}} |
| 207 | |
| 208 | if cmd == "stats": |
| 209 | managed_alive = 0 |
| 210 | for process in self._managed.values(): |
| 211 | if process.poll() is None: |
| 212 | managed_alive += 1 |
| 213 | return {"ok": True, "data": {"managed_alive": managed_alive}} |
| 214 | |
| 215 | return {"ok": False, "error": f"unsupported command: {cmd}"} |
| 216 | |
| 217 | def serve(self): |
| 218 | context = zmq.Context() |
no test coverage detected