diff --git a/src/main.py b/src/main.py index a573101..c08b7cd 100644 --- a/src/main.py +++ b/src/main.py @@ -157,6 +157,9 @@ async def main(): print("\n正在启动服务...\n") + # 记录总启动时间 + startup_time = datetime.now() + # 创建所有服务任务 tasks = [] for svc in SERVICES: @@ -179,10 +182,63 @@ async def main(): except KeyboardInterrupt: print("\n\n正在停止所有服务...") logger.info("收到停止信号,关闭所有服务") + + shutdown_start = datetime.now() + + # 取消所有任务 for task in tasks: task.cancel() - await asyncio.gather(*tasks, return_exceptions=True) - print("所有服务已停止。") + + # 等待任务完成并收集结果 + results = await asyncio.gather(*tasks, return_exceptions=True) + + shutdown_end = datetime.now() + shutdown_duration = (shutdown_end - shutdown_start).total_seconds() + total_runtime = (shutdown_end - startup_time).total_seconds() + + # 统计关闭结果 + print("\n" + "=" * 70) + print("服务关闭摘要".center(70)) + print("=" * 70) + + success_count = 0 + failed_services = [] + + for idx, svc in enumerate(SERVICES): + service_name = svc["name"] + result = results[idx] + + if isinstance(result, asyncio.CancelledError) or result is None: + print(f" ✓ {service_name:<20} 正常关闭") + success_count += 1 + else: + print(f" ✗ {service_name:<20} 关闭异常: {result}") + failed_services.append(service_name) + + print("=" * 70) + print(f"\n关闭统计:") + print(f" • 成功关闭: {success_count}/{len(SERVICES)}") + print(f" • 关闭耗时: {shutdown_duration:.2f} 秒") + print(f" • 总运行时间: {total_runtime:.2f} 秒") + + if failed_services: + print(f"\n⚠️ 以下服务关闭时出现异常: {', '.join(failed_services)}") + print(f" 请检查日志文件以获取详细信息:") + print(f" 日志目录: {project_root}/logs/") + print(f" 日志命名规则: .log") + print(f" 相关日志文件:") + for service_name in failed_services: + # 服务名到模块名映射 + module_map = { + "DataService": "data_service", + "CoreService": "core_service", + "QMTService": "qmt_service", + "StrategyEngine": "strategy_engine", + } + module_name = module_map.get(service_name, service_name.lower()) + print(f" - logs/{module_name}.log") + + print("\n所有服务已停止。\n") def run_single(service_name: str):