Files
instameex/src/script/app/jobs.py
T
Djeex 974690132e Reorganize into src/ with script/assets/templates, illustration/ at root
New layout:
  src/script/app/    Python package (was app/)
  src/assets/img/    SVGs (was static/*.svg)
  src/assets/css/    stylesheet (was static/style.css)
  src/templates/     HTML templates (was templates/)
  illustration/      README screenshot (was screenshots/), kept at
                      the repo root since it's a doc asset, not
                      something the app serves
  main.py, jobs/, VERSION, docker/  unchanged locations

main.py stays at the repo root as the entry point (matches how
Docker and local runs already invoke it) and prepends src/script to
sys.path before importing app, rather than moving into src/script/
itself and needing every entry point updated to match.

config.py's path constants are recomputed for app/config.py's new
depth (src/script/app/), with SRC_DIR and REPO_ROOT resolved
explicitly rather than a single ambiguous BASE_DIR, since "templates
and assets live under src/" and "jobs and VERSION live at the repo
root" are no longer the same directory. Flask's static_folder now
points at src/assets, which serves img/ and css/ under it at
whatever URL prefix Flask derives from the folder name (/assets, not
/static); templates already reference assets via the "static"
endpoint name through url_for, so this needed the url_for() calls'
filename argument updated with the img/ or css/ prefix, not a
hardcoded URL change.

Docker copies src/ into the image preserving this same internal
structure, so the path arithmetic in config.py resolves identically
in both a local checkout and the container, no --chdir or other
container-specific path handling needed.

Verified end to end after the move: local Flask dev server, a full
Docker rebuild, and a real HTTP conversion request through the
running container (TIFF HDR pipeline, numpy/tifffile imports
included) all succeed.
2026-08-01 15:26:18 +02:00

66 lines
1.7 KiB
Python

"""In-memory job registry and lifecycle (creation, heartbeat, reaping).
Single-process only: JOBS lives in this module's memory, so this assumes
one gunicorn worker (see Dockerfile). Multiple workers would each keep a
separate registry and requests would 404 depending on which one handled
them.
"""
import shutil
import threading
import time
from pathlib import Path
from .config import HEARTBEAT_TIMEOUT, JOBS_DIR, REAPER_INTERVAL
JOBS: dict[str, dict] = {}
def reset_jobs_dir() -> None:
"""Wipe any leftovers from a previous run (crash, forced restart) —
the in-memory registry never survives a restart anyway, so orphaned
job dirs on disk would otherwise sit there forever."""
shutil.rmtree(JOBS_DIR, ignore_errors=True)
JOBS_DIR.mkdir(parents=True, exist_ok=True)
def new_job_dir(job_id: str) -> Path:
job_dir = JOBS_DIR / job_id
job_dir.mkdir(parents=True, exist_ok=True)
return job_dir
def register(job_id: str, **fields) -> None:
JOBS[job_id] = {**fields, "last_seen": time.time()}
def get(job_id: str) -> dict | None:
return JOBS.get(job_id)
def touch(job_id: str) -> None:
job = JOBS.get(job_id)
if job:
job["last_seen"] = time.time()
def discard_dir(job_dir: Path) -> None:
shutil.rmtree(job_dir, ignore_errors=True)
def cleanup(job_id: str) -> None:
JOBS.pop(job_id, None)
discard_dir(JOBS_DIR / job_id)
def _reap_stale_jobs() -> None:
while True:
time.sleep(REAPER_INTERVAL)
cutoff = time.time() - HEARTBEAT_TIMEOUT
stale = [jid for jid, job in list(JOBS.items()) if job.get("last_seen", 0) < cutoff]
for jid in stale:
cleanup(jid)
def start_reaper() -> None:
threading.Thread(target=_reap_stale_jobs, daemon=True).start()