diff --git a/backend/app/x_clips_db.py b/backend/app/x_clips_db.py index 326c100..81bedcf 100644 --- a/backend/app/x_clips_db.py +++ b/backend/app/x_clips_db.py @@ -440,12 +440,25 @@ def reclaim_stale_x_clip_recordings(*, max_age_sec: float = 240) -> int: conn.close() -def reclaim_stale_x_clip_posts(*, max_age_sec: float = 900) -> int: +def reclaim_stale_x_clip_posts(*, max_age_sec: float = 900, max_attempts: int = 3) -> int: cutoff = time.time() - max(120.0, float(max_age_sec)) + now = time.time() with _lock: conn = _connect() try: ensure_x_clips_tables(conn) + # Fail jobs that exceeded retry limit + conn.execute( + """ + UPDATE x_clip_jobs + SET status = 'failed', error = '超过最大重试次数', updated_at = ?, finished_at = ? + WHERE status = 'posting' AND updated_at < ? + AND IFNULL(tweet_id, '') = '' + AND IFNULL(finished_at, 0) = 0 + AND attempts >= ? + """, + (now, now, cutoff, max(1, int(max_attempts))), + ) cur = conn.execute( """ UPDATE x_clip_jobs @@ -453,8 +466,10 @@ def reclaim_stale_x_clip_posts(*, max_age_sec: float = 900) -> int: x2_job_id = '', media_key = CASE WHEN media_url != '' THEN media_key ELSE '' END WHERE status = 'posting' AND updated_at < ? AND IFNULL(tweet_id, '') = '' + AND IFNULL(finished_at, 0) = 0 + AND attempts < ? """, - (time.time(), cutoff), + (now, cutoff, max(1, int(max_attempts))), ) conn.commit() return int(cur.rowcount or 0) diff --git a/backend/app/x_clips_pipeline.py b/backend/app/x_clips_pipeline.py index 183a5da..9cb9938 100644 --- a/backend/app/x_clips_pipeline.py +++ b/backend/app/x_clips_pipeline.py @@ -605,6 +605,10 @@ class XClipsPipeline: or result.get("id") or "" ).strip() + # Extension postTweet does not return tweetId; use x2 job id + # as proof of completion to prevent reclaim loop. + if not tweet_id: + tweet_id = f"x2:{x2_id}" x_clips_db.update_x_clip_job( jid, status="posted",