From 9b9fb57491a86520acbdcc5e2ac60caad4ccb272 Mon Sep 17 00:00:00 2001 From: eric Date: Fri, 14 Aug 2026 10:48:35 -0500 Subject: [PATCH] =?UTF-8?q?fix:=20TikTok=20=E7=AB=8B=E5=8D=B3=E4=B8=8A?= =?UTF-8?q?=E4=BC=A0=E5=85=81=E8=AE=B8=E5=8D=A1=E4=BD=8F=E7=9A=84=20upload?= =?UTF-8?q?ing=20=E4=BB=BB=E5=8A=A1=E5=BC=BA=E5=88=B6=E9=87=8D=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Cursor --- backend/app/tiktok_db.py | 14 ++++++++++++-- backend/app/tiktok_pipeline.py | 4 ++-- backend/app/tiktok_schedule.py | 5 ++++- backend/app/tiktok_upload.py | 28 +++++++++++++++++----------- backend/tests/test_tiktok_session.py | 14 ++++++++++---- frontend/src/app/tiktok/page.tsx | 2 +- 6 files changed, 46 insertions(+), 21 deletions(-) diff --git a/backend/app/tiktok_db.py b/backend/app/tiktok_db.py index 756ef5f..a426ffb 100644 --- a/backend/app/tiktok_db.py +++ b/backend/app/tiktok_db.py @@ -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: diff --git a/backend/app/tiktok_pipeline.py b/backend/app/tiktok_pipeline.py index 10ebd32..8416441 100644 --- a/backend/app/tiktok_pipeline.py +++ b/backend/app/tiktok_pipeline.py @@ -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)) diff --git a/backend/app/tiktok_schedule.py b/backend/app/tiktok_schedule.py index de98cf3..19d6d6c 100644 --- a/backend/app/tiktok_schedule.py +++ b/backend/app/tiktok_schedule.py @@ -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 = [] diff --git a/backend/app/tiktok_upload.py b/backend/app/tiktok_upload.py index 56282fc..3361349 100644 --- a/backend/app/tiktok_upload.py +++ b/backend/app/tiktok_upload.py @@ -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 diff --git a/backend/tests/test_tiktok_session.py b/backend/tests/test_tiktok_session.py index ebabfac..0cd4bb7 100644 --- a/backend/tests/test_tiktok_session.py +++ b/backend/tests/test_tiktok_session.py @@ -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) diff --git a/frontend/src/app/tiktok/page.tsx b/frontend/src/app/tiktok/page.tsx index 58fb566..2385352 100644 --- a/frontend/src/app/tiktok/page.tsx +++ b/frontend/src/app/tiktok/page.tsx @@ -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" ? "重试上传" : "上传"} )} {j.status === "failed" && (