[AZ-180] Refactor detection event handling and improve SSE support

- Updated the detection image endpoint to require a channel ID for event streaming.
- Introduced a new endpoint for streaming detection events, allowing clients to receive real-time updates.
- Enhanced the internal buffering mechanism for detection events to manage multiple channels.
- Refactored the inference module to support the new event handling structure.

Made-with: Cursor
This commit is contained in:
Oleksandr Bezdieniezhnykh
2026-04-03 02:42:05 +03:00
parent 2c35e59a77
commit 8baa96978b
26 changed files with 819 additions and 413 deletions
+24 -14
View File
@@ -1,6 +1,7 @@
import json
import threading
import time
import uuid
from pathlib import Path
import pytest
@@ -24,29 +25,22 @@ def video_events(warm_engine, http_client, auth_headers):
if not Path(_VIDEO).is_file():
pytest.skip(f"missing fixture {_VIDEO}")
r = http_client.post(
"/detect/video",
data=_chunked_reader(_VIDEO),
headers={
**auth_headers,
"X-Filename": "video_test01.mp4",
"Content-Type": "application/octet-stream",
},
timeout=15,
)
assert r.status_code == 200
media_id = r.json()["mediaId"]
channel_id = str(uuid.uuid4())
collected: list[tuple[float, dict]] = []
thread_exc: list[BaseException] = []
done = threading.Event()
connected = threading.Event()
def _listen():
try:
with http_client.get(
f"/detect/{media_id}", stream=True, timeout=35, headers=auth_headers
f"/detect/events/{channel_id}",
stream=True,
timeout=60,
headers=auth_headers,
) as resp:
resp.raise_for_status()
connected.set()
sse = sseclient.SSEClient(resp)
for event in sse.events():
if not event.data or not str(event.data).strip():
@@ -61,10 +55,26 @@ def video_events(warm_engine, http_client, auth_headers):
except BaseException as e:
thread_exc.append(e)
finally:
connected.set()
done.set()
th = threading.Thread(target=_listen, daemon=True)
th.start()
connected.wait(timeout=5)
r = http_client.post(
"/detect/video",
data=_chunked_reader(_VIDEO),
headers={
**auth_headers,
"X-Channel-Id": channel_id,
"X-Filename": "video_test01.mp4",
"Content-Type": "application/octet-stream",
},
timeout=15,
)
assert r.status_code == 202
assert done.wait(timeout=30)
th.join(timeout=5)
assert not thread_exc, thread_exc