Commit graph

4 commits

Author SHA1 Message Date
victorcavero14
faf633ed43 fix(deploy): hold new jobs while the draining instance still fills the GPU
During a rolling deploy the old container finishes its running jobs on the
same GPU while the new one starts its own full MAX_CONCURRENT_JOBS. On
22-sep-2026 that was 7 old + 3 new jobs on a 20 GB card: TransNetV2 and
NVENC ran out of memory and 5 of 12 managed jobs failed. The new instance
now counts the jobs another instance is heartbeating and waits until the
total fits (bounded by the drain timeout; a killed instance goes stale in 60 s).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-22 22:18:09 +02:00
juancarlos.cavero
b308f6e00e fix(deploy): keep serving until the proxy drops us, then exit for real
- PROXY_DRAIN_SECONDS (20): after the jobs are drained, keep the socket
  open with /health/ready at 503 so Traefik pulls the instance out of
  rotation while it can still answer. Coolify builds the container's
  HEALTHCHECK from its own interval*retries, so closing earlier was up to
  60 s of alternating 502/200 per deploy (retries lowered to 3 as well).
- HARD_EXIT_SECONDS (30): uvicorn finishing does not end the interpreter
  while an executor thread hangs in a network probe (a /api/process cut
  off mid-shutdown left yt-dlp running), which kept a drained container
  alive for the full 900 s grace and the deploy in "Removing old
  containers". Exit outright a little after handing uvicorn the signal.
2026-08-25 15:11:55 +02:00
juancarlos.cavero
2935aca896 fix(deploy): pull a stopping container out of Traefik before its socket closes
- uvicorn --timeout-graceful-shutdown 15: after the drain, stop waiting
  forever on open range downloads. The old container lived the full 900 s
  stop grace with its port already closed, so Traefik alternated 502/200
  for 15 minutes on 2026-08-25.
- /health/ready answers 503 once SIGTERM arrives, and a Dockerfile
  HEALTHCHECK on it lets Traefik drop the instance within ~10 s while it
  can still serve. Coolify's health check now waits on the same path
  before stopping the old container. The marker drain does not flip it:
  the new container is still booting at that point.
- ignore .worktrees
2026-08-25 12:45:13 +02:00
juancarlos.cavero
b8ee43570e feat(api): drain on deploy and hand running jobs over between instances
Coolify starts the new container before stopping the old one and both share
output/, so the new one used to re-enqueue at startup the very jobs the old
one was still rendering, and the old one had 30 s to live regardless.

- Each instance stamps output/.instance; an instance that sees another id
  drains: finishes what it runs, starts nothing, leaves queued manifests.
- A running job heartbeats its manifest every 10 s; the resume scan (startup
  and every 30 s) adopts only manifests nobody heartbeated for 60 s, so no
  job runs twice and none is lost. Reservations of live-elsewhere jobs are
  kept out of the orphan refund; poison jobs stay refundable.
- SIGTERM drains too (up to DRAIN_TIMEOUT_SECONDS, default 840) before the
  signal reaches uvicorn, so the socket stays open and Traefik keeps getting
  answers. The app's Coolify stop grace period is now 900 s.
- /api/status answers from disk for a job this instance never held, so a
  poll landing on either container during the handover works.

Verified in the local stack: SIGTERM mid-job -> job finished its clips,
'Drained', exit 0; status polled 200 throughout an 8 min drain; a job
submitted during the drain was left for, and resumed by, the next instance.
2026-08-25 02:39:51 +02:00