fix: TikTok 立即上传允许卡住的 uploading 任务强制重试

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
eric 2026-08-14 10:48:35 -05:00
parent d0bbcb484a
commit 9b9fb57491
6 changed files with 46 additions and 21 deletions

View File

@ -424,14 +424,24 @@ def claim_tiktok_job_manual_upload(job_id: str) -> dict[str, Any] | None:
if not row:
return None
st = str(row["status"] or "")
if st not in {"scheduled", "recorded"} or not str(row["clip_path"] or "").strip():
if st not in {"scheduled", "recorded", "uploading", "failed"}:
return None
if not str(row["clip_path"] or "").strip():
return None
if str(row["tiktok_video_id"] or "").strip():
return None
conn.execute(
"UPDATE tiktok_jobs SET status = 'uploading', upload_at = ?, updated_at = ? WHERE id = ?",
"""
UPDATE tiktok_jobs
SET status = 'uploading', upload_at = ?, attempts = attempts + 1,
error = '', finished_at = 0, updated_at = ?
WHERE id = ? AND tiktok_video_id = ''
""",
(now, now, job_id),
)
if conn.total_changes == 0:
conn.rollback()
return None
conn.commit()
return _row_to_dict(conn.execute("SELECT * FROM tiktok_jobs WHERE id = ?", (job_id,)).fetchone())
finally:

View File

@ -169,7 +169,7 @@ class TikTokPipeline:
async def upload_job_now(self, job_id: str) -> dict[str, Any]:
job = tiktok_db.claim_tiktok_job_manual_upload(job_id)
if not job:
raise ValueError("任务不可立即上传(需 scheduled/recorded 且有成片)")
raise ValueError("任务不可立即上传(需要成片且未发布;上传中卡住的可强制重试)")
await self._do_upload(job)
fresh = tiktok_db.get_tiktok_job(job_id)
if not fresh:
@ -207,7 +207,7 @@ class TikTokPipeline:
error="",
)
try:
tiktok_db.reclaim_stale_tiktok_uploads(max_age_sec=900)
tiktok_db.reclaim_stale_tiktok_uploads(max_age_sec=180)
except Exception:
pass
max_up = max(1, int(cfg.get("tiktok_max_concurrent") or 1))

View File

@ -113,7 +113,10 @@ def schedule_board(limit: int = 24) -> dict[str, Any]:
"channel_name": ch.get("name") or "",
"status": j.get("status"),
"clip_ready": bool(clip) and Path(clip).is_file(),
"can_upload_now": str(j.get("status") or "") in {"scheduled", "recorded"},
"can_upload_now": (
str(j.get("status") or "") in {"scheduled", "recorded", "uploading", "failed"}
and bool(str(j.get("clip_path") or "").strip())
),
}
)
channel_rows = []

View File

@ -98,14 +98,20 @@ async def upload_tiktok_mp4(
caption = (title or "").strip()
if description.strip() and description.strip() not in caption:
caption = f"{caption}\n{description.strip()}"[:2200]
return await asyncio.to_thread(
_upload_sync,
channel_pk=channel_pk,
file_path=file_path,
title=caption,
privacy_level=privacy_level,
allow_comment=0 if disable_comment else 1,
allow_duet=0 if disable_duet else 1,
allow_stitch=0 if disable_stitch else 1,
proxy=proxy,
)
try:
return await asyncio.wait_for(
asyncio.to_thread(
_upload_sync,
channel_pk=channel_pk,
file_path=file_path,
title=caption,
privacy_level=privacy_level,
allow_comment=0 if disable_comment else 1,
allow_duet=0 if disable_duet else 1,
allow_stitch=0 if disable_stitch else 1,
proxy=proxy,
),
timeout=180,
)
except TimeoutError as exc:
raise ValueError("TikTok 上传超时(180s),Cookie 可能失效或签名服务卡住,请重试") from exc

View File

@ -14,7 +14,13 @@ def test_save_and_load_session_cookies(temp_db, tmp_path, monkeypatch):
assert "tt-target-idc" in names
def test_channel_has_session_false_when_missing(temp_db, tmp_path, monkeypatch):
monkeypatch.setattr(tiktok_session, "cookies_dir", lambda: tmp_path)
ch = tiktok_db.create_tiktok_channel(name="tt2")
assert not tiktok_session.channel_has_session(ch)
def test_manual_upload_allows_stuck_uploading(temp_db, tmp_path):
ch = tiktok_db.create_tiktok_channel(name="tt3")
job = tiktok_db.create_tiktok_job(session_id="", room_id="r1", channel_pk=ch["id"])
clip = tmp_path / "a.mp4"
clip.write_bytes(b"x")
tiktok_db.update_tiktok_job(job["id"], status="uploading", clip_path=str(clip))
claimed = tiktok_db.claim_tiktok_job_manual_upload(job["id"])
assert claimed is not None
assert claimed["status"] == "uploading"
assert claimed["clip_path"] == str(clip)

View File

@ -801,7 +801,7 @@ export default function TikTokPage() {
onClick={() => void uploadNow(j.id)}
className="rounded bg-teal-500/15 px-2 py-0.5 text-[10px] text-teal-200"
>
上传
{j.status === "uploading" ? "重试上传" : "上传"}
</button>
)}
{j.status === "failed" && (