| 454 | |
| 455 | @app.route("/wait_for_infer", methods=["POST"]) |
| 456 | def wait_for_infer(): |
| 457 | timeout = int(request.args.get("timeout", 120)) # 可选超时时间,默认120秒 |
| 458 | interval = 2 |
| 459 | response_interval = 10 |
| 460 | start_time = time.time() |
| 461 | next_response_time = start_time |
| 462 | |
| 463 | def generate(): |
| 464 | nonlocal next_response_time |
| 465 | while True: |
| 466 | health, msg = is_server_running() |
| 467 | now = time.time() |
| 468 | |
| 469 | elapsed = time.time() - start_time |
| 470 | |
| 471 | if health: |
| 472 | ports_status = { |
| 473 | "api_port": FD_API_PORT if is_port_in_use(FD_API_PORT) else None, |
| 474 | "queue_port": FD_ENGINE_QUEUE_PORT if is_port_in_use(FD_ENGINE_QUEUE_PORT) else None, |
| 475 | "metrics_port": FD_METRICS_PORT if is_port_in_use(FD_METRICS_PORT) else None, |
| 476 | } |
| 477 | msg["status"] = "服务启动完成" |
| 478 | msg["ports_status"] = ports_status |
| 479 | yield json.dumps(msg, ensure_ascii=False) + "\n" |
| 480 | break |
| 481 | |
| 482 | if elapsed >= timeout: |
| 483 | |
| 484 | def tail_file(path, lines=50): |
| 485 | try: |
| 486 | with open(path, "r", encoding="utf-8", errors="ignore") as f: |
| 487 | return "".join(f.readlines()[-lines:]) |
| 488 | except Exception as e: |
| 489 | return f"[无法读取 {path}]: {e}, {str(traceback.format_exc())}\n" |
| 490 | |
| 491 | result = f"服务启动超时,耗时:[{timeout}s]\n\n" |
| 492 | result += "==== server.log tail 50 ====\n" |
| 493 | result += tail_file("server.log") |
| 494 | result += "\n==== log/workerlog.0 tail 50 ====\n" |
| 495 | result += tail_file("log/workerlog.0") |
| 496 | |
| 497 | yield result |
| 498 | break |
| 499 | |
| 500 | if now >= next_response_time: |
| 501 | msg["status"] = f"服务启动中,耗时:[{int(elapsed)}s]" |
| 502 | yield json.dumps(msg, ensure_ascii=False) + "\n" |
| 503 | next_response_time += response_interval |
| 504 | |
| 505 | time.sleep(interval) |
| 506 | |
| 507 | return Response(generate(), status=200, content_type="text/plain") |
| 508 | |
| 509 | |
| 510 | if __name__ == "__main__": |