refactor(fudai): 抽 _handle_in_fudai_detail 中"task_count_zero 错误处理"块到 handle_task_count_zero_error

将"last_status == error 且 reason == task_count_zero"分支(约 87 行深嵌套)抽到 helper:
- 累加 _fudai_detail_consecutive_failures 计数
- 连续 3 次失败:强制关闭详情 + 切换直播间 + 重置计数 + 写完成日志
- 否则:写日志 + 写完成日志

返回值约定:
- True:已处理 task_count_zero 错误(调用方应直接 return)
- False:不是 task_count_zero 错误或处理异常,调用方继续

douyin_fudai.py: 1606 → 1525 行 (-81)
fudai_detail_helpers.py: 2398 → 2493 行

douyin_fudai.py 跌破 1600 行(4121 → 1525,本会话累计削减 2596 行 = -63%)

验证:
- pytest 1013 passed
- compile OK

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
CodexandClaude Opus 4.7 committed 2026-05-22 12:22:18 +08:00
1 parent 5f713afc5c
commit 0eb4670d46
2 files changed
+100 -86

No files matched your search

+5 -86
View File
@@ -1295,93 +1295,12 @@ class fudai_analyse:
return
elif last_status == "error":
# 检查是否是任务数量为0的错误
try:
last_extra = getattr(self, "_last_fudai_detail_extra", {})
if (
isinstance(last_extra, dict)
and last_extra.get("reason") == "task_count_zero"
):
# 记录连续失败次数
consecutive_failures = (
getattr(self, "_fudai_detail_consecutive_failures", 0) or 0
)
consecutive_failures += 1
try:
self._fudai_detail_consecutive_failures = (
consecutive_failures
)
except Exception:
pass
from douyin.core.fudai_modules.fudai_detail_helpers import (
handle_task_count_zero_error,
)
# 如果连续失败3次,强制关闭详情页并切换直播间,避免卡死
if consecutive_failures >= 3:
try:
self._log_kv(
"流程",
"_handle_in_fudai_detail:连续3次识别到任务数量为0,强制关闭详情页并切换直播间,避免卡死",
consecutive_failures=consecutive_failures,
)
except Exception:
pass
# 强制关闭详情页
try:
douyin_fudai_detail.close_fudai_detail(self)
self.operation.delay(0.5)
except Exception:
pass
# 切换直播间
try:
needswitch = bool(getattr(self, "needswitch", True))
except Exception:
needswitch = True
if needswitch:
try:
from douyin.core.fudai_modules.fudai_detail_helpers import (
fallback_swipe_up_to_next_live,
)
fallback_swipe_up_to_next_live(self)
self.operation.delay(0.5)
except Exception:
pass
# 重置失败计数
try:
self._fudai_detail_consecutive_failures = 0
except Exception:
pass
# 清除任务数量标志
clear_task_count_flag(self)
elapsed = time.time() - start_time
self._log_kv(
"流程",
"_handle_in_fudai_detail完成(连续失败)",
elapsed_seconds=round(elapsed, 2),
)
return
else:
try:
self._log_kv(
"流程",
"_handle_in_fudai_detail:检测到任务数量为0,跳过后续处理",
consecutive_failures=consecutive_failures,
)
except Exception:
pass
# 清除任务数量标志
clear_task_count_flag(self)
elapsed = time.time() - start_time
self._log_kv(
"流程",
"_handle_in_fudai_detail完成(任务数量为0)",
elapsed_seconds=round(elapsed, 2),
)
return
except Exception:
pass
if handle_task_count_zero_error(self, start_time):
return
except Exception:
pass
@@ -42,6 +42,101 @@ def log_screenshot_failed(
pass
def handle_task_count_zero_error(analyser, start_time: float) -> bool:
"""_handle_in_fudai_detail 中"task_count_zero"错误的处理。
流程:
- 累加 _fudai_detail_consecutive_failures 计数
- 连续 3 次失败:强制关闭详情 + 切换直播间 + 重置计数 + 写完成日志
- 否则:写日志 + 写完成日志
返回:
True:已处理 task_count_zero 错误(调用方应直接 return)
False:不是 task_count_zero 错误或处理异常,调用方继续
"""
from douyin.fudai import douyin_fudai_detail
try:
last_extra = getattr(analyser, "_last_fudai_detail_extra", {})
if not (
isinstance(last_extra, dict)
and last_extra.get("reason") == "task_count_zero"
):
return False
# 记录连续失败次数
consecutive_failures = (
getattr(analyser, "_fudai_detail_consecutive_failures", 0) or 0
)
consecutive_failures += 1
try:
analyser._fudai_detail_consecutive_failures = consecutive_failures
except Exception:
pass
if consecutive_failures >= 3:
# 连续失败 3 次:强制关闭 + 切换 + 重置
try:
analyser._log_kv(
"流程",
"_handle_in_fudai_detail:连续3次识别到任务数量为0,强制关闭详情页并切换直播间,避免卡死",
consecutive_failures=consecutive_failures,
)
except Exception:
pass
try:
douyin_fudai_detail.close_fudai_detail(analyser)
analyser.operation.delay(0.5)
except Exception:
pass
try:
needswitch = bool(getattr(analyser, "needswitch", True))
except Exception:
needswitch = True
if needswitch:
try:
fallback_swipe_up_to_next_live(analyser)
analyser.operation.delay(0.5)
except Exception:
pass
try:
analyser._fudai_detail_consecutive_failures = 0
except Exception:
pass
clear_task_count_flag(analyser)
elapsed = time.time() - start_time
analyser._log_kv(
"流程",
"_handle_in_fudai_detail完成(连续失败)",
elapsed_seconds=round(elapsed, 2),
)
return True
else:
try:
analyser._log_kv(
"流程",
"_handle_in_fudai_detail:检测到任务数量为0,跳过后续处理",
consecutive_failures=consecutive_failures,
)
except Exception:
pass
clear_task_count_flag(analyser)
elapsed = time.time() - start_time
analyser._log_kv(
"流程",
"_handle_in_fudai_detail完成(任务数量为0)",
elapsed_seconds=round(elapsed, 2),
)
return True
except Exception:
return False
def process_reservation_multi_room_mode(
analyser,
focus_ref,