Phase 4: scale out

Remote workers, Postgres and S3, all chosen by configuration.

- Pull-based worker protocol over HTTP: claim (long poll), heartbeat,
  complete, fail, upload, source, probe. Workers need no inbound ports
- cmd/worker: standalone stateless worker with health endpoint and
  scheduled yt-dlp self-update
- Per-worker bearer tokens, stored hashed, revocable; identity always
  taken from the token; actions only on jobs the worker holds a lease for
- Tags: site_tags routes a site's jobs and lookups to workers that carry
  the required tags
- Drain, /workers, /ytdlp update, and "core worker add|revoke|list"
- Link lookups can run on remote workers (PROBE_MODE), so the core needs
  no yt-dlp and no residential address
- Postgres backend with pgx, same schema and queries; per-user advisory
  lock and job row lock where SQLite's serial writes were relied on
- The whole test suite runs on SQLite and on Postgres
- S3 storage backend with presigned worker uploads, sharing a conformance
  suite with the local backend
- Dockerfile with core, worker and faketelegram targets, docker-compose
  with optional Postgres and local Bot API server, systemd units
- deploy/acceptance/kill-worker.sh: kills a worker mid-job and checks
  that the other one delivers the file
- Fixes found on the way: /cancel could let the next playlist item start;
  a refund note could be missing when the sweep refunded first

Docker was not available on the build machine: the compose files were
validated with "docker compose config" only. The acceptance scenario was
run with real processes.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
Michilis
2026-11-18 09:27:11 +00:00
co-authored by Claude Fable 5.2
parent 9c9d641eda
commit 7d4893bc3f
51 changed files with 4732 additions and 108 deletions
+86
View File
@@ -366,3 +366,89 @@ Read from the LNbits source at `v0.12.12` (the target) and `v1.7.1` (current).
- `/subs` lists written subtitles before automatic captions, with the
requester's language first and marked. At most 24 languages are shown.
- `/history` shows the last 10 delivered files.
## Phase 4
### What could and could not be run here
- **Docker was not available on the build machine.** The user that built
this has no access to the Docker socket, and rootless Docker is missing
its helpers. So `deploy/Dockerfile` and `docker-compose.yml` were written
and checked with `docker compose config` (which parses, merges and
interpolates them without a daemon), but **no image was built and no
container was run**. Treat the first `docker compose up --build` as the
real test of those files.
- **The acceptance scenario itself was run for real**, with the same
binaries as separate OS processes: one core, two workers over the HTTP
worker protocol, the real yt-dlp downloading a rate-limited public file,
and `kill -9` on the worker that held the job. The core logged the
expired lease, requeued the job, and the second worker delivered the
file. `deploy/acceptance/kill-worker.sh` does this in both modes;
`MODE=compose` is the untested one.
- **Postgres was tested against a real Postgres 15**, started as a private
instance. The whole test suite, end-to-end flows included, passes on
SQLite and on Postgres.
- **S3 was tested against an in-memory S3 server** (`gofakes3`), not
against Garage, MinIO or AWS. The client is `minio-go`, which those
servers are routinely used with.
- **The local Bot API server was not run.** Support for it is
configuration: `TELEGRAM_API_URL` and `TELEGRAM_MAX_UPLOAD_MB`. Uploads
are streamed, never buffered, so a 2 GB file does not need 2 GB of RAM.
### Database
- One schema and one set of queries for both engines. Queries use `?`
placeholders that are rewritten to `$n` for Postgres.
- **Postgres needed two explicit locks that SQLite gives for free.**
SQLite runs write transactions one at a time; Postgres runs them side by
side. A per-user advisory lock guards "count active jobs, then insert
one", and a row lock on the job guards every "read the status, then act
on it". Without the second, cancelling a job could cross with a worker
claiming it and leave a job running for nobody. The test suite found
that on its first Postgres run.
- One core per database. Nothing coordinates two cores.
- No data migration between SQLite and Postgres.
### Workers
- **Tokens** are random, per worker, stored as SHA-256 hashes, and
revocable. They are created with `core worker add <name>` (a CLI
subcommand rather than a bot command, so a token never passes through
Telegram) or listed in `WORKER_TOKENS` for fixed setups.
- A worker's identity comes from its token. The `worker_id` field in a
request body is ignored.
- **Scope:** a token can claim, heartbeat, complete, fail and upload, and
only for jobs leased to that worker. The storage key of a result is
chosen by the core.
- **Tags** are matched exactly: a job goes to a worker that has all of the
site's tags. A claim looks at the first 25 queued jobs to find one the
worker qualifies for, so a job waiting for a rare tag does not block the
queue behind it.
- **Draining** is a flag on the core, checked on every claim. A draining
worker gets neither jobs nor lookups.
- **Lease expiry counts as an attempt; a clean shutdown does not.**
- **Fleet capacity** for the reserved free slot is still counted in memory
from recent claims.
### Link lookups
- With remote workers only, the core has no yt-dlp, so lookups for cards
are handed to workers through their claim calls (`PROBE_MODE`). They are
held in memory: a core restart drops a pending lookup and the user sends
the link again. Workers keep two extra claim requests open for lookups,
so cards stay quick when all download slots are busy.
- Lookups follow the same tag rules as downloads.
### Storage
- With S3, workers upload through presigned URLs and the core is not in
the data path. With local disk, they stream the file to the core.
- An upload of unknown size is spooled to a temp file first, to avoid
chunked streaming uploads that not every S3 compatible server accepts.
- The core's upload endpoint enforces the job's size cap while receiving.
### yt-dlp updates
- Each worker updates its own yt-dlp on a schedule (`yt-dlp -U`).
`/ytdlp update` is delivered to workers with their next claim, so it
reaches every connected worker within seconds.