fix(yt-long): reclaim cascade-only jobs into pending so catch-up actually records

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
eric 2026-08-21 07:57:52 -05:00
parent ada999751e
commit 052c49714f
2 changed files with 84 additions and 14 deletions

View File

@ -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: 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() rid = str(room_id or "").strip()
day = push_date or push_date_from_ts(time.time()) day = push_date or push_date_from_ts(time.time())
if not rid: if not rid:
@ -300,26 +305,22 @@ def room_blocks_yt_long_sample(room_id: str, push_date: str, *, need_upload_slot
conn = _connect() conn = _connect()
try: try:
ensure_yt_long_tables(conn) ensure_yt_long_tables(conn)
if not need_upload_slots: if need_upload_slots:
row = conn.execute( row = conn.execute(
""" """
SELECT 1 AS o FROM yt_long_jobs SELECT 1 AS o FROM yt_long_jobs
WHERE room_id = ? AND push_date = ? WHERE room_id = ? AND push_date = ?
AND status NOT IN ('failed', 'skipped') AND status IN ('pending', 'recording', 'scheduled', 'uploading')
LIMIT 1 LIMIT 1
""", """,
(rid, day), (rid, day),
).fetchone() ).fetchone()
return bool(row) return bool(row)
# Still need YouTube uploads: only block if room already has a real upload pipeline job
row = conn.execute( row = conn.execute(
""" """
SELECT 1 AS o FROM yt_long_jobs SELECT 1 AS o FROM yt_long_jobs
WHERE room_id = ? AND push_date = ? WHERE room_id = ? AND push_date = ?
AND ( AND status NOT IN ('failed', 'skipped')
status IN ('pending', 'recording', 'scheduled', 'uploading', 'uploaded')
OR (status = 'recorded' AND IFNULL(clip_path, '') != '' AND IFNULL(error, '') NOT LIKE 'cascade_only%')
)
LIMIT 1 LIMIT 1
""", """,
(rid, day), (rid, day),
@ -473,19 +474,77 @@ def create_yt_long_job(
ensure_yt_long_tables(conn) ensure_yt_long_tables(conn)
rid = str(room_id or "").strip() rid = str(room_id or "").strip()
if rid: 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 SELECT id FROM yt_long_jobs
WHERE room_id = ? AND push_date = ? 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 LIMIT 1
""", """,
(rid, day), (rid, day),
).fetchone() ).fetchone()
if exists: if inflight:
return _row_to_dict( 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 ) # 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( conn.execute(
""" """
INSERT INTO yt_long_jobs( INSERT INTO yt_long_jobs(

View File

@ -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 ""), channel_pk=str((channel or {}).get("id") or ""),
push_date=push_date, 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: if channel:
try: try:
yt_long_db.update_yt_long_channel(str(channel["id"]), last_assigned_at=time.time()) yt_long_db.update_yt_long_channel(str(channel["id"]), last_assigned_at=time.time())
except Exception: except Exception:
pass pass
jid = str(created.get("id") or "")
if jid: if jid:
_emit("yt_long_ops", "info", f"长视频取样入队:{created.get('anchor_name') or rid}", created) _emit("yt_long_ops", "info", f"长视频取样入队:{created.get('anchor_name') or rid}", created)
return jid return jid
@ -336,9 +339,15 @@ def try_fill_daily_quota() -> dict[str, Any]:
if gap <= 0: if gap <= 0:
return {"enqueued": 0, "gap": 0, "reason": "quota_full"} return {"enqueued": 0, "gap": 0, "reason": "quota_full"}
patched = {**settings, "yt_long_live_sample_batch": min(gap, 8), "yt_long_live_sample": True} 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 total = 0
rounds = 0 rounds = 0
while total < gap and rounds < 12: while total < max(gap, upload_gap) and rounds < 12:
rounds += 1 rounds += 1
n = try_sample_live_rooms(patched) n = try_sample_live_rooms(patched)
if n <= 0: if n <= 0:
@ -348,7 +357,9 @@ def try_fill_daily_quota() -> dict[str, Any]:
"enqueued": total, "enqueued": total,
"gap": yt_long_db.yt_long_source_gap(cfg=settings), "gap": yt_long_db.yt_long_source_gap(cfg=settings),
"upload_gap": yt_long_db.yt_long_upload_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": "ok" if total else "no_candidates",
"reason_zh": "已重新入队待录" if total else "当前没有可取样的在播源",
} }