Run transcode jobs on several workers at once
Database · PostgreSQL · advanced · modification
Transcoding is the slowest step after an upload, and one worker thread can no longer keep up at peak. This PR lets the worker scale out: `main()` starts `WORKER_CONCURRENCY` threads (default 4) through the existing `run_threads` supervisor, and the deployment change to 3 worker pods ships with it (infra repo). Since several workers now read the same queue, the query that picks the next job claims it: `FOR UPDATE SKIP LOCKED` locks the oldest queued row and lets the other workers skip past it to the next one, so jobs are spread across workers. Everything else in `run_once` (marking a job `done` or `failed`) is unchanged. Adds worker tests in `tests/test_transcode_worker.py`.
Video platform worker, Python 3.12 with psycopg 3.2 (plain SQL, no ORM) against one PostgreSQL 16 primary at the default isolation level, READ COMMITTED. `app.db.worker_connection()` (existing) returns a dedicated `psycopg.connect(DATABASE_URL, autocommit=True)` connection; every worker in this codebase uses autocommit connections. `app.supervisor.run_threads(target, count, stop)` (existing) runs `target(stop)` in `count` threads and starts a call again after logging it when it raises. `app.transcoder.transcode(video_id, preset)` (existing) takes from about 10 seconds to at most 3 minutes, writes the output to object storage under a key derived from `video_id` and `preset` (overwriting), and raises `transcoder.TranscodeError` for every failure of the job itself (unreadable input, unsupported codec, upload rejected); any other exception means the worker itself broke. Table `transcode_jobs` (earlier migration): `id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY`, `video_id bigint NOT NULL`, `preset text NOT NULL`, `status text NOT NULL DEFAULT 'queued'` with `CHECK (status IN ('queued', 'done', 'failed'))`, `error text` nullable, `created_at timestamptz NOT NULL DEFAULT now()`, `finished_at timestamptz` nullable, and a partial index on `(created_at, id) WHERE status = 'queued'`. The upload API inserts a queued job per (video, preset); nothing else changes a job's row. `WORKER_CONCURRENCY` is set by the worker deployment manifest to 4 on every pod; the supported range is 1 to 4. The worker database role has `idle_in_transaction_session_timeout = '5min'`, longer than any job, and the team accepts one open transaction per busy worker thread (at most 12). The test suite (`tests/conftest.py`, existing) builds a fresh test database from the migrations, empties `transcode_jobs` before each test, and provides `enqueue_job(video_id, preset="720p")` (returns the job id) and `fetch_one(sql, params)`.
Requirements
- The transcode worker runs `WORKER_CONCURRENCY` threads per pod (default 4), and the worker deployment goes from 1 pod to 3. Every thread takes jobs from the same `transcode_jobs` table, oldest first (`created_at`, then `id`).
- A job runs on one worker at a time: while a worker is transcoding a job, no other thread or pod starts the same job. Idle workers move on to the next queued job instead.
- The queue keeps its existing design: a job has no `running` status. A worker holds its claim on a job as a lock on the job's row for as long as the job runs, so when a worker dies mid-job (pod killed, connection lost) Postgres releases the claim and another worker runs the job again. Running a job again after a crash is safe (the output key is derived from the video and preset and is overwritten).
- A job that ends in `TranscodeError` is marked `failed` with the error text (at most 500 characters) and is not retried; a job that succeeds is marked `done`. Both set `finished_at`.
Files touched
- app/transcode_worker.py
- tests/test_transcode_worker.py