fix: TikTok 发布成功后正确记为已上传,并禁止并发重复上传
vendor 的 upload_video 成功后没有 return True,被误判失败;同时立即上传会抢正在上传的任务导致重复发布。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
0815e05094
commit
c7a7275229
@ -428,16 +428,23 @@ def claim_tiktok_job_manual_upload(job_id: str) -> dict[str, Any] | None:
|
|||||||
return None
|
return None
|
||||||
if not str(row["clip_path"] or "").strip():
|
if not str(row["clip_path"] or "").strip():
|
||||||
return None
|
return None
|
||||||
if str(row["tiktok_video_id"] or "").strip():
|
if str(row["tiktok_video_id"] or "").strip() or str(row["tiktok_publish_id"] or "").strip():
|
||||||
|
return None
|
||||||
|
stale_before = now - 180
|
||||||
|
if st == "uploading" and float(row["updated_at"] or 0) >= stale_before:
|
||||||
return None
|
return None
|
||||||
conn.execute(
|
conn.execute(
|
||||||
"""
|
"""
|
||||||
UPDATE tiktok_jobs
|
UPDATE tiktok_jobs
|
||||||
SET status = 'uploading', upload_at = ?, attempts = attempts + 1,
|
SET status = 'uploading', upload_at = ?, attempts = attempts + 1,
|
||||||
error = '', finished_at = 0, updated_at = ?
|
error = '', finished_at = 0, updated_at = ?
|
||||||
WHERE id = ? AND tiktok_video_id = ''
|
WHERE id = ? AND IFNULL(tiktok_video_id,'') = '' AND IFNULL(tiktok_publish_id,'') = ''
|
||||||
|
AND (
|
||||||
|
status IN ('scheduled', 'recorded', 'failed')
|
||||||
|
OR (status = 'uploading' AND updated_at < ?)
|
||||||
|
)
|
||||||
""",
|
""",
|
||||||
(now, now, job_id),
|
(now, now, job_id, stale_before),
|
||||||
)
|
)
|
||||||
if conn.total_changes == 0:
|
if conn.total_changes == 0:
|
||||||
conn.rollback()
|
conn.rollback()
|
||||||
|
|||||||
@ -170,7 +170,16 @@ class TikTokPipeline:
|
|||||||
job = tiktok_db.claim_tiktok_job_manual_upload(job_id)
|
job = tiktok_db.claim_tiktok_job_manual_upload(job_id)
|
||||||
if not job:
|
if not job:
|
||||||
raise ValueError("任务不可立即上传(需要成片且未发布;上传中卡住的可强制重试)")
|
raise ValueError("任务不可立即上传(需要成片且未发布;上传中卡住的可强制重试)")
|
||||||
|
try:
|
||||||
await self._do_upload(job)
|
await self._do_upload(job)
|
||||||
|
except Exception as exc:
|
||||||
|
tiktok_db.update_tiktok_job(
|
||||||
|
job_id,
|
||||||
|
status="failed",
|
||||||
|
error=str(exc)[:500],
|
||||||
|
finished_at=time.time(),
|
||||||
|
)
|
||||||
|
raise
|
||||||
fresh = tiktok_db.get_tiktok_job(job_id)
|
fresh = tiktok_db.get_tiktok_job(job_id)
|
||||||
if not fresh:
|
if not fresh:
|
||||||
raise ValueError("任务不存在")
|
raise ValueError("任务不存在")
|
||||||
|
|||||||
@ -54,7 +54,12 @@ def _upload_sync(
|
|||||||
|
|
||||||
session_user = channel_pk
|
session_user = channel_pk
|
||||||
visibility = _privacy_to_visibility(privacy_level)
|
visibility = _privacy_to_visibility(privacy_level)
|
||||||
|
import contextlib
|
||||||
|
import io
|
||||||
|
|
||||||
|
buf = io.StringIO()
|
||||||
try:
|
try:
|
||||||
|
with contextlib.redirect_stdout(buf), contextlib.redirect_stderr(buf):
|
||||||
ok = upload_video(
|
ok = upload_video(
|
||||||
session_user,
|
session_user,
|
||||||
str(path.resolve()),
|
str(path.resolve()),
|
||||||
@ -75,15 +80,23 @@ def _upload_sync(
|
|||||||
raise ValueError(f"TikTok 上传失败:缺少字段 {key}") from exc
|
raise ValueError(f"TikTok 上传失败:缺少字段 {key}") from exc
|
||||||
except SystemExit as exc:
|
except SystemExit as exc:
|
||||||
raise ValueError("TikTok Cookie 缺少 sessionid,请重新粘贴") from exc
|
raise ValueError("TikTok Cookie 缺少 sessionid,请重新粘贴") from exc
|
||||||
if not ok:
|
log = buf.getvalue()
|
||||||
raise ValueError("TikTok 上传失败,请检查 Cookie 是否过期或 Node.js 是否可用")
|
if log:
|
||||||
|
print(log, end="")
|
||||||
|
published = "Published successfully" in log
|
||||||
|
if published or ok is True:
|
||||||
return {
|
return {
|
||||||
"ok": True,
|
"ok": True,
|
||||||
"publish_id": f"cookie-{uuid.uuid4().hex[:12]}",
|
"publish_id": f"cookie-{uuid.uuid4().hex[:12]}",
|
||||||
"video_id": "",
|
"video_id": "",
|
||||||
"mode": "cookie",
|
"mode": "cookie",
|
||||||
"privacy_level": privacy_level,
|
"privacy_level": privacy_level,
|
||||||
|
"dc_id": working_dc,
|
||||||
}
|
}
|
||||||
|
tail = " ".join(line.strip() for line in log.strip().splitlines()[-4:] if line.strip())[:240]
|
||||||
|
raise ValueError(
|
||||||
|
tail or "TikTok 上传失败,请检查 Cookie 是否过期或 Node.js 是否可用"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def upload_tiktok_mp4(
|
async def upload_tiktok_mp4(
|
||||||
|
|||||||
@ -1,4 +1,6 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
import time
|
||||||
|
|
||||||
from app import tiktok_db
|
from app import tiktok_db
|
||||||
from app import tiktok_session
|
from app import tiktok_session
|
||||||
|
|
||||||
@ -14,12 +16,31 @@ def test_save_and_load_session_cookies(temp_db, tmp_path, monkeypatch):
|
|||||||
assert "tt-target-idc" in names
|
assert "tt-target-idc" in names
|
||||||
|
|
||||||
|
|
||||||
|
def test_manual_upload_rejects_in_flight_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))
|
||||||
|
assert tiktok_db.claim_tiktok_job_manual_upload(job["id"]) is None
|
||||||
|
|
||||||
|
|
||||||
def test_manual_upload_allows_stuck_uploading(temp_db, tmp_path):
|
def test_manual_upload_allows_stuck_uploading(temp_db, tmp_path):
|
||||||
ch = tiktok_db.create_tiktok_channel(name="tt3")
|
ch = tiktok_db.create_tiktok_channel(name="tt3")
|
||||||
job = tiktok_db.create_tiktok_job(session_id="", room_id="r1", channel_pk=ch["id"])
|
job = tiktok_db.create_tiktok_job(session_id="", room_id="r1", channel_pk=ch["id"])
|
||||||
clip = tmp_path / "a.mp4"
|
clip = tmp_path / "a.mp4"
|
||||||
clip.write_bytes(b"x")
|
clip.write_bytes(b"x")
|
||||||
tiktok_db.update_tiktok_job(job["id"], status="uploading", clip_path=str(clip))
|
tiktok_db.update_tiktok_job(job["id"], status="uploading", clip_path=str(clip))
|
||||||
|
with tiktok_db._lock:
|
||||||
|
conn = tiktok_db._connect()
|
||||||
|
try:
|
||||||
|
conn.execute(
|
||||||
|
"UPDATE tiktok_jobs SET updated_at = ? WHERE id = ?",
|
||||||
|
(time.time() - 200, job["id"]),
|
||||||
|
)
|
||||||
|
conn.commit()
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
claimed = tiktok_db.claim_tiktok_job_manual_upload(job["id"])
|
claimed = tiktok_db.claim_tiktok_job_manual_upload(job["id"])
|
||||||
assert claimed is not None
|
assert claimed is not None
|
||||||
assert claimed["status"] == "uploading"
|
assert claimed["status"] == "uploading"
|
||||||
|
|||||||
@ -120,6 +120,16 @@ def _patch_vendor_upload() -> None:
|
|||||||
text = text.replace(old, new)
|
text = text.replace(old, new)
|
||||||
changed = True
|
changed = True
|
||||||
|
|
||||||
|
old_ret = (
|
||||||
|
'\tif not uploaded:\n'
|
||||||
|
'\t\tprint("[-] Could not upload video")\n'
|
||||||
|
'\t\treturn False\n'
|
||||||
|
)
|
||||||
|
new_ret = old_ret + '\treturn True\n'
|
||||||
|
if old_ret in text and '\treturn True\n' not in text[text.find(old_ret): text.find(old_ret) + 200]:
|
||||||
|
text = text.replace(old_ret, new_ret, 1)
|
||||||
|
changed = True
|
||||||
|
|
||||||
if changed:
|
if changed:
|
||||||
path.write_text(text, encoding="utf-8")
|
path.write_text(text, encoding="utf-8")
|
||||||
print("patched:", path)
|
print("patched:", path)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user