"""Child process for the work that touches media: one job, or one check. Running it apart from the server lets the parent stop a job that runs too long, and keeps a crash or a memory spike in a render away from the API. An unexpected exception ends the process with a traceback and no result, which the parent reports as a crashed job. python -m app.runner job python -m app.runner check """ from __future__ import annotations import json import sys from pathlib import Path from . import engine def _save(path: Path, data: dict) -> None: temporary = path.with_suffix(".tmp") temporary.write_text(json.dumps(data, ensure_ascii=False, default=str), encoding="utf-8") temporary.replace(path) def _job(root: Path) -> dict: request = json.loads((root / "request.json").read_text(encoding="utf-8")) def progress(stage: str, note: str) -> None: _save(root / "progress.json", {"stage": stage, "note": note}) try: return {"ok": True, **engine.run_job(root, request, progress)} except engine.EngineError as exc: return {"ok": False, "error": {"code": "job_failed", "message": str(exc), "hint": "Check the files and the options, then send the job again."}} def _check(path: Path, target: str) -> dict: try: return {"ok": True, **engine.check_file(path, target)} except engine.EngineError as exc: return {"ok": False, "message": str(exc)} def main(argv: list[str]) -> None: if argv[0] == "job": root = Path(argv[1]) _save(root / "result.json", _job(root)) else: sys.stdout.write(json.dumps(_check(Path(argv[1]), argv[2]), ensure_ascii=False, default=str)) if __name__ == "__main__": main(sys.argv[1:])