feat/raise-target-clips-limit (#12)
All checks were successful
CI / unit (push) Successful in 46s
CI / backend-e2e (push) Successful in 1m22s
CI / dashboard-e2e (push) Successful in 2m26s

Reviewed-on: #12
Co-authored-by: pedro-bento <mail@pbento.pt>
Co-committed-by: pedro-bento <mail@pbento.pt>
This commit is contained in:
pedro-bento 2026-10-14 09:54:06 +01:00 committed by pedro-bento
parent 980dae98e8
commit c7f922de32
4 changed files with 26 additions and 49 deletions

View file

@ -53,8 +53,6 @@ MAX_FILE_SIZE_MB = int(os.environ.get("MAX_FILE_SIZE_MB", "0") or "0")
# age-based sweep alone can't stop a burst of long videos from filling the disk.
# 0 disables the cap.
OUTPUT_MAX_GB = int(os.environ.get("OUTPUT_MAX_GB", "25"))
# Same idea for source uploads, which are the biggest single files on disk.
UPLOADS_MAX_GB = int(os.environ.get("UPLOADS_MAX_GB", "15"))
# Pre-flight quality gate: warn before processing a YouTube source below this
# height (0 disables). Only applies to URLs; uploads are whatever the user gave.
QUALITY_GATE_MIN_HEIGHT = int(os.environ.get("QUALITY_GATE_MIN_HEIGHT", "720"))
@ -117,7 +115,7 @@ for _stream in (sys.stdout, sys.stderr):
# Job/file retention (issue #46). Self-host defaults to 24h: the 1h sweep kept
# deleting finished projects under users who never touched their env, and the
# OUTPUT_MAX_GB / UPLOADS_MAX_GB caps below already bound the disk.
# OUTPUT_MAX_GB cap below already bounds the disk.
JOB_RETENTION_SECONDS = int(
os.environ.get("JOB_RETENTION_SECONDS", "86400")
)
@ -1038,39 +1036,6 @@ def _sweep_expired_uploads(now):
pass
def _enforce_uploads_size_cap():
"""Delete the oldest source uploads while UPLOAD_DIR is over UPLOADS_MAX_GB.
Sources are only needed while a job runs (and for the preview afterwards),
but they're the biggest files on disk — up to MAX_FILE_SIZE_MB each.
"""
cap = UPLOADS_MAX_GB * 1024 ** 3
if cap <= 0:
return
used = _dir_size(UPLOAD_DIR)
if used <= cap:
return
files = []
for name in os.listdir(UPLOAD_DIR):
p = os.path.join(UPLOAD_DIR, name)
if os.path.isfile(p) and not _job_is_active(_upload_job_id(name)):
try:
files.append((os.path.getmtime(p), p, os.path.getsize(p)))
except OSError:
pass
files.sort()
print(f"🧹 Uploads at {used / 1024**3:.1f} GB (cap {UPLOADS_MAX_GB} GB) — trimming.")
for _mtime, path, size in files:
if used <= cap:
break
try:
os.remove(path)
used -= size
print(f"🧹 Size cap: removed upload {os.path.basename(path)}")
except OSError:
pass
def _enforce_output_size_cap():
"""Delete the oldest job dirs while OUTPUT_DIR is over OUTPUT_MAX_GB."""
cap = OUTPUT_MAX_GB * 1024 ** 3
@ -1154,7 +1119,6 @@ async def cleanup_jobs():
# under the cap — clips are already archived to R2 and get restored
# on demand, so this only costs a re-download.
_enforce_output_size_cap()
_enforce_uploads_size_cap()
# Agent upload slots: expire with their file (the file sweep below
# removes it; a slot whose file is gone or too old is dropped).
@ -2501,7 +2465,7 @@ async def process_endpoint(
detail=f"{name} must be between {lo:g} and {hi:g}")
return int(val) if integer else val
n_clips = _gen_control(target_clips, "target_clips", 1, 15, integer=True)
n_clips = _gen_control(target_clips, "target_clips", 1, 31, integer=True)
min_secs = _gen_control(clip_min_seconds, "clip_min_seconds", 5, 175)
max_secs = _gen_control(clip_max_seconds, "clip_max_seconds", 10, 180)
if min_secs is not None and max_secs is not None and max_secs < min_secs + 5:

View file

@ -413,7 +413,7 @@ export default function MediaInput({ onProcess, isProcessing }) {
<div>
<p className="eyebrow mb-1.5">clips to aim for</p>
<input
type="number" min="1" max="15" step="1"
type="number" min="1" max="31" step="1"
value={targetClips}
onChange={(e) => setTargetClips(e.target.value)}
placeholder="auto"

View file

@ -261,7 +261,29 @@ async def test_target_clips_validation(client, media_factory):
{"file": ("clip.mp4", media_factory.six.read_bytes(), "video/mp4")},
)
assert out_of_range.status_code == 400
assert "target_clips must be between 1 and 15" in out_of_range.json()["detail"]
assert "target_clips must be between 1 and 31" in out_of_range.json()["detail"]
async def test_target_clips_accepts_raised_ceiling(
client, app_state, media_factory):
# The ceiling was raised from 15 to 31; the top of the new band must queue
# with the override the selector reads, and 32 must still be rejected.
resp = await _submit_file(
client,
{"acknowledged": "true", "target_clips": "31"},
{"file": ("clip.mp4", media_factory.six.read_bytes(), "video/mp4")},
)
job_id = _job(client, resp)
env = app_state.app.jobs[job_id]["env"]
assert env["CLIP_TARGET_MIN"] == "31" and env["CLIP_TARGET_MAX"] == "31"
too_many = await _submit_file(
client,
{"acknowledged": "true", "target_clips": "32"},
{"file": ("clip.mp4", media_factory.six.read_bytes(), "video/mp4")},
)
assert too_many.status_code == 400
assert "target_clips must be between 1 and 31" in too_many.json()["detail"]
async def test_clip_range_validation(client, media_factory):

View file

@ -117,12 +117,3 @@ class TestUploadSweep:
app_module.jobs["done"] = {"status": "failed"}
app_module._sweep_expired_uploads(time.time())
assert not f.exists()
def test_size_cap_skips_live_sources(self, disk, monkeypatch):
_, up = disk
live = _upload(up, "live")
app_module._running_jobs.add("live")
dead = _upload(up, "dead")
monkeypatch.setattr(app_module, "UPLOADS_MAX_GB", 1 / 1024 ** 3)
app_module._enforce_uploads_size_cap()
assert live.exists() and not dead.exists()