Spaces:
Running
Running
fix: terminate stalled ragas tasks
Browse files
app.py
CHANGED
|
@@ -19,6 +19,7 @@ import os
|
|
| 19 |
import threading
|
| 20 |
import time
|
| 21 |
import uuid
|
|
|
|
| 22 |
|
| 23 |
import gradio as gr
|
| 24 |
from langgraph.types import Command
|
|
@@ -517,8 +518,32 @@ def _ragas_worker(
|
|
| 517 |
handler,
|
| 518 |
) -> None:
|
| 519 |
"""Daemon thread body: compute scores, stash them for the Timer poll."""
|
|
|
|
| 520 |
try:
|
| 521 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 522 |
except Exception as exc:
|
| 523 |
logger.warning("RAGAS worker failed: %s", exc)
|
| 524 |
with _ragas_lock:
|
|
|
|
| 19 |
import threading
|
| 20 |
import time
|
| 21 |
import uuid
|
| 22 |
+
from concurrent.futures import ThreadPoolExecutor, TimeoutError as FutureTimeoutError
|
| 23 |
|
| 24 |
import gradio as gr
|
| 25 |
from langgraph.types import Command
|
|
|
|
| 518 |
handler,
|
| 519 |
) -> None:
|
| 520 |
"""Daemon thread body: compute scores, stash them for the Timer poll."""
|
| 521 |
+
timeout = float(os.getenv("OCR_RAGAS_TIMEOUT_SECONDS", "90"))
|
| 522 |
try:
|
| 523 |
+
# The provider call may block on a network timeout. Keep the task
|
| 524 |
+
# terminal and observable even when the provider does not return.
|
| 525 |
+
pool = ThreadPoolExecutor(max_workers=1, thread_name_prefix="ragas-call")
|
| 526 |
+
future = pool.submit(
|
| 527 |
+
_compute_and_log_ragas,
|
| 528 |
+
inputs,
|
| 529 |
+
repo,
|
| 530 |
+
pr_number,
|
| 531 |
+
run_trace_id,
|
| 532 |
+
handler,
|
| 533 |
+
)
|
| 534 |
+
try:
|
| 535 |
+
scores = future.result(timeout=timeout)
|
| 536 |
+
except FutureTimeoutError:
|
| 537 |
+
scores = {}
|
| 538 |
+
with _ragas_lock:
|
| 539 |
+
entry = _ragas_tasks.get(task_id)
|
| 540 |
+
if entry is not None:
|
| 541 |
+
entry["error"] = f"RAGAS exceeded {int(timeout)} second timeout"
|
| 542 |
+
entry["done"] = True
|
| 543 |
+
pool.shutdown(wait=False, cancel_futures=True)
|
| 544 |
+
return
|
| 545 |
+
else:
|
| 546 |
+
pool.shutdown(wait=True)
|
| 547 |
except Exception as exc:
|
| 548 |
logger.warning("RAGAS worker failed: %s", exc)
|
| 549 |
with _ragas_lock:
|