fix: add logging to backend — analyze, job submit, status, cleanup
Backend had zero logging. Frontend logs were fine (logging.basicConfig in app.py). Now both services write structured logs to systemd journal: journalctl -u vm-bench -f # frontend (LXC) journalctl -u vm-bench-backend -f # backend (srv2)
This commit is contained in:
parent
989807f29e
commit
42d3cdf1fe
1 changed files with 28 additions and 3 deletions
|
|
@ -8,7 +8,8 @@ Matches open-api.yaml v1.1.0 spec exactly.
|
|||
|
||||
from __future__ import annotations
|
||||
|
||||
from fastapi import FastAPI, HTTPException
|
||||
import logging
|
||||
from fastapi import FastAPI, HTTPException, Request
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
|
||||
from models import (
|
||||
|
|
@ -24,6 +25,15 @@ from models import (
|
|||
from converter import extract_if_needed, discover_disk, detect_os, detect_efi
|
||||
from provisioner import submit_job, get_job_status, cleanup_staging
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Logging
|
||||
# ---------------------------------------------------------------------------
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format="%(asctime)s %(levelname)s %(message)s",
|
||||
)
|
||||
logger = logging.getLogger("backend")
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Constants
|
||||
# ---------------------------------------------------------------------------
|
||||
|
|
@ -71,20 +81,26 @@ def health() -> HealthResponse:
|
|||
def analyze(req: AnalyzeRequest) -> AnalyzeResponse:
|
||||
"""Analyze a source file: extract if archive, find disk, detect OS + EFI."""
|
||||
_validate_filename(req.source_filename)
|
||||
logger.info("Analyze request: vmid=%d file=%s", req.vmid, req.source_filename)
|
||||
|
||||
try:
|
||||
source = extract_if_needed(req.source_filename)
|
||||
logger.info("Source ready: %s", source)
|
||||
except Exception as exc:
|
||||
logger.error("Extraction failed: %s", exc)
|
||||
raise HTTPException(status_code=400, detail=str(exc))
|
||||
|
||||
try:
|
||||
disk = discover_disk(source)
|
||||
logger.info("Disk found: %s (format=%s, size=%.1f GiB)", disk.path, disk.format, disk.size_gb)
|
||||
except Exception as exc:
|
||||
logger.error("Disk discovery failed: %s", exc)
|
||||
raise HTTPException(status_code=400, detail=f"Disk discovery failed: {exc}")
|
||||
|
||||
extract_dir = source if source.is_dir() else source.parent
|
||||
os_type = detect_os(disk.path, extract_dir)
|
||||
efi = detect_efi(disk.path)
|
||||
logger.info("Analysis result: os=%s efi=%s", os_type, efi)
|
||||
|
||||
return AnalyzeResponse(
|
||||
vmid=req.vmid,
|
||||
|
|
@ -99,9 +115,15 @@ def analyze(req: AnalyzeRequest) -> AnalyzeResponse:
|
|||
@app.post(f"{API_PREFIX}/jobs", response_model=JobStatusResponse, status_code=202)
|
||||
def create_job(req: JobSubmissionRequest) -> JobStatusResponse:
|
||||
"""Submit a conversion + provisioning job."""
|
||||
logger.info("Job submit: vmid=%d name=%s disk=%s storage=%s",
|
||||
req.vmid, req.vm_name,
|
||||
req.boot_disk.source_filename, req.target_storage)
|
||||
try:
|
||||
return submit_job(req)
|
||||
result = submit_job(req)
|
||||
logger.info("Job queued: %s", result.job_id)
|
||||
return result
|
||||
except Exception as exc:
|
||||
logger.error("Job submission failed: %s", exc)
|
||||
raise HTTPException(status_code=400, detail=str(exc))
|
||||
|
||||
|
||||
|
|
@ -109,8 +131,10 @@ def create_job(req: JobSubmissionRequest) -> JobStatusResponse:
|
|||
def job_status(job_id: str) -> JobStatusResponse:
|
||||
"""Get current status of a conversion job."""
|
||||
try:
|
||||
return get_job_status(job_id)
|
||||
result = get_job_status(job_id)
|
||||
return result
|
||||
except Exception:
|
||||
logger.warning("Job not found: %s", job_id)
|
||||
raise HTTPException(status_code=404, detail=f"Job not found: {job_id}")
|
||||
|
||||
|
||||
|
|
@ -121,6 +145,7 @@ def cleanup_job(job_id: str, req: CleanupRequest) -> CleanupResponse:
|
|||
vmid = int(job_id.split("_")[1])
|
||||
except (IndexError, ValueError):
|
||||
vmid = 0
|
||||
logger.info("Cleanup: job=%s vmid=%d delete=%s", job_id, vmid, req.delete_staging_files)
|
||||
result = cleanup_staging(vmid, req.delete_staging_files)
|
||||
return CleanupResponse(
|
||||
job_id=result["job_id"],
|
||||
|
|
|
|||
Loading…
Reference in a new issue