Files
karaoke-gen/.github/workflows
Andrew Beveridge ☄️andClaude Fable 5 3cdb62a058 fix: run render-video worker as Cloud Run Job to survive backend deploys (#994)
## Summary

Tenant E2E run #107 (`singa`) failed with job `41e06b90` frozen at
`rendering_video` — even though the GCE encoder **finished the render
and uploaded every output**. Root cause: a backend deploy landed
mid-render and killed the render poll, which ran as a FastAPI
BackgroundTask on the Cloud Run service.

Full investigation:
`docs/archive/2026-09-13-render-deploy-survival-investigation.md`

### The failure chain (4 layers, each structurally broken)

1. **BackgroundTask execution** — the render endpoint returns 200
immediately and polls post-response; Cloud Run SIGKILLs old-revision
instances ~10s after SIGTERM on every deploy.
2. **Unreachable shutdown parking** — uvicorn waits for background tasks
*before* running lifespan shutdown, so
`park_active_render_jobs_for_shutdown()` never runs in exactly the
scenario it was written for. Its comment claimed a 600s grace period;
Cloud Run gives 10s. Verified in logs: the old revision's last lines
were uvicorn's `Waiting for background tasks to complete.` — then
SIGKILL.
3. **Cloud Tasks retry defeated** — the queue got its 200 at dispatch;
`dispatch_deadline=1800s` protects nothing.
4. **45-min stuck sweep** — reachable but slower than the E2E's 30-min
budget and a ~50-60 min silent stall + wasted re-render for a real
customer, on every deploy that lands mid-render (deploys go out on every
merge to main).

This is the **identical bug class fixed for the video worker in
2026-03** (`docs/archive/2026-03-08-fix-encoding-deploy-survival.md`,
`USE_CLOUD_RUN_JOBS_FOR_VIDEO`) — the render worker was never migrated.

### Changes

- **Route `trigger_render_video_worker` through a Cloud Run Job** —
reuses `video-encoding-job` with args override `python -m
backend.workers.render_video_worker --job-id <id>`, behind
`USE_CLOUD_RUN_JOBS_FOR_RENDER` (enabled in deploy env). Job executions
run to completion; deploys never touch them.
- **New CLI entrypoint** in `render_video_worker.py`. Exit-code contract
differs from video_worker deliberately: clean returns (True AND False)
exit 0 because `process_render_video` parks/fails/supersedes internally;
only an escaped crash exits 1 (job template has `max_retries=2`).
- **Worker-side idempotency gate** (review finding): the CRJ path
bypasses the HTTP endpoint's `_check_worker_idempotency`, and CRJ
retries could re-enter a job in `RENDERING_VIDEO` (hard-failing it via
the same-state transition guard) or resurrect a terminal job. The worker
now skips terminal + already-rendering jobs cleanly regardless of
dispatch path.
- **Surface failed video-worker dispatch** (review finding): the ignored
`trigger_video_worker` return after render completion now logs at ERROR
instead of silently stalling the job at `instrumental_selected`.
- **Pulumi**: `video-encoding-job` env gains both
`USE_CLOUD_RUN_JOBS_FOR_*` flags so the render execution's in-process
video-worker trigger also dispatches a CRJ. **Already applied to prod**
(`pulumi up` run locally, verified on the job).
- **`main.py`**: corrected the false 600s comment; shrank the
unreachable 480s lifespan wait to 5s. Parking hook kept as
legacy/flag-off net.

### Rollback

Remove `USE_CLOUD_RUN_JOBS_FOR_RENDER=true` from the service env → falls
back to the legacy Cloud Tasks path.

## Testing

- Backend: 4422 unit + 151 emulator tests pass locally.
- New tests: CRJ routing (flag on/off), CLI exit-code contract,
worker-side idempotency gate (terminal skip, already-rendering skip).
- Post-deploy: will dispatch the Tenant Portals E2E workflow (the
failing scenario) against prod.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **New Features**
- Video rendering now runs as durable background jobs, allowing renders
to continue through service deployments.
- Added safeguards to prevent duplicate processing and safely handle
completed or in-progress jobs.
- Improved failure reporting when a video-processing job cannot be
started.

- **Bug Fixes**
- Fixed render jobs becoming stuck when deployments interrupted
background processing.

- **Documentation**
- Added troubleshooting guidance and incident lessons for interrupted or
stuck renders.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-09-13 03:02:15 -04:00
..