⛔ 游戏启动改成会重试 —— 02:03 那轮 "exe 起来就退了", 白丢一轮

Browse Source
现象: 02:03 计划任务跑了 241s, 报"游戏启动超时"。证据:
  * Steam 的 content_log 里 02:0x **毫无记录**(上一轮 22:03:43 还有 "App Running",
    13 分钟后我手动启动也是 5 秒就起来)
  * 应用事件日志 02:00~02:12 没有任何错误
  * 游戏进程始终没出现
说明 Popen 返回了, 但 exe 起来就退, 连 Steam 都没碰上。

原来的写法是"点一次火, 干等 240 秒"。碰上一次静默失败就白丢一整轮(这轮的体力白攒)。
改成试 3 次、每次等 timeout/3 秒, 中间自然间隔:
  * 大多数瞬时故障靠重试就能救回来
  * 日志里先记一行 Steam 进程数, 便于下次判断是不是 Steam 侧的问题

验证: 5 个用例(需要假时钟 —— sleep 打桩成空函数后墙钟几乎不走, 内层循环会狂转,
不控时钟就测不出"一次等满 40 秒"这件事):
  * 首次检查已在运行 -> 不点火
  * 第一次尝试内成功 -> 只点火 1 次
  * 第一次静默失败、第二次成功 -> 能救回来, 点火 >1 次
  * 三次全失败 -> 放弃, 正好点火 3 次
  * 总等待 = 3 x 40 = 120 秒(不再把 240 秒全花在第一次上)

全套 340/340 通过。
This commit is contained in:
Codex Agent
2026-10-17 02:26:10 +08:00
parent 1712254b9a
commit b61cd17852
2 changed files with 112 additions and 10 deletions
+34 -10
View File
@@ -364,22 +364,46 @@ def ensure_window_size(width, height, find=None, client=None, resize=None):
return after return after
def launch_game(timeout_s=240): def launch_game(timeout_s=240, tries=3):
"""启动游戏, 分几次尝试。
⛔ 2026-10-17 02:03 实测: 那一轮 exe 起来就退了 —— Steam 的 content_log 里
02:0x 毫无记录(上一轮 22:03:43 有 "App Running"), 应用事件日志也没有报错,
游戏进程始终没出现。而 13 分钟后我手动启动, 5 秒就起来了。
时间线上, 01:49 刚发生过一次会话断开重连, Steam 的 webhelper 直到 02:16 才恢复活动。
原来的写法是"点一次火, 干等 240 秒", 碰上一次静默失败就白丢一整轮(这轮体力白攒)。
改成多次尝试 —— 中间隔一会儿再来一次, 大多数瞬时故障都能靠这个救回来。
"""
if game_running(): if game_running():
log('游戏已在运行') log('游戏已在运行')
return True return True
log('启动游戏: %s' % GAME_EXE) log('Steam 进程数: %d' % len([p for p in _popen_psutil('steam')]))
subprocess.Popen([GAME_EXE], close_fds=True) each = max(40, timeout_s // max(1, tries))
t0 = time.time() for i in range(tries):
while time.time() - t0 < timeout_s: log('启动游戏(第 %d/%d 次, 每次等 %ds): %s' % (i + 1, tries, each, GAME_EXE))
time.sleep(5) subprocess.Popen([GAME_EXE], close_fds=True)
if game_running(): t0 = time.time()
log('游戏进程已出现 (%.0fs)' % (time.time() - t0)) while time.time() - t0 < each:
return True time.sleep(5)
log('游戏启动超时') if game_running():
log('游戏进程已出现 (%.0fs)' % (time.time() - t0))
return True
log(' 这一次没等到游戏进程')
log('游戏启动超时(试了 %d 次)' % tries)
return False return False
def _popen_psutil(name):
"""按进程名查进程(只用于日志, 失败返回空)。"""
try:
out = subprocess.run(['tasklist', '/FI', 'IMAGENAME eq %s.exe' % name],
capture_output=True, text=True, errors='ignore').stdout
return [l for l in out.splitlines() if l.lower().startswith(name.lower())]
except OSError:
return []
def close_game(): def close_game():
subprocess.run(['taskkill', '/IM', '%s.exe' % GAME_PROC, '/F'], subprocess.run(['taskkill', '/IM', '%s.exe' % GAME_PROC, '/F'],
capture_output=True, text=True, errors='ignore') capture_output=True, text=True, errors='ignore')