diff --git a/backend/app/worker.py b/backend/app/worker.py index a42a178..86f1086 100644 --- a/backend/app/worker.py +++ b/backend/app/worker.py @@ -76,6 +76,26 @@ def _parse_upload_date(raw) -> date | None: # --- claim + recovery ----------------------------------------------------------------------- +def _wait_for_schema(timeout: float = 300.0) -> None: + """Block until the download tables exist before doing any DB work. + + The API applies migrations on its OWN startup, and the worker is a separate container that may + boot first (they start in parallel). Without this, the worker would hit a missing `download_jobs` + table and crash-loop until the API's migrations land. Poll patiently instead.""" + deadline = time.monotonic() + timeout + while not _stop.is_set(): + try: + with SessionLocal() as db: + db.execute(text("SELECT 1 FROM download_jobs LIMIT 1")) + return + except Exception as exc: # noqa: BLE001 — table not migrated yet / DB not up yet + if time.monotonic() > deadline: + log.warning("download schema still absent after %ss; continuing: %s", timeout, str(exc)[:120]) + return + log.info("waiting for the database schema (API migrations)…") + _stop.wait(3.0) + + def _recover_orphans() -> None: """Reset state left behind by a previous crash so nothing is stuck.""" with SessionLocal() as db: @@ -573,6 +593,9 @@ def main() -> None: log.info("WORKER_ENABLED is off; worker exiting (nothing to do).") return + _wait_for_schema() # the API may still be applying migrations when we boot + if _stop.is_set(): + return _recover_orphans() n = max(1, settings.download_worker_concurrency) log.info("Download worker started (concurrency=%d, root=%s).", n, settings.download_root) diff --git a/docker-compose.selfhost.yml b/docker-compose.selfhost.yml index fe96969..c159f68 100644 --- a/docker-compose.selfhost.yml +++ b/docker-compose.selfhost.yml @@ -84,6 +84,10 @@ services: depends_on: db: condition: service_healthy + # Start only after the API is healthy (it applies the DB migrations on startup), so the + # download tables exist before the worker touches them. + api: + condition: service_healthy bgutil-pot: condition: service_started networks: [internal] diff --git a/docker-compose.yml b/docker-compose.yml index 1c26e30..a7fa989 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -66,6 +66,9 @@ services: depends_on: db: condition: service_healthy + # Start only after the API is healthy (it applies the DB migrations on startup). + api: + condition: service_healthy bgutil-pot: condition: service_started volumes: