Stream job logs from current offset

This commit is contained in:
2026-06-30 15:51:22 +08:00
parent e766e4ed26
commit 4b3d750df9
5 changed files with 73 additions and 10 deletions

View File

@@ -59,6 +59,11 @@ def evaluate_project() -> dict:
and "setResults(resultsNext)" in frontend_text
and "slice(0, 240)" not in frontend_text,
"job_progress_ui": "JobProgressBar" in frontend_text and "progressTrack" in frontend_text,
"live_log_stream_ui": "EventSource" in frontend_text
and "eventSourceRef" in frontend_text
and "log_size" in frontend_text
and "events?offset=" in frontend_text,
"live_log_offset_api": "log_size" in backend_text and "offset: int = Query(0, ge=0)" in backend_text,
"runtime_readiness_ui": "runtimeReadiness" in frontend_text and "环境就绪" in frontend_text,
"capability_matrix_ui": "capabilities" in frontend_text and "全功能矩阵" in frontend_text,
"dataset_api": "/api/datasets" in backend_text and "api_upload_dataset_files" in backend_text,

View File

@@ -39,6 +39,8 @@ app.add_middleware(
def _job_with_progress(job: dict, include_log_tail: bool = False) -> dict:
enriched = dict(job)
max_bytes = 65536 if include_log_tail else 32768
log_path = Path(enriched["log_path"])
enriched["log_size"] = log_path.stat().st_size if log_path.exists() else 0
enriched["progress"] = progress_from_log_path(enriched["log_path"], enriched["status"], max_bytes=max_bytes)
if include_log_tail:
enriched["log_tail"] = db.log_tail(enriched["log_path"], max_bytes=max_bytes)
@@ -183,9 +185,9 @@ def api_cancel_job(job_id: str) -> dict:
@app.get("/api/jobs/{job_id}/events")
async def api_job_events(job_id: str):
async def api_job_events(job_id: str, offset: int = Query(0, ge=0)):
async def stream():
last_size = 0
last_size = offset
while True:
job = db.get_job(job_id)
if not job:
@@ -196,6 +198,8 @@ async def api_job_events(job_id: str):
chunk = ""
if path.exists():
size = path.stat().st_size
if last_size > size:
last_size = size
if size > last_size:
with path.open("rb") as handle:
handle.seek(last_size)