Newer
Older
MedSegAgentWebApp / webapp / backend / jobs.py
"""In-memory job store. Single-process, single-user demo -- no DB/Redis
needed. Guarded by a plain lock since FastAPI's BackgroundTasks run in a
threadpool, not the event loop thread.
"""

import threading
import time

_jobs = {}
_jobs_lock = threading.Lock()


def create_job(job_id, **fields):
    with _jobs_lock:
        _jobs[job_id] = {
            "status": "queued",
            "message": "",
            "created_at": time.time(),
            **fields,
        }


def update_job(job_id, **fields):
    with _jobs_lock:
        if job_id in _jobs:
            _jobs[job_id].update(fields)


def get_job(job_id):
    with _jobs_lock:
        job = _jobs.get(job_id)
        return dict(job) if job is not None else None