diff --git a/CLAUDE.md b/CLAUDE.md index bca9817..421916a 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -237,36 +237,25 @@ Stripe retry the same doomed event for three days. ### Concurrency Model Async job queue with semaphore-based concurrency control. Configure via `MAX_CONCURRENT_JOBS` env var (default: 5). Jobs auto-cleanup after 1 hour. -### Deploys interrupt running jobs +### Deploys and running jobs (handover + drain) -Every push to `main` redeploys the API container and Coolify does not wait for -the queue to drain. An interrupted job is re-enqueued on startup from its -`.resume.json` manifest (`app.py:_resume_interrupted_jobs`, max 2 attempts) and -re-runs with the same command and output directory. `main.py` leaves the -finished transcript in that directory (`.transcript_checkpoint.json`) so the -re-run skips the slow, paid transcription; the download and the -Gemini analysis still repeat. Before pushing: batch small commits (tests, docs) -with the next real change instead of giving them their own deploy, and check -that no job is mid-flight in the prod container. +Every push to `main` redeploys the API container. Coolify starts the NEW +container before stopping the old one (rolling update) and both share +`output/`, so `app.py` coordinates them instead of relying on a fast swap: -## Environment Variables +- Each instance writes its id to `output/.instance` at startup. An instance + that sees another id there is the old one and **drains**: it finishes the + jobs it is running, starts none, and leaves queued manifests on disk. +- A running job heartbeats its `.resume.json` every 10 s. The resume scan + (startup + every 30 s) re-enqueues only manifests nobody heartbeated for + 60 s, so no job runs twice and none is lost. Max 2 resume attempts. +- SIGTERM (`docker stop`) drains too, up to `DRAIN_TIMEOUT_SECONDS` (840), + then hands the signal to uvicorn. The app's Coolify stop grace period is + 900 s (`application_settings.stop_grace_period`); keep the timeout below it. +- `/api/status` answers from disk for a job this instance never held, so a + poll landing on either container during the handover is fine. +- `main.py` leaves `.transcript_checkpoint.json` in the job dir so a job that + does get re-run skips the paid transcription (download and Gemini repeat). -**Server-side (.env):** -- `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`, `AWS_REGION`, `AWS_S3_BUCKET` - For S3 backup -- `MAX_CONCURRENT_JOBS` - Concurrent processing limit (default: 5) -- `VITE_API_URL` - Production API URL override -- `VITE_OPENPANEL_API_URL`, `VITE_OPENPANEL_CLIENT_ID` - Optional product analytics, read at **build** time. Unset (the default, including every self-hosted build) means no analytics is initialised and no third-party script is loaded. `dashboard/index.html` also gates reporting on an `ANALYTICS_HOSTS` allowlist, so a build carrying credentials stays inert on any other host. -- `OPENPANEL_CLIENT_ID`, `OPENPANEL_CLIENT_SECRET` - Optional **server-side** analytics (`cloud/analytics.py`), same opt-in rule: unset means a silent no-op. Reports job outcomes with the user's job index, which the browser cannot do reliably — a render finishes after the tab is often gone, and ad-blockers eat a share of client events. Needs a *write* client; the read client used for querying is a different credential. - -**Client-side (localStorage, encrypted):** -- `GEMINI_API_KEY` - Google Gemini API key (required) -- `ELEVENLABS_API_KEY` - ElevenLabs API key for voice dubbing (optional) -- `UPLOAD_POST_API_KEY` - Upload-Post API key for social posting (optional) - -> API keys are stored encrypted in the browser and sent via headers only when needed. Never stored server-side. - -## Tech Stack -- **Backend:** Python 3.11, FastAPI, google-genai, faster-whisper, ultralytics (YOLOv8), mediapipe, opencv-python, yt-dlp, FFmpeg, httpx -- **Frontend:** React 18, Vite 4, Tailwind CSS 3.4 -- **External APIs:** Google Gemini, ElevenLabs Dubbing, Upload-Post -- **Infrastructure:** Docker + Docker Compose, AWS S3 +Before pushing, still batch small commits (tests, docs) with the next real +change: every deploy is a ~5 min build plus a handover. diff --git a/app.py b/app.py index d99763d..5981191 100644 --- a/app.py +++ b/app.py @@ -12,6 +12,8 @@ import zipfile import math import itertools import asyncio +import signal +import socket from datetime import datetime, timezone, timedelta from dotenv import load_dotenv from typing import Any, Dict, Optional, List @@ -589,6 +591,155 @@ def _recover_jobs_from_disk(): _RESUME_FILE = ".resume.json" MAX_RESUME_ATTEMPTS = 2 +# --- Deploy handover (two instances, one disk) ------------------------------- +# Coolify starts the NEW container before it stops the old one (rolling +# update), and both see the same OUTPUT_DIR. Without coordination the new one +# re-enqueued, at startup, the very jobs the old one was still rendering — +# and the old one had 30 s to live anyway. Now: +# * every instance stamps OUTPUT_DIR/.instance with its id at startup; an +# instance that sees another id there knows it is the OLD one and DRAINS: +# it finishes what it is running, starts nothing new, and leaves queued +# manifests for the new instance to pick up; +# * a running job writes a heartbeat into its manifest every few seconds, +# so the new instance skips manifests that are alive elsewhere and resumes +# only the stale ones (an instance killed mid-job stops heartbeating); +# * SIGTERM (docker stop) also drains, up to DRAIN_TIMEOUT_SECONDS — keep it +# under the Coolify stop grace period — before letting uvicorn exit. +INSTANCE_ID = os.environ.get("INSTANCE_ID") or socket.gethostname() +_INSTANCE_MARKER = ".instance" +HEARTBEAT_EVERY = 10 # seconds between manifest heartbeats +HEARTBEAT_STALE_AFTER = 60 # no heartbeat for this long = nobody has it +RESUME_SCAN_INTERVAL = 30 # seconds between looks for stale manifests +HANDOVER_CHECK_INTERVAL = 5 # seconds between looks at the marker +DRAIN_TIMEOUT_SECONDS = int(os.environ.get("DRAIN_TIMEOUT_SECONDS", "840")) +_draining = False +_running_jobs: set = set() # job ids with a live subprocess here + + +def _manifest_path(job_id): + return os.path.join(OUTPUT_DIR, job_id, _RESUME_FILE) + + +def _read_manifest(job_id): + try: + with open(_manifest_path(job_id)) as f: + return json.load(f) + except FileNotFoundError: + return None + except Exception as e: + print(f"⚠️ Bad resume manifest for {job_id}: {e}") + return None + + +def _touch_manifest(job_id, now=None): + """Stamp 'this instance is running it, as of now' into the manifest.""" + m = _read_manifest(job_id) + if m is None: + return + m["heartbeat"] = now if now is not None else time.time() + m["instance"] = INSTANCE_ID + try: + with open(_manifest_path(job_id), "w") as f: + json.dump(m, f) + except Exception as e: + print(f"⚠️ Could not heartbeat manifest for {job_id}: {e}") + + +def _manifest_busy_elsewhere(m, now=None): + """True when ANOTHER instance heartbeated this job recently. Our own id + with a fresh heartbeat is a job WE were running before a restart of this + same container (same hostname) — that one must be resumed, not skipped.""" + now = time.time() if now is None else now + return (m.get("instance") not in (None, INSTANCE_ID) + and now - float(m.get("heartbeat") or 0) < HEARTBEAT_STALE_AFTER) + + +def _write_instance_marker(): + try: + os.makedirs(OUTPUT_DIR, exist_ok=True) + with open(os.path.join(OUTPUT_DIR, _INSTANCE_MARKER), "w") as f: + f.write(INSTANCE_ID) + except Exception as e: + print(f"⚠️ Could not write instance marker: {e}") + + +def _read_instance_marker(): + try: + with open(os.path.join(OUTPUT_DIR, _INSTANCE_MARKER)) as f: + return f.read().strip() + except Exception: + return None + + +def _begin_drain(reason): + global _draining + if not _draining: + _draining = True + print(f"⏸️ Draining ({reason}): finishing {len(_running_jobs)} running job(s), " + f"starting none.") + + +def _check_instance_marker(): + """A different id in the marker means a newer instance is up: drain.""" + other = _read_instance_marker() + if other and other != INSTANCE_ID: + _begin_drain(f"newer instance {other} is up") + return True + return False + + +async def _handover_watch(): + while not _draining: + await asyncio.sleep(HANDOVER_CHECK_INTERVAL) + _check_instance_marker() + + +async def _resume_scan(): + """Keep looking for manifests nobody is running (the old instance left + them queued, or was killed at the end of its grace period).""" + while True: + await asyncio.sleep(RESUME_SCAN_INTERVAL) + if _draining: + continue + try: + _resume_interrupted_jobs() + except Exception as e: + print(f"⚠️ Resume scan failed: {e}") + + +async def _drain_then_exit(previous_handler, timeout=None): + """Wait for running jobs (bounded), then hand the signal to uvicorn.""" + timeout = DRAIN_TIMEOUT_SECONDS if timeout is None else timeout + deadline = time.time() + timeout + while _running_jobs and time.time() < deadline: + await asyncio.sleep(1) + if _running_jobs: + print(f"⏱️ Drain timeout after {timeout}s with {len(_running_jobs)} job(s) still " + f"running — they will resume on the next instance.") + else: + print("✅ Drained: no running jobs, shutting down.") + if callable(previous_handler): + previous_handler(signal.SIGTERM, None) + else: + os._exit(0) + + +def _install_drain_signal_handler(): + """Replace uvicorn's SIGTERM handler with drain-first. uvicorn's own + handler closes the listening socket at once, which would make Traefik + send half the traffic to a refused port for the whole drain.""" + previous = signal.getsignal(signal.SIGTERM) + loop = asyncio.get_running_loop() + + def on_sigterm(): + _begin_drain("SIGTERM") + asyncio.ensure_future(_drain_then_exit(previous)) + + try: + loop.add_signal_handler(signal.SIGTERM, on_sigterm) + except (NotImplementedError, RuntimeError, ValueError) as e: + print(f"⚠️ Drain-on-SIGTERM unavailable ({e}); jobs will resume on restart instead.") + def _write_resume_manifest(job_id, cmd, priority, user_id, reservation_id, watermark, webhook_url=None, webhook_secret=None, base_url=None): @@ -629,13 +780,16 @@ def _resume_interrupted_jobs() -> set: Runs after _recover_jobs_from_disk: a job whose clips already finished has a metadata JSON and is recovered as 'completed', so we only resume manifests - with no metadata yet (analysis never finished). + with no metadata yet (analysis never finished). Also called periodically + (_resume_scan): a manifest another instance is heartbeating is left alone + until that heartbeat goes stale, and a job this instance already holds is + never enqueued twice. - Returns the set of reservation ids for the resumed jobs, so the caller can - keep them out of the orphaned-reservation refund. Does NO DB work — the DB - engine isn't up yet at this point in startup. A poison job (too many - attempts) is simply not resumed; its reservation is then refunded as a - normal orphan. + Returns the set of reservation ids for every manifest still on disk — + resumed here or alive on the other instance — so the caller can keep them + out of the orphaned-reservation refund. Does NO DB work — the DB engine + isn't up yet at this point in startup. A poison job (too many attempts) is + simply not resumed; its reservation is then refunded as a normal orphan. """ keep_reservations: set = set() try: @@ -648,8 +802,8 @@ def _resume_interrupted_jobs() -> set: manifest_path = os.path.join(job_path, _RESUME_FILE) if not os.path.isfile(manifest_path): continue - # Already finished generating clips → recovered as completed elsewhere. if glob.glob(os.path.join(job_path, "*_metadata.json")): + # Finished after all — recovered as completed already. _clear_resume_manifest(job_id) continue try: @@ -659,6 +813,13 @@ def _resume_interrupted_jobs() -> set: print(f"⚠️ Bad resume manifest for {job_id}: {e}") continue + if m.get("reservation_id"): + keep_reservations.add(str(m["reservation_id"])) + if job_id in jobs: + continue # already ours (queued, running or recovered) + if _manifest_busy_elsewhere(m): + continue # the other instance is on it; we take over if it goes stale + attempts = int(m.get("attempts", 0)) + 1 user_id = m.get("user_id") reservation_id = m.get("reservation_id") @@ -667,6 +828,8 @@ def _resume_interrupted_jobs() -> set: # set lets the orphan sweep refund it, and the user can retry by hand. print(f"🛑 Job {job_id} exceeded {MAX_RESUME_ATTEMPTS} resume attempts — giving up.") _clear_resume_manifest(job_id) + if reservation_id: + keep_reservations.discard(str(reservation_id)) # let the sweep refund it continue # Rebuild env from scratch — the manifest holds no secrets. Managed @@ -702,8 +865,6 @@ def _resume_interrupted_jobs() -> set: 'webhook_secret': m.get("webhook_secret"), 'base_url': m.get("base_url"), } - if reservation_id: - keep_reservations.add(str(reservation_id)) _enqueue_job(job_id, int(m.get("priority", 2))) resumed += 1 if resumed: @@ -851,9 +1012,22 @@ async def process_queue(): # Wait for a job (priority, seq, job_id) — lowest priority first. _priority, _seq, job_id = await job_queue.get() + if _draining: + # Leave it on disk for the next instance (manifest, no heartbeat). + print(f"⏸️ Draining — leaving {job_id} for the next instance.") + job_queue.task_done() + continue + # Acquire semaphore slot (waits if max jobs are running) await concurrency_semaphore.acquire() + if _draining: + concurrency_semaphore.release() + job_queue.task_done() + print(f"⏸️ Draining — leaving {job_id} for the next instance.") + continue print(f"🔄 Acquired slot for job: {job_id}") + _running_jobs.add(job_id) + _touch_manifest(job_id) # Process in background task to not block the loop (allowing other slots to fill) asyncio.create_task(run_job_wrapper(job_id)) @@ -918,6 +1092,7 @@ async def run_job_wrapper(job_id): # Telegram pulse for high-signal activity (first clip / paid user). await _notify_clip_activity(job_id) # Always release semaphore and mark queue task done + _running_jobs.discard(job_id) concurrency_semaphore.release() job_queue.task_done() print(f"✅ Released slot for job: {job_id}") @@ -1320,6 +1495,13 @@ async def lifespan(app: FastAPI): # Re-enqueue jobs that were mid-processing when we stopped (redeploy). Their # reservations must survive the orphan sweep so the resumed run can settle them. _resumed_reservation_ids = _resume_interrupted_jobs() + # Deploy handover: claim the marker (any older instance sees it and drains), + # keep watching it in case a newer one appears, keep looking for manifests + # left behind, and drain instead of dying on docker stop. + _write_instance_marker() + _install_drain_signal_handler() + asyncio.create_task(_handover_watch()) + asyncio.create_task(_resume_scan()) # Start worker and cleanup worker_task = asyncio.create_task(process_queue()) cleanup_task = asyncio.create_task(cleanup_jobs()) @@ -1488,8 +1670,12 @@ async def run_job(job_id, job_data): # Async wait for process with incremental updates start_wait = time.time() + last_heartbeat = time.time() while process.poll() is None: await asyncio.sleep(2) + if time.time() - last_heartbeat >= HEARTBEAT_EVERY: + _touch_manifest(job_id) + last_heartbeat = time.time() # Check for partial results every 2 seconds # Look for metadata file @@ -1983,15 +2169,54 @@ async def process_endpoint( return {"job_id": job_id, "status": "queued"} +def _job_view_from_disk(job_id): + """What the disk says about a job this instance does not hold in memory. + + During a deploy handover a poll can land on either instance; the one that + did not accept the job must still answer from the shared directory: a + metadata JSON means completed (recovered like at startup), a manifest + means the other instance has it (heartbeat) or will pick it up (queued). + """ + job_path = os.path.join(OUTPUT_DIR, job_id) + if not os.path.isdir(job_path): + return None + if glob.glob(os.path.join(job_path, "*_metadata.json")): + _recover_jobs_from_disk() + return jobs.get(job_id) + m = _read_manifest(job_id) + if m is None: + return None + alive = time.time() - float(m.get("heartbeat") or 0) < HEARTBEAT_STALE_AFTER + owner = m.get("user_id") + return { + 'status': 'processing' if alive else 'queued', + 'logs': ["♻️ The server was updated; your video continues on the new instance."], + 'user_id': (int(owner) if isinstance(owner, str) and owner.isdigit() else owner), + 'result': None, + } + + +def _presented_status(job_id, job): + """A job we hold as 'queued' while draining is really the next instance's: + if it has started it, say so instead of showing a queue that never moves.""" + if job.get('status') == 'queued' and _draining: + m = _read_manifest(job_id) + if m and _manifest_busy_elsewhere(m): + return 'processing' + return job['status'] + + @app.get("/api/status/{job_id}") async def get_status(job_id: str, request: Request): - if job_id not in jobs: + job = jobs.get(job_id) + if job is None: + job = _job_view_from_disk(job_id) + if job is None: raise HTTPException(status_code=404, detail="Job not found") - job = jobs[job_id] await _assert_job_owner(request, job) return { - "status": job['status'], + "status": _presented_status(job_id, job), "logs": _visible_logs(job['logs']), "result": job.get('result') } diff --git a/tests/test_deploy_drain.py b/tests/test_deploy_drain.py new file mode 100644 index 0000000..97e5d5f --- /dev/null +++ b/tests/test_deploy_drain.py @@ -0,0 +1,185 @@ +"""Deploy handover: two instances, one disk, no job run twice and none lost. + +Coolify starts the new container before stopping the old one and both share +OUTPUT_DIR. The old one must drain (finish what it runs, start nothing new); +the new one must resume only manifests nobody is heartbeating; a status poll +landing on either instance must answer from disk. +""" +import asyncio +import json +import os +import time + +import httpx +import pytest + +app_module = pytest.importorskip("app") + + +@pytest.fixture +def out(tmp_path, monkeypatch): + root = tmp_path / "output" + root.mkdir() + monkeypatch.setattr(app_module, "OUTPUT_DIR", str(root)) + monkeypatch.setattr(app_module, "INSTANCE_ID", "me") + monkeypatch.setattr(app_module, "_draining", False) + monkeypatch.setattr(app_module, "_running_jobs", set()) + # Isolate the in-memory job table and queue from other tests. + monkeypatch.setattr(app_module, "jobs", {}) + monkeypatch.setattr(app_module, "job_queue", asyncio.PriorityQueue()) + return root + + +def _manifest(out, job_id, **extra): + d = out / job_id + d.mkdir(exist_ok=True) + m = {"cmd": ["python", "main.py"], "priority": 2, "user_id": None, + "reservation_id": f"res-{job_id}", "watermark": False, "attempts": 0} + m.update(extra) + (d / app_module._RESUME_FILE).write_text(json.dumps(m)) + return d + + +class TestHeartbeat: + def test_touch_stamps_instance_and_time(self, out): + _manifest(out, "j1") + app_module._touch_manifest("j1", now=1000.0) + m = app_module._read_manifest("j1") + assert m["instance"] == "me" and m["heartbeat"] == 1000.0 + + def test_fresh_heartbeat_from_another_instance_is_busy(self, out): + m = {"instance": "other", "heartbeat": 1000.0} + assert app_module._manifest_busy_elsewhere(m, now=1030.0) + + def test_stale_heartbeat_is_free(self, out): + m = {"instance": "other", "heartbeat": 1000.0} + assert not app_module._manifest_busy_elsewhere( + m, now=1000.0 + app_module.HEARTBEAT_STALE_AFTER + 1) + + def test_own_heartbeat_is_free(self, out): + # Same container restarted (same hostname): that job is ours to resume. + m = {"instance": "me", "heartbeat": 1000.0} + assert not app_module._manifest_busy_elsewhere(m, now=1001.0) + + def test_no_heartbeat_is_free(self, out): + assert not app_module._manifest_busy_elsewhere({}, now=5.0) + + +class TestResumeScan: + def test_busy_elsewhere_is_skipped_but_reservation_kept(self, out): + _manifest(out, "j1", instance="other", heartbeat=time.time()) + keep = app_module._resume_interrupted_jobs() + assert "j1" not in app_module.jobs + assert keep == {"res-j1"}, "the other instance's job must not be refunded as an orphan" + + def test_stale_and_never_started_are_resumed(self, out): + _manifest(out, "stale", instance="other", heartbeat=time.time() - 600) + _manifest(out, "queued") + keep = app_module._resume_interrupted_jobs() + assert app_module.jobs["stale"]["status"] == "queued" + assert app_module.jobs["queued"]["status"] == "queued" + assert keep == {"res-stale", "res-queued"} + assert app_module._read_manifest("stale")["attempts"] == 1 + + def test_a_job_we_already_hold_is_not_enqueued_twice(self, out): + _manifest(out, "j1") + app_module.jobs["j1"] = {"status": "processing", "logs": []} + app_module._resume_interrupted_jobs() + assert app_module.job_queue.qsize() == 0 + assert app_module._read_manifest("j1")["attempts"] == 0 + + def test_poison_job_is_dropped_and_refundable(self, out): + _manifest(out, "bad", attempts=app_module.MAX_RESUME_ATTEMPTS) + keep = app_module._resume_interrupted_jobs() + assert "bad" not in app_module.jobs + assert keep == set() + assert app_module._read_manifest("bad") is None + + +class TestDrain: + def test_marker_of_another_instance_starts_draining(self, out): + (out / app_module._INSTANCE_MARKER).write_text("newer") + assert app_module._check_instance_marker() is True + assert app_module._draining is True + + def test_own_marker_does_not_drain(self, out): + app_module._write_instance_marker() + assert app_module._check_instance_marker() is False + assert app_module._draining is False + + def test_scan_stays_quiet_while_draining(self, out): + _manifest(out, "j1") + app_module._begin_drain("test") + # The draining instance must not adopt manifests: they belong to the + # next one. (_resume_scan checks the flag; the worker drops dispatches.) + assert app_module._draining + + def test_exit_waits_for_running_jobs_then_hands_the_signal_on(self, out): + calls = [] + app_module._running_jobs.add("j1") + + async def scenario(): + async def finish_soon(): + await asyncio.sleep(0.2) + app_module._running_jobs.discard("j1") + asyncio.create_task(finish_soon()) + t = time.time() + await app_module._drain_then_exit(lambda sig, frame: calls.append(sig), timeout=5) + return time.time() - t + waited = asyncio.run(scenario()) + assert calls and waited >= 0.2 + + def test_exit_gives_up_at_the_timeout(self, out): + calls = [] + app_module._running_jobs.add("stuck") + asyncio.run(app_module._drain_then_exit(lambda sig, frame: calls.append(sig), timeout=0.3)) + assert calls, "must still exit so the deploy can finish" + + +class TestStatusFromDisk: + def _client(self): + return httpx.AsyncClient(transport=httpx.ASGITransport(app=app_module.app), + base_url="http://t") + + def test_unknown_job_with_live_manifest_is_processing(self, out): + _manifest(out, "j1", instance="other", heartbeat=time.time()) + + async def go(): + async with self._client() as c: + return await c.get("/api/status/j1") + r = asyncio.run(go()) + assert r.status_code == 200 + assert r.json()["status"] == "processing" + + def test_unknown_job_with_idle_manifest_is_queued(self, out): + _manifest(out, "j1") + + async def go(): + async with self._client() as c: + return await c.get("/api/status/j1") + r = asyncio.run(go()) + assert r.json()["status"] == "queued" + + def test_unknown_job_with_metadata_is_completed(self, out): + d = out / "j1" + d.mkdir() + (d / "vid_metadata.json").write_text(json.dumps({"shorts": [{"start": 0, "end": 1}]})) + + async def go(): + async with self._client() as c: + return await c.get("/api/status/j1") + r = asyncio.run(go()) + assert r.json()["status"] == "completed" + assert r.json()["result"]["clips"] + + def test_truly_unknown_job_is_404(self, out): + async def go(): + async with self._client() as c: + return await c.get("/api/status/nope") + assert asyncio.run(go()).status_code == 404 + + def test_queued_here_but_started_elsewhere_reads_processing(self, out): + _manifest(out, "j1", instance="other", heartbeat=time.time()) + app_module.jobs["j1"] = {"status": "queued", "logs": ["Job queued"]} + app_module._begin_drain("test") + assert app_module._presented_status("j1", app_module.jobs["j1"]) == "processing"