From c4b6a8e5f082d0937e46367c812199f447beda54 Mon Sep 17 00:00:00 2001 From: Codex Date: Fri, 22 May 2026 05:15:49 +0800 Subject: [PATCH] =?UTF-8?q?refactor(reservation):=20=E6=8B=86=E5=87=BA?= =?UTF-8?q?=E5=9B=9E=E9=80=80=E6=88=90=E5=8A=9F=E5=90=8E=E7=9A=84=E9=94=81?= =?UTF-8?q?=E5=AE=9A=E6=9C=9F=20+=20=E5=AE=8C=E6=88=90=E6=97=A5=E5=BF=97?= =?UTF-8?q?=EF=BC=88530=E2=86=92467=EF=BC=8C-63=20=E8=A1=8C=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit enter_lock_period_and_log_success:进入锁定期 + 写运行日志 + 行为日志 + 状态文件 + 完成日志,把回退成功后的所有善后动作集中处理。 新增 3 个单元测试,1343 → 1346 用例全过。 Co-Authored-By: Claude Opus 4.7 (1M context) --- .../reservation_logic/attend_window.py | 99 ++++--------------- .../attend_window_helpers.py | 89 +++++++++++++++++ tests/test_attend_window_helpers.py | 47 +++++++++ 3 files changed, 154 insertions(+), 81 deletions(-) diff --git a/douyin/reservation/reservation_logic/attend_window.py b/douyin/reservation/reservation_logic/attend_window.py index 45d8409..c64fdad 100644 --- a/douyin/reservation/reservation_logic/attend_window.py +++ b/douyin/reservation/reservation_logic/attend_window.py @@ -29,6 +29,7 @@ from .attend_window_helpers import ( check_waiting_for_reservation_lottery, compute_dynamic_ahead, discard_invalid_future_ts, + enter_lock_period_and_log_success, execute_seek_reservation_callback, handle_lock_period_same_reservation_check, handle_time_window_early_returns, @@ -436,87 +437,23 @@ def process_reservation_attend_window( pass return False - # 进入锁定期:在开奖附近一段时间内不再切换直播间 - focus_until_new = ft + max(0.0, poll_before) - if reservation_focus_until_ts_ref: - reservation_focus_until_ts_ref[0] = focus_until_new - else: - reservation_focus_until_ts_ref.append(focus_until_new) - - if reservation_after_action_pending_ref: - reservation_after_action_pending_ref[0] = True - else: - reservation_after_action_pending_ref.append(True) - - try: - from datetime import datetime - - ft_str = ( - datetime.fromtimestamp(ft).strftime("%Y-%m-%d %H:%M:%S") - if ft > 0 - else "无效" - ) - - # 运行日志 - analyser._log_kv( - "预约", - f"[预约] 已回退到目标直播间,等待详情识别与参与 - 直播间:'{target_room_title[:30] if target_room_title else ''}', 开奖时间:{ft_str}, 价值:{target_value}元", - future_ts=int(ft), - future_str=ft_str, - focus_until=int(focus_until_new), - lock_period_seconds=int(focus_until_new - _now_ts()), - remain_seconds_to_open=int(ft - _now_ts()), - target_room_title=target_room_title, - target_room_title_raw=target_room_title_raw, - target_content=target_content, - target_value_yuan=target_value, - ) - - # 行为日志 - analyser._log_kv( - "行为", - "回退到预约直播间成功", - action="回退到预约直播间", - result="成功", - detail={ - "目标直播间": ( - target_room_title - if target_room_title - else target_room_title_raw - ), - "目标直播间原始": target_room_title_raw, - "福袋内容": target_content, - "福袋价值": target_value, - "预计开奖时间": ft_str, - "预计开奖时间戳": int(ft), - "剩余秒数": int(ft - _now_ts()) if ft > _now_ts() else 0, - }, - ) - except Exception: - pass - - # 不在这里弹出队列,等待真正"参与完毕"后由上层统一处理 - try: - write_state_callback() - except Exception: - pass - - # 添加函数完成日志 - try: - from datetime import datetime - - elapsed = time.time() - start_time - analyser._log_kv( - "预约", - "[预约] process_reservation_attend_window 返回结果", - result="已触发回退", - future_ts=int(ft), - now_ts=int(now_ts), - remain_seconds_to_open=int(ft - now_ts) if ft > now_ts else 0, - elapsed_seconds=round(elapsed, 3), - ) - except Exception: - pass + # 进入锁定期 + 写运行/行为日志 + 写状态文件 + 写完成日志 + enter_lock_period_and_log_success( + analyser, + target, + target_room_title, + target_room_title_raw, + target_content, + target_value, + ft, + now_ts, + poll_before, + start_time, + reservation_focus_until_ts_ref, + reservation_after_action_pending_ref, + write_state_callback, + _now_ts, + ) return True diff --git a/douyin/reservation/reservation_logic/attend_window_helpers.py b/douyin/reservation/reservation_logic/attend_window_helpers.py index cac3310..16ccbed 100644 --- a/douyin/reservation/reservation_logic/attend_window_helpers.py +++ b/douyin/reservation/reservation_logic/attend_window_helpers.py @@ -180,6 +180,95 @@ def _decide_retry_seek( return False +def enter_lock_period_and_log_success( + analyser: Any, + target: Any, + target_room_title: str, + target_room_title_raw: str, + target_content: str, + target_value: float, + ft: float, + now_ts: float, + poll_before: float, + start_time: float, + reservation_focus_until_ts_ref: list, + reservation_after_action_pending_ref: list, + write_state_callback: Any, + now_ts_func: Any, +) -> None: + """回退成功后:进入锁定期 + 写运行/行为日志 + 写状态文件 + 写完成日志。""" + focus_until_new = ft + max(0.0, poll_before) + if reservation_focus_until_ts_ref: + reservation_focus_until_ts_ref[0] = focus_until_new + else: + reservation_focus_until_ts_ref.append(focus_until_new) + + if reservation_after_action_pending_ref: + reservation_after_action_pending_ref[0] = True + else: + reservation_after_action_pending_ref.append(True) + + try: + ft_str = ( + datetime.fromtimestamp(ft).strftime("%Y-%m-%d %H:%M:%S") + if ft > 0 + else "无效" + ) + + analyser._log_kv( + "预约", + f"[预约] 已回退到目标直播间,等待详情识别与参与 - 直播间:'{target_room_title[:30] if target_room_title else ''}', 开奖时间:{ft_str}, 价值:{target_value}元", + future_ts=int(ft), + future_str=ft_str, + focus_until=int(focus_until_new), + lock_period_seconds=int(focus_until_new - now_ts_func()), + remain_seconds_to_open=int(ft - now_ts_func()), + target_room_title=target_room_title, + target_room_title_raw=target_room_title_raw, + target_content=target_content, + target_value_yuan=target_value, + ) + + analyser._log_kv( + "行为", + "回退到预约直播间成功", + action="回退到预约直播间", + result="成功", + detail={ + "目标直播间": ( + target_room_title if target_room_title else target_room_title_raw + ), + "目标直播间原始": target_room_title_raw, + "福袋内容": target_content, + "福袋价值": target_value, + "预计开奖时间": ft_str, + "预计开奖时间戳": int(ft), + "剩余秒数": int(ft - now_ts_func()) if ft > now_ts_func() else 0, + }, + ) + except Exception: + pass + + try: + write_state_callback() + except Exception: + pass + + try: + elapsed = time.time() - start_time + analyser._log_kv( + "预约", + "[预约] process_reservation_attend_window 返回结果", + result="已触发回退", + future_ts=int(ft), + now_ts=int(now_ts), + remain_seconds_to_open=int(ft - now_ts) if ft > now_ts else 0, + elapsed_seconds=round(elapsed, 3), + ) + except Exception: + pass + + def handle_verify_failure_retry_via_mapping( analyser: Any, target: Any, diff --git a/tests/test_attend_window_helpers.py b/tests/test_attend_window_helpers.py index bae5692..488f49d 100644 --- a/tests/test_attend_window_helpers.py +++ b/tests/test_attend_window_helpers.py @@ -856,3 +856,50 @@ def test_decide_retry_seek_correction_up_direction(): target = {} h._decide_retry_seek(a, target, 5, 5, 2) # 2 < 5 assert target["_seek_direction_hint"] == "up" + + +# ---- enter_lock_period_and_log_success ---- # + +def test_enter_lock_period_sets_focus_and_pending(): + a = MagicMock() + focus_ref = [0.0] + pending_ref = [False] + write_cb = MagicMock() + h.enter_lock_period_and_log_success( + a, target={"pos_seen": 5}, target_room_title="标题", target_room_title_raw="raw", + target_content="内容", target_value=99.9, + ft=1700000100.0, now_ts=1700000050.0, poll_before=10.0, + start_time=time.time(), reservation_focus_until_ts_ref=focus_ref, + reservation_after_action_pending_ref=pending_ref, + write_state_callback=write_cb, now_ts_func=lambda: 1700000050.0, + ) + assert focus_ref[0] == 1700000110.0 # ft + poll_before + assert pending_ref[0] is True + write_cb.assert_called_once() + + +def test_enter_lock_period_appends_to_empty_refs(): + a = MagicMock() + focus_ref = [] + pending_ref = [] + h.enter_lock_period_and_log_success( + a, target={}, target_room_title="", target_room_title_raw="", target_content="", + target_value=0.0, ft=1700000100.0, now_ts=0, poll_before=10.0, + start_time=time.time(), reservation_focus_until_ts_ref=focus_ref, + reservation_after_action_pending_ref=pending_ref, + write_state_callback=MagicMock(), now_ts_func=lambda: 0, + ) + assert focus_ref == [1700000110.0] + assert pending_ref == [True] + + +def test_enter_lock_period_swallows_log_exception(): + a = MagicMock() + a._log_kv.side_effect = RuntimeError("boom") + h.enter_lock_period_and_log_success( + a, target={}, target_room_title="", target_room_title_raw="", target_content="", + target_value=0.0, ft=0, now_ts=0, poll_before=0, + start_time=time.time(), reservation_focus_until_ts_ref=[0.0], + reservation_after_action_pending_ref=[False], + write_state_callback=MagicMock(), now_ts_func=lambda: 0, + )