refactor(fudai): 抽 run_once_basic 中"30分钟超时检查"块到 try_no_attend_timeout
将步骤 0"30分钟未参与成功福袋则返回推荐页"独立块(约 68 行)抽到 fudai_detail_helpers.try_no_attend_timeout。逻辑: - 如果距离 _last_successful_attend_ts > _no_attend_timeout_minutes 触发 - 如果从未参与且运行时长 > _no_attend_timeout_minutes 触发 - 触发时调用 _return_to_recommend_and_reenter_live + 写完成日志 返回值约定: - True:触发超时处理,调用方应直接 return - False:未触发,调用方继续 douyin_fudai.py: 2971 → 2910 行 (-61) fudai_detail_helpers.py: 745 → 819 行 验证: - pytest 1013 passed - compile OK Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
0114af2321
commit
e351745e92
2 files changed
+79
-66
No files matched your search
@@ -872,73 +872,12 @@ class fudai_analyse:
|
||||
self._current_attending_reservation_fudai = False
|
||||
|
||||
# 0. 检查30分钟超时:如果30分钟没有成功参与福袋,则返回推荐页面并重新进入直播间
|
||||
try:
|
||||
last_attend_ts = float(
|
||||
getattr(self, "_last_successful_attend_ts", 0.0) or 0.0
|
||||
)
|
||||
timeout_minutes = float(
|
||||
getattr(self, "_no_attend_timeout_minutes", 30.0) or 30.0
|
||||
)
|
||||
current_ts = time.time()
|
||||
run_start_ts = float(
|
||||
getattr(self, "_run_start_ts", current_ts) or current_ts
|
||||
)
|
||||
from douyin.core.fudai_modules.fudai_detail_helpers import (
|
||||
try_no_attend_timeout,
|
||||
)
|
||||
|
||||
# 如果从未成功参与过,且运行时间超过30分钟,则触发超时
|
||||
# 如果成功参与过,检查距离最后一次成功参与是否超过30分钟
|
||||
if last_attend_ts > 0:
|
||||
elapsed_minutes = (current_ts - last_attend_ts) / 60.0
|
||||
if elapsed_minutes >= timeout_minutes:
|
||||
try:
|
||||
self._log_kv(
|
||||
"超时",
|
||||
"30分钟未参与成功福袋,返回推荐页面重新进入直播间",
|
||||
last_attend_minutes_ago=round(elapsed_minutes, 1),
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
# 返回推荐页面并重新进入直播间
|
||||
self._return_to_recommend_and_reenter_live()
|
||||
# 重置最后成功参与时间,避免立即再次触发
|
||||
try:
|
||||
self._last_successful_attend_ts = current_ts
|
||||
except Exception:
|
||||
pass
|
||||
elapsed = time.time() - start_time
|
||||
self._log_kv(
|
||||
"流程",
|
||||
"run_once_basic完成(30分钟超时处理)",
|
||||
elapsed_seconds=round(elapsed, 2),
|
||||
)
|
||||
return
|
||||
else:
|
||||
# 从未成功参与过,检查运行时间
|
||||
elapsed_minutes = (current_ts - run_start_ts) / 60.0
|
||||
if elapsed_minutes >= timeout_minutes:
|
||||
try:
|
||||
self._log_kv(
|
||||
"超时",
|
||||
f"运行{round(elapsed_minutes, 1)}分钟仍未参与成功福袋,返回推荐页面重新进入直播间",
|
||||
run_minutes=round(elapsed_minutes, 1),
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
# 返回推荐页面并重新进入直播间
|
||||
self._return_to_recommend_and_reenter_live()
|
||||
# 重置运行开始时间(在成功重新进入直播间后重置,避免立即再次触发)
|
||||
# 注意:重置逻辑已移至 _return_to_recommend_and_reenter_live() 方法中
|
||||
elapsed = time.time() - start_time
|
||||
self._log_kv(
|
||||
"流程",
|
||||
"run_once_basic完成(30分钟超时处理)",
|
||||
elapsed_seconds=round(elapsed, 2),
|
||||
)
|
||||
return
|
||||
except Exception as exc:
|
||||
try:
|
||||
self._log_kv("超时", "检查30分钟超时时出错", error=str(exc))
|
||||
except Exception:
|
||||
pass
|
||||
if try_no_attend_timeout(self, start_time):
|
||||
return
|
||||
|
||||
# 0.1. 若启用高价值预约:先处理"是否需要回退到预约直播间"
|
||||
try:
|
||||
|
||||
@@ -42,6 +42,80 @@ def log_screenshot_failed(
|
||||
pass
|
||||
|
||||
|
||||
def try_no_attend_timeout(analyser, start_time: float) -> bool:
|
||||
"""run_once_basic 步骤 0:检查 30 分钟超时(默认配置)。
|
||||
|
||||
- 如果距离 _last_successful_attend_ts > _no_attend_timeout_minutes,触发返回推荐页
|
||||
- 如果从未成功参与且运行时长 > _no_attend_timeout_minutes,同样触发
|
||||
|
||||
返回:
|
||||
True:触发了超时处理(已返回推荐页 + 写完成日志),调用方应直接 return
|
||||
False:未触发,调用方继续
|
||||
"""
|
||||
try:
|
||||
last_attend_ts = float(
|
||||
getattr(analyser, "_last_successful_attend_ts", 0.0) or 0.0
|
||||
)
|
||||
timeout_minutes = float(
|
||||
getattr(analyser, "_no_attend_timeout_minutes", 30.0) or 30.0
|
||||
)
|
||||
current_ts = time.time()
|
||||
run_start_ts = float(
|
||||
getattr(analyser, "_run_start_ts", current_ts) or current_ts
|
||||
)
|
||||
|
||||
if last_attend_ts > 0:
|
||||
elapsed_minutes = (current_ts - last_attend_ts) / 60.0
|
||||
if elapsed_minutes >= timeout_minutes:
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"超时",
|
||||
"30分钟未参与成功福袋,返回推荐页面重新进入直播间",
|
||||
last_attend_minutes_ago=round(elapsed_minutes, 1),
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
analyser._return_to_recommend_and_reenter_live()
|
||||
try:
|
||||
analyser._last_successful_attend_ts = current_ts
|
||||
except Exception:
|
||||
pass
|
||||
elapsed = time.time() - start_time
|
||||
analyser._log_kv(
|
||||
"流程",
|
||||
"run_once_basic完成(30分钟超时处理)",
|
||||
elapsed_seconds=round(elapsed, 2),
|
||||
)
|
||||
return True
|
||||
else:
|
||||
# 从未成功参与过,检查运行时间
|
||||
elapsed_minutes = (current_ts - run_start_ts) / 60.0
|
||||
if elapsed_minutes >= timeout_minutes:
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"超时",
|
||||
f"运行{round(elapsed_minutes, 1)}分钟仍未参与成功福袋,返回推荐页面重新进入直播间",
|
||||
run_minutes=round(elapsed_minutes, 1),
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
analyser._return_to_recommend_and_reenter_live()
|
||||
# 重置运行开始时间已移至 _return_to_recommend_and_reenter_live 内
|
||||
elapsed = time.time() - start_time
|
||||
analyser._log_kv(
|
||||
"流程",
|
||||
"run_once_basic完成(30分钟超时处理)",
|
||||
elapsed_seconds=round(elapsed, 2),
|
||||
)
|
||||
return True
|
||||
except Exception as exc:
|
||||
try:
|
||||
analyser._log_kv("超时", "检查30分钟超时时出错", error=str(exc))
|
||||
except Exception:
|
||||
pass
|
||||
return False
|
||||
|
||||
|
||||
def try_skip_stage_diagnosis(analyser, start_time: float) -> bool:
|
||||
"""run_once_basic 步骤 0.5:检查是否需要跳过阶段检测,直接查找福袋(点击参与抽奖后)。
|
||||
|
||||
|
||||
Reference in new issue
Block a user