From 052c49714f2aa59ab7c87030a92d853ee5860f21 Mon Sep 17 00:00:00 2001 From: eric Date: Fri, 21 Aug 2026 07:57:52 -0500 Subject: [PATCH] fix(yt-long): reclaim cascade-only jobs into pending so catch-up actually records Co-authored-by: Cursor --- backend/app/yt_long_db.py | 83 ++++++++++++++++++++++++++++----- backend/app/yt_long_pipeline.py | 15 +++++- 2 files changed, 84 insertions(+), 14 deletions(-) diff --git a/backend/app/yt_long_db.py b/backend/app/yt_long_db.py index 89a9c77..1f80c40 100644 --- a/backend/app/yt_long_db.py +++ b/backend/app/yt_long_db.py @@ -291,7 +291,12 @@ def yt_long_channel_daily_max(cfg: dict[str, Any] | None = None) -> int: def room_blocks_yt_long_sample(room_id: str, push_date: str, *, need_upload_slots: bool = False) -> bool: - """Room-day dedupe. If upload slots remain, cascade-only dead-ends may be re-recorded.""" + """Room-day dedupe. + + When YouTube upload slots remain, only block rooms that already have an + in-flight job — finished cascade-only / uploaded rooms may be re-sampled + so we can catch up channel daily targets with few live sources. + """ rid = str(room_id or "").strip() day = push_date or push_date_from_ts(time.time()) if not rid: @@ -300,26 +305,22 @@ def room_blocks_yt_long_sample(room_id: str, push_date: str, *, need_upload_slot conn = _connect() try: ensure_yt_long_tables(conn) - if not need_upload_slots: + if need_upload_slots: row = conn.execute( """ SELECT 1 AS o FROM yt_long_jobs WHERE room_id = ? AND push_date = ? - AND status NOT IN ('failed', 'skipped') + AND status IN ('pending', 'recording', 'scheduled', 'uploading') LIMIT 1 """, (rid, day), ).fetchone() return bool(row) - # Still need YouTube uploads: only block if room already has a real upload pipeline job row = conn.execute( """ SELECT 1 AS o FROM yt_long_jobs WHERE room_id = ? AND push_date = ? - AND ( - status IN ('pending', 'recording', 'scheduled', 'uploading', 'uploaded') - OR (status = 'recorded' AND IFNULL(clip_path, '') != '' AND IFNULL(error, '') NOT LIKE 'cascade_only%') - ) + AND status NOT IN ('failed', 'skipped') LIMIT 1 """, (rid, day), @@ -473,19 +474,77 @@ def create_yt_long_job( ensure_yt_long_tables(conn) rid = str(room_id or "").strip() if rid: - exists = conn.execute( + # Only block while another job for this room is actively running. + inflight = conn.execute( """ SELECT id FROM yt_long_jobs WHERE room_id = ? AND push_date = ? - AND status NOT IN ('failed', 'skipped') + AND status IN ('pending', 'recording', 'scheduled', 'uploading') + ORDER BY created_at DESC LIMIT 1 """, (rid, day), ).fetchone() - if exists: + if inflight: return _row_to_dict( - conn.execute("SELECT * FROM yt_long_jobs WHERE id = ?", (exists["id"],)).fetchone() + conn.execute("SELECT * FROM yt_long_jobs WHERE id = ?", (inflight["id"],)).fetchone() ) # type: ignore + + # Prefer reclaiming a cascade-only dead-end (no clip) over inserting a new row + dead = conn.execute( + """ + SELECT id FROM yt_long_jobs + WHERE room_id = ? AND push_date = ? + AND status = 'recorded' + AND IFNULL(error, '') LIKE 'cascade_only%' + AND IFNULL(youtube_video_id, '') = '' + ORDER BY created_at DESC + LIMIT 1 + """, + (rid, day), + ).fetchone() + if dead: + conn.execute( + """ + UPDATE yt_long_jobs SET + status = 'pending', + error = '', + channel_pk = CASE + WHEN ? != '' THEN ? + ELSE channel_pk + END, + stream_url = CASE WHEN ? != '' THEN ? ELSE stream_url END, + source_url = CASE WHEN ? != '' THEN ? ELSE source_url END, + anchor_name = CASE WHEN ? != '' THEN ? ELSE anchor_name END, + room_pk = CASE WHEN ? != '' THEN ? ELSE room_pk END, + clip_path = '', + clip_seconds = 0, + upload_at = 0, + started_at = 0, + finished_at = 0, + updated_at = ? + WHERE id = ? + """, + ( + str(channel_pk or ""), + str(channel_pk or ""), + str(stream_url or ""), + str(stream_url or ""), + str(source_url or ""), + str(source_url or ""), + str(anchor_name or ""), + str(anchor_name or ""), + str(room_pk or ""), + str(room_pk or ""), + now, + str(dead["id"]), + ), + ) + conn.commit() + return _row_to_dict( + conn.execute("SELECT * FROM yt_long_jobs WHERE id = ?", (dead["id"],)).fetchone() + ) # type: ignore + conn.execute( """ INSERT INTO yt_long_jobs( diff --git a/backend/app/yt_long_pipeline.py b/backend/app/yt_long_pipeline.py index a38a096..d340b7a 100644 --- a/backend/app/yt_long_pipeline.py +++ b/backend/app/yt_long_pipeline.py @@ -203,12 +203,15 @@ def _enqueue_from_room(room: dict[str, Any], cfg: dict[str, Any], push_date: str channel_pk=str((channel or {}).get("id") or ""), push_date=push_date, ) + # create() may return an already-finished job; only count real pending work + if str(created.get("status") or "") != "pending": + return "" + jid = str(created.get("id") or "") if channel: try: yt_long_db.update_yt_long_channel(str(channel["id"]), last_assigned_at=time.time()) except Exception: pass - jid = str(created.get("id") or "") if jid: _emit("yt_long_ops", "info", f"长视频取样入队:{created.get('anchor_name') or rid}", created) return jid @@ -336,9 +339,15 @@ def try_fill_daily_quota() -> dict[str, Any]: if gap <= 0: return {"enqueued": 0, "gap": 0, "reason": "quota_full"} patched = {**settings, "yt_long_live_sample_batch": min(gap, 8), "yt_long_live_sample": True} + # Prefer filling YouTube upload gap when source still has room + upload_gap = yt_long_db.yt_long_upload_gap(cfg=settings) + if upload_gap > 0: + patched["yt_long_live_sample_batch"] = max( + int(patched["yt_long_live_sample_batch"]), min(8, upload_gap) + ) total = 0 rounds = 0 - while total < gap and rounds < 12: + while total < max(gap, upload_gap) and rounds < 12: rounds += 1 n = try_sample_live_rooms(patched) if n <= 0: @@ -348,7 +357,9 @@ def try_fill_daily_quota() -> dict[str, Any]: "enqueued": total, "gap": yt_long_db.yt_long_source_gap(cfg=settings), "upload_gap": yt_long_db.yt_long_upload_gap(cfg=settings), + "pending": len(yt_long_db.list_pending_yt_long_jobs(limit=50)), "reason": "ok" if total else "no_candidates", + "reason_zh": "已重新入队待录" if total else "当前没有可取样的在播源", }