Consulawtech commited on
Commit
9496b0b
·
1 Parent(s): c8fe1a2

Store submitted_at; purge job rows after 7 days

Browse files
Files changed (1) hide show
  1. app.py +30 -0
app.py CHANGED
@@ -104,6 +104,7 @@ def _upsert_job(
104
  result: Optional[dict] = None,
105
  progress: Optional[dict] = None,
106
  detail: Optional[str] = None,
 
107
  ):
108
  """Write job state to Supabase. Falls back to local cache on failure."""
109
  payload = {
@@ -116,6 +117,8 @@ def _upsert_job(
116
  payload["progress"] = progress
117
  if detail is not None:
118
  payload["detail"] = detail
 
 
119
 
120
  _job_results_cache[job_id] = dict(payload)
121
  if progress is not None:
@@ -318,6 +321,31 @@ async def diarization_status_endpoint():
318
  _CANCELLED: set = set()
319
 
320
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
321
  class _JobCancelled(Exception):
322
  pass
323
 
@@ -1680,7 +1708,9 @@ async def transcribe(
1680
  jid,
1681
  status="processing",
1682
  progress={"current": 0, "total": 0, "percent": 0},
 
1683
  )
 
1684
 
1685
  # For Nigerian languages we pass the primary language code explicitly
1686
  # (e.g. "yo") so Whisper uses its Yoruba decoder and produces proper
 
104
  result: Optional[dict] = None,
105
  progress: Optional[dict] = None,
106
  detail: Optional[str] = None,
107
+ submitted_at: Optional[float] = None,
108
  ):
109
  """Write job state to Supabase. Falls back to local cache on failure."""
110
  payload = {
 
117
  payload["progress"] = progress
118
  if detail is not None:
119
  payload["detail"] = detail
120
+ if submitted_at is not None:
121
+ payload["submitted_at"] = submitted_at
122
 
123
  _job_results_cache[job_id] = dict(payload)
124
  if progress is not None:
 
321
  _CANCELLED: set = set()
322
 
323
 
324
+ # Job rows hold finished transcripts so the app can collect them; they are
325
+ # not kept beyond JOB_RETENTION_DAYS (stated in the privacy policy).
326
+ JOB_RETENTION_DAYS = 7
327
+ _last_purge = 0.0
328
+
329
+
330
+ def _purge_old_jobs() -> None:
331
+ global _last_purge
332
+ now = time.time()
333
+ if now - _last_purge < 3600:
334
+ return
335
+ _last_purge = now
336
+ cutoff = now - JOB_RETENTION_DAYS * 86400
337
+ def run():
338
+ if _SUPABASE_URL and _SUPABASE_KEY and _requests_available:
339
+ try:
340
+ for when in (f"lt.{cutoff}", "is.null"): # null: rows from before timestamps were stored
341
+ _requests.delete(f"{_SUPABASE_URL}/rest/v1/transcription_jobs",
342
+ params={"submitted_at": when}, headers=_sb_headers(), timeout=15)
343
+ except Exception as e:
344
+ logger.warning(f"Job purge failed: {e}")
345
+ threading.Thread(target=run, daemon=True).start()
346
+
347
+
348
+
349
  class _JobCancelled(Exception):
350
  pass
351
 
 
1708
  jid,
1709
  status="processing",
1710
  progress={"current": 0, "total": 0, "percent": 0},
1711
+ submitted_at=_JOB_SUBMIT_TIMES[jid],
1712
  )
1713
+ _purge_old_jobs()
1714
 
1715
  # For Nigerian languages we pass the primary language code explicitly
1716
  # (e.g. "yo") so Whisper uses its Yoruba decoder and produces proper