停止大模型推理服务
(signum=None, frame=None)
| 169 | |
| 170 | |
| 171 | def stop_server(signum=None, frame=None): |
| 172 | """停止大模型推理服务""" |
| 173 | pid_port = get_server_pid() |
| 174 | if pid_port is None: |
| 175 | if signum: |
| 176 | sys.exit(0) |
| 177 | return jsonify({"status": "error", "message": "Service is not running"}), 400 |
| 178 | |
| 179 | server_pid, _ = pid_port["PID"], pid_port["PORT"] |
| 180 | |
| 181 | # 清理PID文件 |
| 182 | if os.path.exists(PID_FILE): |
| 183 | os.remove(PID_FILE) |
| 184 | if os.path.exists("gemm_profiles.json"): |
| 185 | os.remove("gemm_profiles.json") |
| 186 | |
| 187 | try: |
| 188 | clean_ports() |
| 189 | # 终止进程组(包括所有子进程) |
| 190 | os.killpg(os.getpgid(pid_port["PID"]), signal.SIGTERM) |
| 191 | except Exception as e: |
| 192 | print(f"Failed to stop server: {e}, {str(traceback.format_exc())}") |
| 193 | try: |
| 194 | result = subprocess.run( |
| 195 | f"ps -efww | grep {FD_CACHE_QUEUE_PORT} | grep -v grep", shell=True, capture_output=True, text=True |
| 196 | ) |
| 197 | for line in result.stdout.strip().split("\n"): |
| 198 | if not line: |
| 199 | continue |
| 200 | parts = line.split() |
| 201 | pid = int(parts[1]) |
| 202 | print(f"Killing PID: {pid}") |
| 203 | os.kill(pid, signal.SIGKILL) |
| 204 | except Exception as e: |
| 205 | print(f"Failed to kill cache manager process: {e}, {str(traceback.format_exc())}") |
| 206 | |
| 207 | for port in [FD_API_PORT, FD_ENGINE_QUEUE_PORT, FD_METRICS_PORT, FD_CACHE_QUEUE_PORT]: |
| 208 | try: |
| 209 | output = subprocess.check_output(f"lsof -i:{port} -t", shell=True).decode().strip() |
| 210 | for pid in output.splitlines(): |
| 211 | os.kill(int(pid), signal.SIGKILL) |
| 212 | print(f"Killed process on port {port}, pid={pid}") |
| 213 | except Exception as e: |
| 214 | print(f"Failed to kill process on port: {e}, {str(traceback.format_exc())}") |
| 215 | # 若log目录存在,则重命名为log_timestamp |
| 216 | if os.path.isdir("./log"): |
| 217 | os.rename("./log", "./log_{}".format(time.strftime("%Y%m%d%H%M%S"))) |
| 218 | if os.path.exists("gemm_profiles.json"): |
| 219 | os.remove("gemm_profiles.json") |
| 220 | |
| 221 | if signum: |
| 222 | sys.exit(0) |
| 223 | |
| 224 | return jsonify({"status": "success", "message": "Service stopped", "pid": server_pid}), 200 |
| 225 | |
| 226 | |
| 227 | # 捕获 SIGINT (Ctrl+C) 和 SIGTERM (kill) |
no test coverage detected