Skip to content

Commit 1cd46e1

Browse files
修复parse_exit语义
1 parent 123d5b0 commit 1cd46e1

3 files changed

Lines changed: 223 additions & 80 deletions

File tree

‎scripts/collect_log.py‎

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,19 @@ def _resolve_daily(daily_raw: bool | None, status: str) -> bool:
7474

7575

7676
def _resolve_exited(exited_raw: bool | None, status: str) -> bool:
77-
"""推导是否正常退出:日志有明确信号(True/False)时直接用;
77+
"""推导是否正常退出(进程是否正常收尾,与结果成败正交):
7878
79-
无日志(None)时按整体运行状态推断——整体跑成即视为已退出,
80-
失败 / 无日志即未正常退出。避免「未知」这类模棱两可的呈现。
79+
- 日志有明确信号(True/False)时直接用,不推断;
80+
- 仅在 parse_exit 判定不了(None)时,按状态做保守推断——
81+
无日志(NO_LOG)视为未正常退出,成功(SUCCESS)视为已退出。
82+
注意「正常退出」≠「成功」:结果失败但进程正常收尾(如部分失败)仍算正常退出;
83+
非正常退出(被杀/崩溃/异常结束)才是 FAILED 的一种成因,parse_exit 应判 False。
8184
"""
8285
if exited_raw is not None:
8386
return exited_raw
8487
return status == ScriptLogStatus.SUCCESS
8588

8689

87-
8890
_LOG_TAIL_LINES = 200
8991

9092
_LOG_CONFIGURED = False
@@ -379,7 +381,9 @@ def parse_daily(self, content: str) -> bool | None:
379381
return None
380382

381383
def parse_exit(self, content: str) -> bool | None:
382-
# 完成 / 部分失败 → 已正常跑完整轮;异常结束 / 运行中 → 异常退出。
384+
# 正常退出 = 进程跑完整轮自行收尾(完成 / 部分失败皆属此类);
385+
# 异常结束 / 运行中(被杀 / 崩溃)属非正常退出。parse_exit 只看进程是否正常退出,
386+
# 与结果成败正交——部分失败是「正常退出 + 结果失败」,仍算正常退出。
383387
if "执行状态: 完成" in content or "执行状态: 部分失败" in content:
384388
return True
385389
return False
@@ -551,7 +555,9 @@ def parse_log(script_name: str, script_path: str = "") -> dict:
551555
"""
552556
# 不支持的脚本在 parse_logs 入口已过滤,到此处即不可能。
553557
supported_names = {cls.script_name for cls in _PARSERS if cls.script_name}
554-
assert script_name in supported_names, f"不支持的脚本不应进入 parse_log: {script_name}"
558+
assert script_name in supported_names, (
559+
f"不支持的脚本不应进入 parse_log: {script_name}"
560+
)
555561
for parser_cls in _PARSERS:
556562
if script_name == parser_cls.script_name:
557563
result = parser_cls().parse(script_path)
@@ -593,7 +599,9 @@ def _format_diagnostic_sections(entries: list[dict], collapse_fn=None) -> str:
593599
log_info 打印,邮件调用方拼入正文(并传入 collapse_fn 折叠连续重复行防刷屏)。
594600
"""
595601
error_entries = [e for e in entries if e["result"]["errors"]]
596-
failed_entries = [e for e in entries if e["result"]["status"] == ScriptLogStatus.FAILED]
602+
failed_entries = [
603+
e for e in entries if e["result"]["status"] == ScriptLogStatus.FAILED
604+
]
597605
if not error_entries and not failed_entries:
598606
return ""
599607
lines: list[str] = []
@@ -682,15 +690,20 @@ def emit(line: str) -> None:
682690
exited = result["exited"]
683691
# 所有展示状态均已纳入 status_cn;未覆盖即属不可能,直接断言。
684692
assert display_status in status_cn, f"未覆盖的展示状态: {display_status}"
685-
emit(_pad_row([
686-
entry["display_name"],
687-
status_cn[display_status],
688-
str(stamina) if stamina is not None else "—",
689-
"是" if daily else "否",
690-
"是" if exited else "否",
691-
str(len(errors)),
692-
str(extra) if extra is not None else "—",
693-
], widths))
693+
emit(
694+
_pad_row(
695+
[
696+
entry["display_name"],
697+
status_cn[display_status],
698+
str(stamina) if stamina is not None else "—",
699+
"是" if daily else "否",
700+
"是" if exited else "否",
701+
str(len(errors)),
702+
str(extra) if extra is not None else "—",
703+
],
704+
widths,
705+
)
706+
)
694707
emit("=" * total)
695708

696709
emit(
@@ -743,16 +756,23 @@ def parse_logs(do_log: bool = True) -> dict[str, list[str] | str]:
743756
script_name = get_script_name(script)
744757
if not script_name or script_name not in supported:
745758
continue
746-
entries.append({
747-
"script_name": script_name,
748-
"display_name": script.get("display_name", script_name),
749-
"result": parse_log(script_name, script.get("script_path", "")),
750-
})
759+
entries.append(
760+
{
761+
"script_name": script_name,
762+
"display_name": script.get("display_name", script_name),
763+
"result": parse_log(script_name, script.get("script_path", "")),
764+
}
765+
)
751766

752767
rerun_list, notify_list = _prepare_action_lists(entries)
753768
report = _build_summary_report(entries, rerun_list, notify_list, do_log)
754769

755-
return {"rerun": rerun_list, "notify": notify_list, "report": report, "entries": entries}
770+
return {
771+
"rerun": rerun_list,
772+
"notify": notify_list,
773+
"report": report,
774+
"entries": entries,
775+
}
756776

757777

758778
if __name__ == "__main__":

0 commit comments

Comments
 (0)