Ver Fonte

feat: 发布中僵死任务自愈+测试补齐

- run_dispatcher 启动时回收孤儿「发布中」行(liveTime早于30分钟,
  健康发布≤15分钟,超时必为进程被杀遗留)→重新排队接手
- 真机验证:僵死的3xnhxa8jmy4g8k2复位后32秒发布完成(今日第6条)
- 测试:僵尸回收用例(补cookie)、irange类型进字段清单/往返用例;133全绿
wubinggen há 3 meses atrás
pai
commit
834d753ff4

+ 10 - 0
白盒重建/kuaishou_tool/publish.py

@@ -558,6 +558,16 @@ def run_dispatcher(cfg, max_rounds=None, stop_event=None):
     max_rounds=None 为无限循环(生产);传整数仅供测试限制轮数;
     stop_event(可选,threading.Event)置位即退出(空转等待可被立即打断)。"""
     logger.info('定时发布调度线程启动')
+    # 自愈:回收僵死的「发布中」行(进程被杀后无人回写;健康发布≤15分钟完成,
+    # liveTime 早于30分钟前的发布中行必为孤儿)→ 重新排队由本线程接手。
+    _stale = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time() - 1800))
+    _zombies = db.query("SELECT videoId FROM publishvideo WHERE state='发布中' "
+                        'AND liveTime<=?', (_stale,))
+    if _zombies:
+        for z in _zombies:
+            db.exe("UPDATE publishvideo SET state='排队' WHERE videoId=?", (z['videoId'],))
+        logger.warning('回收僵死发布中任务 %d 条: %s', len(_zombies),
+                       ','.join(z['videoId'] for z in _zombies))
     rounds = 0
     while max_rounds is None or rounds < max_rounds:
         if stop_event is not None and stop_event.is_set():

+ 20 - 0
白盒重建/tests/test_publish.py

@@ -669,6 +669,26 @@ def test_run_dispatcher_missing_video_marks_fail():
     assert st and st[0]['state'] == '失败'
 
 
+def test_run_dispatcher_reclaims_stale_publishing():
+    """自愈:进程被杀遗留的「发布中」孤儿行(liveTime早于30分钟)被回收重发。"""
+    clean_tables()
+    d = config.dir_of(CFG, '发布中视频')
+    v = d / 'disp_zombie.mp4'
+    v.write_bytes(b'z' * 512)
+    stale = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time() - 3600))
+    db.exe("INSERT INTO publishvideo (videoId, state, liveTime, cookie) "
+           "VALUES (?,?,?,'ck_disp')",
+           ('disp_zombie', '发布中', stale))          # 僵尸:发布中且早已过期
+    fc = FakeClient()
+    fc.script = ok_script()
+    with patch(publish, 'KSClient', lambda **kw: fc), \
+            patch(publish, 'rand_sleep', lambda a, b: None):
+        run_dispatcher_rounds(1)
+    st = db.query("SELECT state FROM publishvideo WHERE videoId='disp_zombie'")[0]
+    assert st['state'] == '完成'                      # 被回收并成功发布
+    v.unlink()
+
+
 def test_run_dispatcher_uses_queue_proxy():
     # 队列行代理传入 KSClient(F3:带代理参数重新执行发布)
     clean_tables()

+ 2 - 2
白盒重建/tests/test_ui.py

@@ -99,7 +99,7 @@ def test_settings_fields_sane():
     paths = [p for _g, p, _l, _t, _d in ui.SETTINGS_FIELDS]
     assert len(paths) == len(set(paths))          # 键路径唯一
     types = {_t for _g, _p, _l, _t, _d in ui.SETTINGS_FIELDS}
-    assert types <= {'int', 'float', 'str', 'bool', 'list'}
+    assert types <= {'int', 'float', 'str', 'bool', 'list', 'irange'}
 
 
 def test_form_roundtrip():
@@ -108,7 +108,7 @@ def test_form_roundtrip():
     expect = {}
     for _g, path, _l, typ, default in ui.SETTINGS_FIELDS:
         expect[path] = {'int': 7, 'float': 1.5, 'str': 'x', 'bool': True,
-                        'list': ['a', 'b']}[typ]
+                        'list': ['a', 'b'], 'irange': [30, 180]}[typ]
         ui._bury(cfg, path, expect[path])
     vals = ui.form_values(cfg)
     out = {}