fix(zopk): Add heartbeat to knowledge extraction SSE stream
Some checks are pending
NordaBiz Tests / Unit & Integration Tests (push) Waiting to run
NordaBiz Tests / E2E Tests (Playwright) (push) Blocked by required conditions
NordaBiz Tests / Smoke Tests (Production) (push) Blocked by required conditions
NordaBiz Tests / Send Failure Notification (push) Blocked by required conditions

Long-running Gemini extractions (30-120s per article) caused SSE
connection timeout. Now runs extraction in a thread and sends
heartbeat updates every 10s to keep the connection alive.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Maciej Pienczyn 2026-02-09 15:42:18 +01:00
parent 3b3bb7bdd7
commit 1c02d109d7

View File

@ -229,7 +229,21 @@ def admin_zopk_knowledge_extract_stream():
# Send processing update
yield f"data: {json.dumps({'current': idx, 'total': total, 'percent': round((idx-1)/total*100, 1), 'stage': 'extracting', 'status': 'processing', 'message': f'Analizuję AI: {article.title[:50]}...', 'article_id': article.id, 'article_title': article.title[:80], 'details': stats}, ensure_ascii=False)}\n\n"
result = service.extract_from_news(article.id)
# Run extraction in thread with heartbeats to prevent SSE timeout
import threading
extract_result = [None]
def _extract():
extract_result[0] = service.extract_from_news(article.id)
t = threading.Thread(target=_extract)
t.start()
heartbeat_count = 0
while t.is_alive():
t.join(timeout=10)
if t.is_alive():
heartbeat_count += 1
elapsed = round(time.time() - start_time)
yield f"data: {json.dumps({'current': idx, 'total': total, 'percent': round((idx-1)/total*100, 1), 'stage': 'extracting', 'status': 'processing', 'message': f'Analizuję AI: {article.title[:50]}... ({elapsed}s)', 'details': stats}, ensure_ascii=False)}\n\n"
result = extract_result[0]
if result.success:
stats['success'] += 1