docs: changelog, README and plan for the one-video-per-job output lifecycle

CHANGELOG: a finished job leaves one compressed video (compressed in place
under the plain name) and the job videos expire after 5 days.

README: the output lifecycle - one video per job, 5-day retention.

Plan: the implementation plan this branch followed.
This commit is contained in:
jetson committed 2026-10-28 15:13:23 +07:00
1 parent 5e2a31792f
commit d7c1415975
3 files changed
+214

No files matched your search

+11
View File
@@ -51,6 +51,17 @@ version tags are cut in this repo — `main` is the release line).
filename fallback for legacy callers).
### Changed
- **A finished job leaves ONE compressed video** (`5e2a317`): the annotated
output is compressed in place when the job finishes — the compressed copy
replaces the uncompressed source under the same file name (`os.replace`,
atomic), so there is no second `_compressed.mp4` next to it, the client
never sees the suffix, and the API's `output_path` stays valid. Only files
over the 250 MB cap are compressed; a failed encode or a result that still
overshoots the cap leaves the original untouched, so no video is ever lost.
A 724 MB annotated video becomes 218 MB. The job videos are deleted **5 days**
after the job (`scripts/purge_expired_outputs.py`, cron daily 03:17); the
job directory, the thumbnail and the sample frames stay, so the jobs list
still renders.
- **Batch lifecycle now matches FCP's** (`1c313d9`, `59d8c55`): the line
counter is fed on **every** frame (FCP `predict.py:1066-1067` — it used to
run only while a batch was open, so crossings before the batch opened were
+10
View File
@@ -61,6 +61,16 @@ recounter --video PATH Input video file
- **Job Status**: Auto-refreshing progress page with abort button
- **Download**: Annotated MP4 per model result
### Output lifecycle
- A finished job leaves **one** video: the annotated output is compressed in
place when the job ends (the compressed copy replaces the uncompressed
source under the same file name). Files over 250 MB are compressed; the
download name is the plain one — there is no `_compressed` file.
- Job videos are **deleted 5 days** after the job
(`scripts/purge_expired_outputs.py`, run daily by cron). The job
directory, the thumbnail and the sample frames stay.
## API Endpoints
| Endpoint | Method | Description |
@@ -0,0 +1,193 @@
# Plan — post-job compression (one video per job), 5-day expiry
Repo: `/home/jetson/feedmill_semarang_project/feedmill_recorder` (branch `main`).
Date: 2026-10-28.
## Goal
1. A job's annotated output ends up as **one** video, compressed, under the **original file
name** (no `_compressed` suffix anywhere).
2. The original (uncompressed) file is gone once the compressed one is in place.
3. The video is deleted 5 days after the job.
4. (One-off) fix `output/job-eed52554/` to that state.
## The key mechanic — atomic in-place replacement
The compressed encode goes to a temp file, is validated, and then **replaces the original
under the original name**:
```python
os.replace(tmp_path, file_path) # original name, compressed content
```
`os.replace` is atomic on the same filesystem → there is never a moment where the file is
missing, and no separate delete step (nothing to lose if the encode fails: the original is
untouched until the replace).
Result on disk after a job:
```unknown
output/job-XXXX/v4-best_annotated_<source>.mp4 <- the compressed video, original name
```
No `_compressed.mp4`, no second video, the API's `output_path` stays valid, and the client
downloads the plain name.
## Facts already established
- `output/job-eed52554/v4-best_annotated_b3-2026-09-26-08-52-33.mp4` — 759,207,240 B (724.0 MiB).
- `output/job-eed52554/..._compressed.mp4` — 228,883,984 B (218.3 MiB), ffprobe-valid
(h264, 1280×720, 12 607 frames, 504.3 s).
- Compression today happens **on download only**: `app.py:428 _compress_for_download()`,
called from the route at `app.py:489`; the original is kept forever.
- Cap: 250 MB (`app.py:428`). At/below the cap → no compression, file kept as is.
- Job worker: `src/job.py` — results appended at `:283` / `:341`, status → `COMPLETED` at
`:359`, `job.completed_at` at `:377`. Jobs are **in-memory only**; orphan dirs are
re-registered on restart with `completed_at = dir mtime` (`src/job.py:96-120`).
- Nothing else reads the annotated output: the status page's player uses
`/api/preview/<job_id>` (JPEG), the thumbnail and samples come from `job.video_path`
(the uploaded source); the only consumer of the annotated file is `/download/<job_id>/`.
- Disk: `/` at 98%, 2.3 G free; `output/` = 2.9 G over 15 job dirs.
- Cron works: `* * * * * .../start_dashboard.sh` (idempotent keep-alive).
## Step 0 — one-off fix of `output/job-eed52554/` — DONE 2026-10-28
```bash
mv -f output/job-eed52554/v4-best_annotated_b3-2026-09-26-08-52-33_compressed.mp4 \
output/job-eed52554/v4-best_annotated_b3-2026-09-26-08-52-33.mp4
```
Result: the dir holds ONE video (228,883,984 B = 218.3 MiB, ffprobe-valid h264 1280×720,
12 607 frames) under the original name + `thumbnail.jpg`. 505.8 MiB freed; `/` now at 94%
(6.0 G free).
## Step 1 — new module `src/compress.py`
`app.py` imports `src.job`, so `src/job.py` cannot import `app.py` (circular). The helper
moves out of `app.py` into `src/compress.py`.
```python
"""Annotated-output lifecycle: compress in place, expire."""
MAX_BYTES = 250 * 1024 * 1024
_AUDIO_BITRATE = 64_000
_MIN_VIDEO_BITRATE = 100_000
def compress_in_place(file_path: str, max_bytes: int = MAX_BYTES) -> str | None:
"""Shrink file_path to max_bytes, replacing it in place under the same name.
Returns file_path when the file was replaced, None when nothing changed
(already within the cap, ffmpeg missing/failed, or the encode still
overshoots — the original then stays untouched).
"""
def purge_expired_media(output_dir: str, max_age_days: float = 5.0,
now: float | None = None) -> list[str]:
"""Delete the job videos under output_dir/job-*/ older than max_age_days.
Keeps the job dir, thumbnail.jpg and samples/ (small) so the jobs list
still renders. Age comes from the file mtime — jobs are in-memory only, so
the file timestamp is the only restart-proof clock. Also removes stale
'*.compress.tmp' leftovers from a killed encode.
"""
```
`compress_in_place` body = today's `_compress_for_download` (`app.py:428-472`) with:
- the encode target `file_path + ".compress.tmp"` (the format is forced with `-f mp4`, so
the odd extension is safe — today's code already relies on that);
- `os.replace(tmp_path, file_path)` instead of `os.replace(tmp_path, out_path)`;
- the cache/`_compressed` logic removed entirely — there is no second file anymore.
## Step 2 — `src/job.py`: compress when the job is done
In the worker, after the results are appended and **before** `job.status = COMPLETED`:
```python
for r in job.results:
path = r.output_path if os.path.isabs(r.output_path) else os.path.join(job.output_dir, r.output_path)
compress_in_place(path) # same name, compressed content
```
- `r.output_path` needs no change — the name is unchanged.
- The replacement only happens after a validated encode (size check passed) → a failed or
oversized encode leaves the original video intact.
- `job.status` stays `RUNNING` during the compression, so the 409 busy guard keeps the next
job out and nobody downloads a half-written file. The counts are already final; only the
file is pending (`job.current_model` gets a "Compressing…" note).
- Files at/below the 250 MB cap: untouched.
## Step 3 — `app.py`: serve it as-is
- Import `compress_in_place` from `src.compress`; drop the local `_compress_for_download`,
`_remove_quiet`, `_compressed_cache_valid` (keep thin aliases if the existing tests import
them by name).
- Download route (`app.py:475-492`) becomes:
```python
send_path = compress_in_place(file_path) or file_path
resp = send_file(send_path, as_attachment=True, download_name=os.path.basename(send_path))
```
- the name is the plain original name — nothing to strip;
- the on-demand compression stays as the fallback for jobs finished before this change
(their originals are still on disk) — and it replaces them in place too, so afterwards
there is still exactly one video per job.
## Step 4 — 5-day expiry
`scripts/purge_expired_outputs.py` — a thin CLI over `purge_expired_media`:
```bash
python scripts/purge_expired_outputs.py --output-dir output --max-age-days 5
```
Cron (new line, daily, off the minute):
```
17 3 * * * cd /home/jetson/feedmill_semarang_project/feedmill_recorder && /usr/bin/python3 scripts/purge_expired_outputs.py --max-age-days 5 >> /tmp/feedmill_purge.log 2>&1
```
- Deletes the video files whose mtime is older than 5 days (the mtime ≈ job end, since the
compressed copy is written seconds after the annotate run) plus stale `*.compress.tmp`.
- Keeps the job dir + `thumbnail.jpg` + `samples/` so the jobs list still renders.
## Step 5 — tests
`tests/test_compress.py` (new) + updates to `tests/test_download_compress.py`:
- `compress_in_place` on a small file → returns None, file untouched.
- `compress_in_place` on an oversized file → same path returned, the file on disk is the
compressed one (size ≤ cap), the name unchanged, no second file next to it.
- Failed encode → returns None, the original survives, no `.tmp` left.
- Oversized result → discarded, the original survives.
- The encode targets a temp path, never the final path directly.
- `purge_expired_media` → deletes a video older than 5 days, keeps a fresh video, keeps
`thumbnail.jpg` and `samples/`, removes a stale `.compress.tmp`.
- Job worker (in `tests/test_job.py`) → on completion the output file exists, is the
compressed one, and there is exactly ONE video in the job dir.
- Download route → serves the video under the plain name, no `_compressed` in the
`Content-Disposition`.
- Existing suite must stay green (318 tests today).
## Step 6 — docs
- `CHANGELOG.md` → unreleased "Changed": the annotated output is compressed in place when
the job finishes (one video, original name) and expires after 5 days.
- `README.md` → the output lifecycle, if the README documents the output dir today.
## Risks
- **The uncompressed annotated video is unrecoverable** after the job finishes. That is the
request; the compressed copy replaces it. The replace only happens after a validated
encode, so a failed compression keeps the original.
- **The job slot is held during the compression** (~1-3 min for a 700 MB output) → the next
job waits. The counting results are already final at that point.
- **Old jobs** (written before this change) keep their originals until downloaded (on-demand
in-place compression) or until the 5-day sweep removes them.
- The 5-day sweep deletes the video of any job whose file is old, downloaded or not.
## Out of scope
- Compressing the uploaded source in `uploads/` (kept for re-processing).
- A UI indicator for "compressing" beyond `job.current_model`.
- Per-job retention settings (fixed 5 days for all jobs).