fix(yt-long): count pending in quota, reclaim orphan recordings, Chinese sample reasons
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
4bb424b73e
commit
89e4c664a6
@ -11,7 +11,14 @@ from .db import _connect, _lock, _row_to_dict
|
||||
from .timeutil import push_date_from_ts
|
||||
|
||||
# Quota-consuming statuses for daily long-form caps
|
||||
_QUOTA_STATUSES = ("scheduled", "uploading", "uploaded")
|
||||
_QUOTA_STATUSES = (
|
||||
"pending",
|
||||
"recording",
|
||||
"recorded",
|
||||
"scheduled",
|
||||
"uploading",
|
||||
"uploaded",
|
||||
)
|
||||
|
||||
YT_LONG_DAILY_DEFAULT = 3
|
||||
YT_LONG_MIN_SEC = 180 # 3 min
|
||||
@ -395,8 +402,31 @@ def claim_due_yt_long_upload() -> dict[str, Any] | None:
|
||||
conn.close()
|
||||
|
||||
|
||||
def reclaim_stale_yt_long_recordings(*, max_age_sec: float = 900) -> int:
|
||||
cutoff = time.time() - max(120.0, float(max_age_sec))
|
||||
def count_yt_long_jobs_by_status(status: str, *, push_date: str = "") -> int:
|
||||
day = push_date or ""
|
||||
with _lock:
|
||||
conn = _connect()
|
||||
try:
|
||||
ensure_yt_long_tables(conn)
|
||||
if day:
|
||||
row = conn.execute(
|
||||
"SELECT COUNT(*) AS c FROM yt_long_jobs WHERE status = ? AND push_date = ?",
|
||||
(status, day),
|
||||
).fetchone()
|
||||
else:
|
||||
row = conn.execute(
|
||||
"SELECT COUNT(*) AS c FROM yt_long_jobs WHERE status = ?",
|
||||
(status,),
|
||||
).fetchone()
|
||||
return int(row["c"]) if row else 0
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
|
||||
def reclaim_stale_yt_long_recordings(*, max_age_sec: float = 600) -> int:
|
||||
"""Reclaim orphan recording rows (no worker). Use started_at when present."""
|
||||
age = max(180.0, float(max_age_sec))
|
||||
cutoff = time.time() - age
|
||||
with _lock:
|
||||
conn = _connect()
|
||||
try:
|
||||
@ -405,9 +435,13 @@ def reclaim_stale_yt_long_recordings(*, max_age_sec: float = 900) -> int:
|
||||
"""
|
||||
UPDATE yt_long_jobs
|
||||
SET status = 'pending', started_at = 0, error = 'recording_stale_reclaim', updated_at = ?
|
||||
WHERE status = 'recording' AND updated_at < ?
|
||||
WHERE status = 'recording'
|
||||
AND (
|
||||
(IFNULL(started_at, 0) > 0 AND started_at < ?)
|
||||
OR (IFNULL(started_at, 0) <= 0 AND updated_at < ?)
|
||||
)
|
||||
""",
|
||||
(time.time(), cutoff),
|
||||
(time.time(), cutoff, cutoff),
|
||||
)
|
||||
conn.commit()
|
||||
return int(cur.rowcount or 0)
|
||||
|
||||
@ -288,12 +288,23 @@ def sample_live_now() -> dict[str, Any]:
|
||||
reason = "no_live"
|
||||
else:
|
||||
reason = "enqueue_failed"
|
||||
reason_zh = {
|
||||
"ok": "已入队",
|
||||
"disabled": "模块未启用",
|
||||
"no_quota": "今日全局配额已满",
|
||||
"no_channel": "没有可用已授权频道",
|
||||
"all_deduped": "今日在播房间均已入队,请等待排队任务录制",
|
||||
"no_live": "当前没有抖音在播源",
|
||||
"enqueue_failed": "入队失败",
|
||||
}.get(reason, reason)
|
||||
return {
|
||||
"enqueued": n,
|
||||
"live_count": len(rooms),
|
||||
"skipped_dedupe": skipped,
|
||||
"pending_jobs": len(pending),
|
||||
"job_ids": [str(j.get("id")) for j in pending],
|
||||
"reason": reason,
|
||||
"reason_zh": reason_zh,
|
||||
}
|
||||
|
||||
|
||||
@ -396,13 +407,21 @@ class YtLongPipeline:
|
||||
async def kick_pending_records(self, ids: list[str] | None = None) -> int:
|
||||
cfg = db.get_settings_map()
|
||||
max_rec = max(1, int(cfg.get("yt_long_max_concurrent") or 1))
|
||||
try:
|
||||
yt_long_db.reclaim_stale_yt_long_recordings(max_age_sec=180)
|
||||
except Exception:
|
||||
pass
|
||||
db_recording = yt_long_db.count_yt_long_jobs_by_status("recording")
|
||||
slots = max(0, max_rec - max(self._active_records, db_recording))
|
||||
if slots <= 0:
|
||||
return 0
|
||||
pending = yt_long_db.list_pending_yt_long_jobs(limit=20)
|
||||
if ids:
|
||||
want = {str(x) for x in ids}
|
||||
pending = [j for j in pending if str(j.get("id") or "") in want]
|
||||
n = 0
|
||||
for job in pending:
|
||||
if self._active_records >= max_rec:
|
||||
if n >= slots or self._active_records >= max_rec:
|
||||
break
|
||||
claimed = yt_long_db.claim_yt_long_job_record(str(job.get("id") or ""))
|
||||
if not claimed:
|
||||
@ -426,7 +445,9 @@ class YtLongPipeline:
|
||||
if n:
|
||||
self.sampled += n
|
||||
try:
|
||||
yt_long_db.reclaim_stale_yt_long_recordings(max_age_sec=900)
|
||||
# Long clips are 3–8 min; reclaim orphans after ~10 min wall clock
|
||||
max_clip = int(cfg.get("yt_long_max_sec") or YT_LONG_MAX_SEC)
|
||||
yt_long_db.reclaim_stale_yt_long_recordings(max_age_sec=max(600, max_clip + 120))
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
@ -435,7 +456,15 @@ class YtLongPipeline:
|
||||
pass
|
||||
|
||||
max_rec = max(1, int(cfg.get("yt_long_max_concurrent") or 1))
|
||||
while self._active_records < max_rec:
|
||||
db_recording = yt_long_db.count_yt_long_jobs_by_status("recording")
|
||||
# If DB has more recording rows than live tasks, they are orphans — force reclaim
|
||||
if db_recording > self._active_records:
|
||||
try:
|
||||
yt_long_db.reclaim_stale_yt_long_recordings(max_age_sec=90)
|
||||
db_recording = yt_long_db.count_yt_long_jobs_by_status("recording")
|
||||
except Exception:
|
||||
pass
|
||||
while self._active_records < max_rec and db_recording < max_rec:
|
||||
pending = yt_long_db.list_pending_yt_long_jobs(limit=1)
|
||||
if not pending:
|
||||
break
|
||||
@ -443,6 +472,7 @@ class YtLongPipeline:
|
||||
if not job:
|
||||
break
|
||||
self._active_records += 1
|
||||
db_recording += 1
|
||||
asyncio.create_task(self._run_record(job))
|
||||
|
||||
if cfg.get("yt_long_auto_upload", True):
|
||||
|
||||
@ -411,7 +411,7 @@ export default function YtLongPage() {
|
||||
`${totals.used} / ${totals.cap || status?.daily_max || "—"}`,
|
||||
beijingWindow ? `${beijingWindow}(北京时间)` : "按各频道日上限合计",
|
||||
],
|
||||
["当日缺口", String(totals.gap || quotaGap), `全局上限 ${status?.daily_max ?? "—"}`],
|
||||
["当日缺口", String(quotaGap), `全局上限 ${status?.daily_max ?? "—"} · 频道合计缺口 ${totals.gap}`],
|
||||
["已上传", String(totals.uploaded), `排队 ${totals.scheduled}`],
|
||||
[
|
||||
"抖音在播源",
|
||||
@ -488,7 +488,7 @@ export default function YtLongPage() {
|
||||
const n = Number(r.enqueued ?? 0);
|
||||
const live = Number(r.live_count ?? 0);
|
||||
if (n > 0) setMsg(`正在从 ${live} 路在播中录制 ${n} 条长视频`);
|
||||
else setError(String(r.reason || "没有可取样的在播直播间"));
|
||||
else setError(String(r.reason_zh || r.reason || "没有可取样的在播直播间"));
|
||||
await load();
|
||||
} catch (e) {
|
||||
setError((e as Error).message);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user