diff --git a/douyin/core/douyin_fudai.py b/douyin/core/douyin_fudai.py index 6264bc6..ee6e796 100644 --- a/douyin/core/douyin_fudai.py +++ b/douyin/core/douyin_fudai.py @@ -2571,39 +2571,8 @@ class fudai_analyse: ) except Exception: pass - # 关闭详情页并返回 - try: - douyin_fudai_detail.close_fudai_detail(self) - except Exception: - pass - - # 步骤2结束计时 - step2_elapsed = (time.time() - step2_start) * 1000 - self._log_kv( - "详情处理", - "步骤2[读取福袋详情]-完成", - 总耗时毫秒=step2_elapsed, - ) - - # 清除任务数量标志 - try: - self._fudai_detail_task_count_from_stage = None - try: - self._log_kv( - "福袋详情", - "_handle_in_fudai_detail:清除任务数量标志", - log_type="DEBUG", - ) - except Exception: - pass - except Exception: - pass - - elapsed = time.time() - start_time - self._log_kv( - "流程", - "_handle_in_fudai_detail完成(数据验证失败)", - elapsed_seconds=round(elapsed, 2), + fudai_detail_blocks.handle_validation_failure_cleanup( + self, start_time, step2_start ) return except Exception: diff --git a/douyin/core/fudai_modules/fudai_detail_blocks.py b/douyin/core/fudai_modules/fudai_detail_blocks.py index 60506fc..5ba334c 100644 --- a/douyin/core/fudai_modules/fudai_detail_blocks.py +++ b/douyin/core/fudai_modules/fudai_detail_blocks.py @@ -104,3 +104,52 @@ def handle_detail_is_none(analyser: Any, start_time: float) -> bool: except Exception: pass return True + + +def handle_validation_failure_cleanup( + analyser: Any, start_time: float, step2_start: float +) -> None: + """步骤2 数据验证失败的善后:关闭详情页 + 写日志 + 清除任务数量。 + + 与上面 handle_* 系列不同:本函数不返回布尔值,调用方在 if not is_valid + 分支中调用本函数后立即 return。这是因为该分支已经在外层的 try 中。 + """ + try: + from douyin.fudai import douyin_fudai_detail + + douyin_fudai_detail.close_fudai_detail(analyser) + except Exception: + pass + + try: + step2_elapsed = (time.time() - step2_start) * 1000 + analyser._log_kv( + "详情处理", + "步骤2[读取福袋详情]-完成", + 总耗时毫秒=step2_elapsed, + ) + except Exception: + pass + + try: + analyser._fudai_detail_task_count_from_stage = None + try: + analyser._log_kv( + "福袋详情", + "_handle_in_fudai_detail:清除任务数量标志", + log_type="DEBUG", + ) + except Exception: + pass + except Exception: + pass + + try: + elapsed = time.time() - start_time + analyser._log_kv( + "流程", + "_handle_in_fudai_detail完成(数据验证失败)", + elapsed_seconds=round(elapsed, 2), + ) + except Exception: + pass diff --git a/tests/test_fudai_detail_blocks.py b/tests/test_fudai_detail_blocks.py index a19400c..33e8baa 100644 --- a/tests/test_fudai_detail_blocks.py +++ b/tests/test_fudai_detail_blocks.py @@ -93,3 +93,31 @@ def test_detail_is_none_swallows_log_exceptions(): # 应当不抛错 result = fudai_detail_blocks.handle_detail_is_none(a, _time.time()) assert result is True + + +# ---- handle_validation_failure_cleanup ---- # + +def test_validation_cleanup_closes_detail_and_logs(): + a = MagicMock() + a._log_kv = MagicMock() + a._fudai_detail_task_count_from_stage = 7 + with patch("douyin.fudai.douyin_fudai_detail.close_fudai_detail") as cd: + fudai_detail_blocks.handle_validation_failure_cleanup( + a, _time.time() - 0.5, _time.time() - 0.3 + ) + cd.assert_called_once_with(a) + assert a._fudai_detail_task_count_from_stage is None + # 至少 3 次日志:步骤2完成 / 清除标志 / 完成(数据验证失败) + assert a._log_kv.call_count >= 2 + + +def test_validation_cleanup_close_failure_does_not_break(): + a = MagicMock() + with patch( + "douyin.fudai.douyin_fudai_detail.close_fudai_detail", + side_effect=RuntimeError("boom"), + ): + fudai_detail_blocks.handle_validation_failure_cleanup( + a, _time.time(), _time.time() + ) + assert a._fudai_detail_task_count_from_stage is None