style: apply ruff format across backend + scripts (line-length 100)
Mechanical, whole-tree `ruff format` pass — no behaviour change. Recorded in .git-blame-ignore-revs (next commit) so git blame skips it.
This commit is contained in:
+35
-12
@@ -15,6 +15,7 @@ Design:
|
||||
yt-dlp download if it's no longer `running`.
|
||||
* Crash-safe: on startup, orphaned `running` jobs are requeued and `downloading` assets reset.
|
||||
"""
|
||||
|
||||
import logging
|
||||
import os
|
||||
import shutil
|
||||
@@ -53,6 +54,7 @@ def _handle_signal(signum, frame):
|
||||
|
||||
# --- claim + recovery -----------------------------------------------------------------------
|
||||
|
||||
|
||||
def _wait_for_schema(timeout: float = 300.0) -> bool:
|
||||
"""Block until the download tables exist before doing any DB work. Returns False if they never
|
||||
appeared (or we were asked to stop while waiting).
|
||||
@@ -74,7 +76,8 @@ def _wait_for_schema(timeout: float = 300.0) -> bool:
|
||||
log.error(
|
||||
"download schema still absent after %ss — the API's migrations have not landed; "
|
||||
"exiting so the container restarts: %s",
|
||||
timeout, str(exc)[:120],
|
||||
timeout,
|
||||
str(exc)[:120],
|
||||
)
|
||||
return False
|
||||
log.info("waiting for the database schema (API migrations)…")
|
||||
@@ -135,9 +138,7 @@ def _claim_job() -> int | None:
|
||||
),
|
||||
{"dmc": default_mc},
|
||||
).fetchall()
|
||||
chosen = next(
|
||||
(jid for jid, uid in rows if not quota.at_concurrency_limit(db, uid)), None
|
||||
)
|
||||
chosen = next((jid for jid, uid in rows if not quota.at_concurrency_limit(db, uid)), None)
|
||||
if chosen is None:
|
||||
db.commit() # release the row locks; nothing eligible right now
|
||||
return None
|
||||
@@ -308,7 +309,12 @@ def _run_supervised(job_id: int, asset_id: int) -> None:
|
||||
except Exception: # noqa: BLE001 — a watchdog error must NEVER leave the child running unsupervised
|
||||
log.exception("supervisor loop failed for job %s; stopping the child", job_id)
|
||||
_fail_after_kill(
|
||||
job_id, asset_id, proc, grace_s, "The download was stopped after a worker error.", "unknown"
|
||||
job_id,
|
||||
asset_id,
|
||||
proc,
|
||||
grace_s,
|
||||
"The download was stopped after a worker error.",
|
||||
"unknown",
|
||||
)
|
||||
return
|
||||
|
||||
@@ -324,24 +330,37 @@ def _run_supervised(job_id: int, asset_id: int) -> None:
|
||||
if status == "running":
|
||||
_cleanup_staging(asset_id)
|
||||
_set_job(
|
||||
job_id, status="error", error_code="unknown", phase=None,
|
||||
job_id,
|
||||
status="error",
|
||||
error_code="unknown",
|
||||
phase=None,
|
||||
error=f"The download process exited unexpectedly (code {proc.returncode}).",
|
||||
)
|
||||
log.warning("job %s: child exited %s without finishing", job_id, proc.returncode)
|
||||
log.warning(
|
||||
"job %s: child exited %s without finishing", job_id, proc.returncode
|
||||
)
|
||||
except Exception: # noqa: BLE001 — boot recovery is the backstop for a stuck 'running' job
|
||||
log.exception("post-exit reconciliation failed for job %s", job_id)
|
||||
return
|
||||
|
||||
if reason == "stalled":
|
||||
_fail_after_kill(
|
||||
job_id, asset_id, proc, grace_s,
|
||||
f"The download stalled (no progress for {int(stall_s)}s) and was stopped.", "network",
|
||||
job_id,
|
||||
asset_id,
|
||||
proc,
|
||||
grace_s,
|
||||
f"The download stalled (no progress for {int(stall_s)}s) and was stopped.",
|
||||
"network",
|
||||
)
|
||||
return
|
||||
if reason == "timeout":
|
||||
_fail_after_kill(
|
||||
job_id, asset_id, proc, grace_s,
|
||||
f"The download exceeded the {int(wall_s)}s time limit and was stopped.", "network",
|
||||
job_id,
|
||||
asset_id,
|
||||
proc,
|
||||
grace_s,
|
||||
f"The download exceeded the {int(wall_s)}s time limit and was stopped.",
|
||||
"network",
|
||||
)
|
||||
return
|
||||
# cancelled / abandon / shutdown: stop + free the asset, but leave the job's own DB status.
|
||||
@@ -349,7 +368,9 @@ def _run_supervised(job_id: int, asset_id: int) -> None:
|
||||
if reason == "shutdown":
|
||||
_requeue_if_running(job_id, None) # resume on the next boot
|
||||
elif reason == "abandon":
|
||||
log.warning("job %s: killed a child wedged past the wall-clock; DB status left intact", job_id)
|
||||
log.warning(
|
||||
"job %s: killed a child wedged past the wall-clock; DB status left intact", job_id
|
||||
)
|
||||
else: # cancelled
|
||||
log.info("job %s: child stopped (route pause/cancel)", job_id)
|
||||
|
||||
@@ -363,6 +384,7 @@ def _cleanup_staging(asset_id: int) -> None:
|
||||
|
||||
# --- processing -----------------------------------------------------------------------------
|
||||
|
||||
|
||||
def _process_job(job_id: int) -> None:
|
||||
"""Dispatch a claimed job. The trivial outcomes (cache hit, dead/contended asset) are handled
|
||||
inline and cheaply; the actual heavy work (yt-dlp download / ffmpeg edit) is claimed here and
|
||||
@@ -423,6 +445,7 @@ def _finish_from_cache(job_id: int, asset_id: int) -> None:
|
||||
|
||||
# --- loop -----------------------------------------------------------------------------------
|
||||
|
||||
|
||||
def _worker_loop(idx: int) -> None:
|
||||
while not _stop.is_set():
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user