feat: re-attach orphaned output dirs after restart (deletable from UI)

Jobs are in-memory; output/job-* dirs survived restarts but never
appeared in /jobs, leaving 5.2GB undeletable. JobQueue now registers
stale job-* dirs (>5min old) as COMPLETED ghosts so the existing delete
flow rmtree's them. register_orphans=False opt-out. 177 tests pass (+3).
This commit is contained in:
jetson committed 2026-10-14 11:47:03 +07:00
1 parent c2d720e86c
commit d09800c634
2 files changed
+77 -1

No files matched your search

+29 -1
View File
@@ -76,12 +76,40 @@ class Job:
class JobQueue:
"""Thread-safe job queue with background worker."""
def __init__(self, output_dir: str = "./output") -> None:
def __init__(self, output_dir: str = "./output", register_orphans: bool = True) -> None:
self._output_dir = Path(output_dir)
self._output_dir.mkdir(parents=True, exist_ok=True)
self._jobs: dict[str, Job] = {}
self._lock = threading.Lock()
self._threads: list[threading.Thread] = []
if register_orphans:
self._register_orphans()
def _register_orphans(self, min_age_sec: float = 300.0) -> None:
"""Re-attach output/job-* dirs left by restarts so they're deletable from the UI.
Jobs are in-memory; without this, dirs survive restart but never appear
in /jobs. Directories younger than min_age_sec are skipped — they may
belong to a job still running in another process.
"""
now = time.time()
for d in sorted(self._output_dir.glob("job-*")):
if not d.is_dir():
continue
if now - d.stat().st_mtime < min_age_sec:
continue
job_id = d.name
with self._lock:
if job_id in self._jobs:
continue
self._jobs[job_id] = Job(
job_id=job_id,
video_path="", # source unknown after restart
model_configs=[],
output_dir=str(d),
status=JobStatus.COMPLETED,
completed_at=d.stat().st_mtime,
)
def add_job(
self,
+48
View File
@@ -360,3 +360,51 @@ def test_run_job_merged_passes_output_max_height(tmp_path, monkeypatch):
assert job.status == JobStatus.COMPLETED
assert seen.get("output_max_height") == 720
# ── Orphan registration (jobs lost across restart) ────────────────────
def test_orphan_output_dirs_registered_on_init(tmp_path):
"""Stale output/job-* dirs re-attach as COMPLETED jobs so UI can delete them."""
import os
import time as _time
stale = tmp_path / "job-abcd1234"
stale.mkdir()
(stale / "merged_annotated.mp4").write_bytes(b"video")
old = _time.time() - 3600
os.utime(stale, (old, old))
q = JobQueue(output_dir=str(tmp_path))
job = q.get_job("job-abcd1234")
assert job is not None
assert job.status == JobStatus.COMPLETED
assert job.output_dir == str(stale)
# And the normal delete flow frees the storage
assert q.delete_job("job-abcd1234") is True
assert not stale.exists()
def test_fresh_output_dir_skipped(tmp_path):
"""Dirs younger than the grace window may belong to a live job — not registered."""
fresh = tmp_path / "job-99999999"
fresh.mkdir() # mtime = now
q = JobQueue(output_dir=str(tmp_path))
assert q.get_job("job-99999999") is None
assert fresh.exists()
def test_orphan_registration_off(tmp_path):
"""register_orphans=False leaves disk state alone (used by tests/tools)."""
import os
import time as _time
stale = tmp_path / "job-abcd1234"
stale.mkdir()
old = _time.time() - 3600
os.utime(stale, (old, old))
q = JobQueue(output_dir=str(tmp_path), register_orphans=False)
assert q.get_job("job-abcd1234") is None