Skip to content

Commit 0717968

Browse files
committed
Pushing the run_id to the queue that feeds the live stream
1 parent a74f182 commit 0717968

3 files changed

Lines changed: 49 additions & 11 deletions

File tree

th_cli/test_run/log_stream_handler.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,23 @@ def set_run_id(self, run_id: int) -> None:
7777
once the run has been created (its id isn't known when the server
7878
starts).
7979
"""
80-
if self.is_running:
81-
self.http_server.set_run_id(run_id)
82-
80+
if not self.is_running:
81+
return
82+
83+
self.http_server.set_run_id(run_id)
84+
85+
# A viewer may already be connected (the run_id is typically set
86+
# only *after* the viewer URL was printed and likely opened), so
87+
# also push it through the existing SSE stream as a control message
88+
# - a future/refreshed page load will pick it up from the HTTP
89+
# server attribute above, but an already-open one only sees this.
90+
try:
91+
self.log_queue.put_nowait({"__event__": "run_id", "run_id": run_id})
92+
except queue.Full:
93+
# Best-effort: a future page load/refresh will still pick up
94+
# the run id via the HTTP server attribute set above.
95+
pass
96+
8397
def stop(self):
8498
"""Stop the log streaming HTTP server."""
8599
if not self.is_running:

th_cli/test_run/log_viewer.html

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -525,11 +525,15 @@
525525

526526
<script>
527527
// Injected server-side: the run's id (or null if the run hasn't been
528-
// created yet) and the backend's configured hostname (or null if it
529-
// was just "localhost"/"127.0.0.1" - meaningless once embedded in a
530-
// page that may be opened from a different device than the one
531-
// running the CLI). See updateDownloadLink() below.
532-
const RUN_ID = {run_id};
528+
// created yet - reassigned later via the 'run_id' SSE event below if
529+
// it arrives after this page has already loaded, which is the
530+
// common case: the viewer URL is printed and typically opened
531+
// before the run is created) and the backend's configured hostname
532+
// (or null if it was just "localhost"/"127.0.0.1" - meaningless
533+
// once embedded in a page that may be opened from a different
534+
// device than the one running the CLI). See updateDownloadLink()
535+
// below.
536+
let RUN_ID = {run_id};
533537
const BACKEND_HOST = {backend_host};
534538

535539
let eventSource = null;
@@ -572,6 +576,16 @@
572576
statusText.textContent = 'Connected';
573577
}});
574578

579+
eventSource.addEventListener('run_id', function(e) {{
580+
// Arrives once the run is created, which is typically after
581+
// this page has already loaded (RUN_ID was baked in as null
582+
// at that point) - re-resolve the download link now that
583+
// it's known, without requiring a page reload.
584+
const data = JSON.parse(e.data);
585+
RUN_ID = data.run_id;
586+
updateDownloadLink();
587+
}});
588+
575589
eventSource.addEventListener('log', function(e) {{
576590
const logEntry = JSON.parse(e.data);
577591
logBatchQueue.push(logEntry);

th_cli/test_run/logs_http_server.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,20 +76,30 @@ def stream_logs(self):
7676
try:
7777
# Get log entry from queue with timeout
7878
log_entry = log_queue.get(timeout=1.0)
79-
79+
8080
if log_entry is None: # Signal to stop
8181
logger.info("Received end-of-stream signal for logs")
8282
self._send_sse_event("end", {"message": "Log stream ended"})
8383
break
8484

85+
if isinstance(log_entry, dict) and log_entry.get("__event__") == "run_id":
86+
# Control message, not a log line - lets an already-
87+
# connected viewer pick up the run id once it's
88+
# known, instead of only a fresh page load.
89+
if not self._send_sse_event("run_id", {"run_id": log_entry["run_id"]}):
90+
logger.debug("Client disconnected while streaming")
91+
client_disconnected = True
92+
break
93+
continue
94+
8595
# Send log entry as SSE event
8696
if not self._send_sse_event("log", log_entry):
8797
logger.debug("Client disconnected while streaming")
8898
client_disconnected = True
8999
break
90-
100+
91101
sent_count += 1
92-
102+
93103
if sent_count % 100 == 0:
94104
logger.debug(f"Sent {sent_count} log entries to client")
95105

0 commit comments

Comments
 (0)