diff --git a/douyin/core/douyin_fudai.py b/douyin/core/douyin_fudai.py index e1dce9b..dc2bda0 100644 --- a/douyin/core/douyin_fudai.py +++ b/douyin/core/douyin_fudai.py @@ -381,6 +381,7 @@ class fudai_analyse: from douyin.core.fudai_modules.fudai_choujiang_helpers import ( cleanup_stale_screenshot, resolve_device_resolution, + run_loop_with_timeout, write_actions_log, ) @@ -410,20 +411,6 @@ class fudai_analyse: resolve_device_resolution(self) write_actions_log("程序开始运行", __file__) - # 捕获并记录未被捕获的异常以便排查立即退出问题 - try: - # 原有后续逻辑继续... - pass - except Exception as top_exc: - try: - self._log_kv( - "ERROR", "fudai_choujiang top-level exception", error=str(top_exc) - ) - except Exception: - import traceback - - traceback.print_exc() - raise # 暂时禁用流式截图,使用直接截图模式 self._log_kv("截图服务", "暂时禁用流式截图,使用直接截图模式") @@ -436,74 +423,8 @@ class fudai_analyse: wait_minutes=int(wait_minutes), ) - # 基础循环:在限定时间内反复执行单轮逻辑 - # 如果 wait_minutes <= 0,则无限循环运行 - if wait_minutes and wait_minutes > 0: - end_ts = time.time() + int(wait_minutes) * 60 - while time.time() < end_ts: - try: - self.run_once_basic() - except Exception as exc: - # 捕获异常,记录日志,但继续循环 - try: - self._log_kv( - "循环", - "run_once_basic 异常", - error=str(exc), - error_type=type(exc).__name__, - ) - except Exception: - pass - # 异常后稍作延迟再继续 - try: - self.operation.delay(2.0) - except Exception: - time.sleep(2.0) - continue - - # 为避免过于频繁截图,增加少量间隔 - try: - self.operation.delay(1.0) - except Exception: - time.sleep(1.0) - - # 记录运行结束(定时模式) - write_actions_log("程序运行结束(时间到)", __file__) - - else: - # 无限循环模式(wait_minutes <= 0) - self._log_kv("循环", "进入无限循环模式(wait_minutes <= 0)") - while True: - try: - self.run_once_basic() - except KeyboardInterrupt: - # 允许 Ctrl+C 退出 - self._log_kv("循环", "收到中断信号,退出循环") - write_actions_log("程序运行结束(中断退出)", __file__) - raise - except Exception as exc: - # 捕获异常,记录日志,但继续循环 - try: - self._log_kv( - "循环", - "run_once_basic 异常", - error=str(exc), - error_type=type(exc).__name__, - ) - except Exception: - pass - # 异常后稍作延迟再继续 - try: - self.operation.delay(2.0) - except Exception: - time.sleep(2.0) - continue - - # 为避免过于频繁截图,增加少量间隔 - try: - self.operation.delay(1.0) - except Exception: - time.sleep(1.0) + # 主循环(定时模式 / 无限循环) + run_loop_with_timeout(self, wait_minutes) # ========== 阶段诊断 ========== def _diagnose_stage_from_current_screen( diff --git a/douyin/core/fudai_modules/fudai_choujiang_helpers.py b/douyin/core/fudai_modules/fudai_choujiang_helpers.py index e9aa695..03ac3b1 100644 --- a/douyin/core/fudai_modules/fudai_choujiang_helpers.py +++ b/douyin/core/fudai_modules/fudai_choujiang_helpers.py @@ -4,6 +4,7 @@ - cleanup_stale_screenshot:清理启动时残留的旧截图 - resolve_device_resolution:解析设备分辨率(带重试和默认回退) - write_actions_log:写运行起止标记到 actions.log +- run_loop_with_timeout:fudai_choujiang 的主循环(定时/无限) """ from __future__ import annotations @@ -12,6 +13,9 @@ import os import time from datetime import datetime +# 用于 write_actions_log 的 log_dir_anchor:从 helper 文件位置回溯到 douyin_fudai.py 所在目录 +_ANCHOR = os.path.join(os.path.dirname(os.path.dirname(__file__)), "douyin_fudai.py") + def cleanup_stale_screenshot(analyser) -> None: """启动时清理旧截图文件,避免复用过期截图。""" @@ -74,6 +78,72 @@ def resolve_device_resolution(analyser) -> None: analyser.resolution_ratio_x, analyser.resolution_ratio_y = 1080, 2400 +def run_loop_with_timeout(analyser, wait_minutes: int) -> None: + """fudai_choujiang 主循环:定时模式 vs 无限循环模式。 + + 定时模式(wait_minutes > 0):在 wait_minutes 分钟内反复跑 run_once_basic。 + 无限模式(wait_minutes <= 0):直到 Ctrl+C。 + + 异常时延迟 2s 后继续,每轮之间延迟 1s 避免频繁截图。 + """ + if wait_minutes and wait_minutes > 0: + end_ts = time.time() + int(wait_minutes) * 60 + while time.time() < end_ts: + try: + analyser.run_once_basic() + except Exception as exc: + try: + analyser._log_kv( + "循环", + "run_once_basic 异常", + error=str(exc), + error_type=type(exc).__name__, + ) + except Exception: + pass + try: + analyser.operation.delay(2.0) + except Exception: + time.sleep(2.0) + continue + + try: + analyser.operation.delay(1.0) + except Exception: + time.sleep(1.0) + + write_actions_log("程序运行结束(时间到)", _ANCHOR) + else: + analyser._log_kv("循环", "进入无限循环模式(wait_minutes <= 0)") + while True: + try: + analyser.run_once_basic() + except KeyboardInterrupt: + analyser._log_kv("循环", "收到中断信号,退出循环") + write_actions_log("程序运行结束(中断退出)", _ANCHOR) + raise + except Exception as exc: + try: + analyser._log_kv( + "循环", + "run_once_basic 异常", + error=str(exc), + error_type=type(exc).__name__, + ) + except Exception: + pass + try: + analyser.operation.delay(2.0) + except Exception: + time.sleep(2.0) + continue + + try: + analyser.operation.delay(1.0) + except Exception: + time.sleep(1.0) + + def write_actions_log(message: str, log_dir_anchor: str) -> None: """写一条带时间戳的标记到 logs/actions.log。