Skip to content
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import os
import argparse
import asyncio
import logging
import traceback

import json
import signal
Expand Down Expand Up @@ -484,6 +486,19 @@ async def server_worker_health(request: Request):
return result


@app.get("/server/job_logs", tags=["serverinfo"])
async def server_job_logs(job_id: str):
try:
job = job_get(job_id)
print(job)
job_data = job.get("job_data", {})
tail = job_data.get("tail", [])
return {"status": "success", "job_id": job_id, "logs": tail}
except Exception:
logging.error("Exception in /server/job_logs: %s", traceback.format_exc())
return {"status": "error", "message": "An internal server error occurred. Please try again later."}


@app.get("/healthz")
async def healthz():
"""
Expand Down
17 changes: 16 additions & 1 deletion transformerlab/shared/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,14 +272,29 @@ async def async_run_python_daemon_and_update_status(
f.write(str(pid))

# keep a tail of recent lines so we can show them on failure:
recent_lines = deque(maxlen=10)
recent_lines = deque(maxlen=50)
last_update_time = 0.0

line = await process.stdout.readline()
error_msg = None
while line:
decoded = line.decode()
recent_lines.append(decoded.strip())

try:
now = time.time()
if now - last_update_time >= 1.0:
try:
job = Job.get(job_id)
job_data = job.get_job_data() or {}
job_data["tail"] = list(recent_lines)
job.set_job_data(job_data)
except Exception:
# Best-effort only; don't fail the loop
pass
last_update_time = now
except Exception:
pass
# If we hit the begin_string then the daemon is started and we can return!
if begin_string in decoded:
if set_process_id_function is not None:
Expand Down
Loading