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

@@ -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)