WhoIsLuwah commited on
Commit
9b5f12c
·
1 Parent(s): b7a2ba9

feat(backend): Supabase queue functions (claim/update/mark_failed)

Browse files
Files changed (2) hide show
  1. db.py +39 -0
  2. verify_pipeline.py +9 -0
db.py CHANGED
@@ -94,6 +94,45 @@ def delete_reel(reel_id: str) -> bool:
94
  return bool(res.data)
95
 
96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
  def cluster_counts() -> list:
98
  res = get_client().table(TABLE).select("cluster").execute()
99
  counts = Counter((r.get("cluster") or "Unclustered") for r in (res.data or []))
 
94
  return bool(res.data)
95
 
96
 
97
+ def claim_next_pending() -> Optional[dict]:
98
+ """Atomically claim the oldest pending reel: flip it to 'processing' and
99
+ return the raw row. Returns None if the queue is empty or another worker
100
+ won the claim."""
101
+ c = get_client()
102
+ res = (c.table(TABLE).select("*")
103
+ .eq("status", "pending").order("created_at").limit(1).execute())
104
+ rows = res.data or []
105
+ if not rows:
106
+ return None
107
+ row = rows[0]
108
+ upd = (c.table(TABLE).update({"status": "processing"})
109
+ .eq("id", row["id"]).eq("status", "pending").execute())
110
+ if not upd.data:
111
+ return None # lost the race
112
+ return row
113
+
114
+
115
+ def update_reel_result(reel_id: str, title: str, raw_transcript, post_caption,
116
+ extracted_json, status: str = "done") -> None:
117
+ """Write pipeline results back onto an existing (claimed) row."""
118
+ get_client().table(TABLE).update({
119
+ "title": title,
120
+ "raw_transcript": raw_transcript,
121
+ "post_caption": post_caption,
122
+ "extracted_json": extracted_json,
123
+ "status": status,
124
+ "error": None,
125
+ }).eq("id", reel_id).execute()
126
+
127
+
128
+ def mark_failed(reel_id: str, error) -> None:
129
+ """Mark a claimed row failed, recording a truncated error message."""
130
+ get_client().table(TABLE).update({
131
+ "status": "failed",
132
+ "error": str(error)[:500],
133
+ }).eq("id", reel_id).execute()
134
+
135
+
136
  def cluster_counts() -> list:
137
  res = get_client().table(TABLE).select("cluster").execute()
138
  counts = Counter((r.get("cluster") or "Unclustered") for r in (res.data or []))
verify_pipeline.py CHANGED
@@ -199,6 +199,14 @@ def test_db_module_surface():
199
  print("[OK] db module surface passed!")
200
 
201
 
 
 
 
 
 
 
 
 
202
  def test_extract_url_regression():
203
  print("Testing POST /extract/url via process_reel_url (mocked)...")
204
  import main
@@ -288,6 +296,7 @@ if __name__ == "__main__":
288
  test_cluster_chunking()
289
  test_cluster_merge_pass()
290
  test_db_module_surface()
 
291
  test_extract_url_regression()
292
  test_delete_reel()
293
  test_list_clusters()
 
199
  print("[OK] db module surface passed!")
200
 
201
 
202
+ def test_db_queue_surface():
203
+ print("Testing db.py exposes queue functions...")
204
+ import db
205
+ for fn in ("claim_next_pending", "update_reel_result", "mark_failed"):
206
+ assert hasattr(db, fn), f"db.{fn} missing"
207
+ print("[OK] db queue surface passed!")
208
+
209
+
210
  def test_extract_url_regression():
211
  print("Testing POST /extract/url via process_reel_url (mocked)...")
212
  import main
 
296
  test_cluster_chunking()
297
  test_cluster_merge_pass()
298
  test_db_module_surface()
299
+ test_db_queue_surface()
300
  test_extract_url_regression()
301
  test_delete_reel()
302
  test_list_clusters()