Follow-up from #162 (detection-review chunk 1).
Problem
app/main.py imports from app.worker import app as procrastinate_app (needed to open the procrastinate connector in the FastAPI lifespan). app/worker.py in turn does from app.services.smoke_detector import SmokeDetector, and smoke_detector.py imports cv2, onnxruntime, and numpy at module top.
Net effect: the API (uvicorn) process loads onnxruntime + cv2 at startup even though it never runs inference — only the worker process does. This costs ~100 MB RAM and slower startup on the API container for zero benefit.
Suggested fix
Split the lightweight procrastinate App handle out of the heavy task module:
app/queue.py (or similar): just app = App(connector=PsycopgConnector(...)) — no heavy imports.
main.py and the enqueue endpoint import the App / task from there.
- Keep the
SmokeDetector import (and the @app.task body that uses it) in the worker task module, which only the procrastinate worker process loads.
This keeps onnxruntime/cv2 out of the API process while preserving the "imports at top" convention (no lazy/inline imports).
Notes
- Not a correctness bug — purely a resource/startup-time cost.
- ~15-line refactor touching module structure.
Follow-up from #162 (detection-review chunk 1).
Problem
app/main.pyimportsfrom app.worker import app as procrastinate_app(needed to open the procrastinate connector in the FastAPI lifespan).app/worker.pyin turn doesfrom app.services.smoke_detector import SmokeDetector, andsmoke_detector.pyimportscv2,onnxruntime, andnumpyat module top.Net effect: the API (uvicorn) process loads
onnxruntime+cv2at startup even though it never runs inference — only theworkerprocess does. This costs ~100 MB RAM and slower startup on the API container for zero benefit.Suggested fix
Split the lightweight procrastinate
Apphandle out of the heavy task module:app/queue.py(or similar): justapp = App(connector=PsycopgConnector(...))— no heavy imports.main.pyand the enqueue endpoint import theApp/ task from there.SmokeDetectorimport (and the@app.taskbody that uses it) in the worker task module, which only theprocrastinate workerprocess loads.This keeps
onnxruntime/cv2out of the API process while preserving the "imports at top" convention (no lazy/inline imports).Notes