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
+54 -5
View File
@@ -8,23 +8,27 @@ phase; sections for later phases say so.
```
Telegram <----> core <---- claim / heartbeat / complete / fail ---- worker(s)
| \ |
| \__ storage (local disk today, S3 in Phase 4) <___/
| \__ storage (local disk or S3) <__________________/
|
database (SQLite today, Postgres in Phase 4)
database (SQLite or Postgres)
```
- **core** (`cmd/core`) talks to Telegram, owns the database and the queue,
serves HTTP and sends finished files. It is the only process that holds
the bot token.
- **worker** pulls jobs, runs yt-dlp and ffmpeg, uploads the result to
storage and reports back. It is stateless. Today it runs inside the core
process; the code is already split along the future network boundary.
storage and reports back. It is stateless. One runs inside the core
process by default (`WORKER_EMBEDDED`); any number of others run as the
`cmd/worker` binary anywhere that can reach the core over HTTPS.
## Packages
| Package | Role |
| --- | --- |
| `cmd/core` | Process entry point. Loads config, starts `internal/app`. |
| `cmd/core` | Process entry point. Loads config, starts `internal/app`. Also `core worker add/revoke/list`. |
| `cmd/worker` | The standalone download worker. |
| `cmd/faketelegram` | A fake Bot API with a control API, for development and the acceptance script. |
| `internal/probe` | Hands link lookups to remote workers and waits for the answer. |
| `internal/app` | Wires everything together. Tests start the bot through it too. |
| `internal/config` | Defaults, optional TOML file, environment overrides, validation. |
| `internal/bot` | Telegram front end: update routing, cards, live status, delivery. |
@@ -119,6 +123,51 @@ that a file is sent by one goroutine only.
- A worker that loses its lease is told so on its next call and drops the
job without reporting a result.
## Scale out
### Two transports, one protocol
The worker loop talks to a `Coordinator` interface: claim, heartbeat,
complete, fail, upload, fetch. `sched.Local` implements it with direct
calls for the embedded worker. `worker.HTTPCoordinator` implements it over
the core's `/worker/v1/` routes for remote workers. Both carry the same
`workerproto` messages, so the scheduler does not know or care where a
worker runs.
### Trust
A remote worker is authenticated by a bearer token that only exists hashed
in the database. The API handler resolves the token to a worker and
overwrites the `worker_id` of every request with it. A job action is only
accepted for a job whose lease is held by that worker, and a completed
job's storage key is derived by the core from the job ID. A worker can
therefore fail or stall its own jobs, which the lease handles, but cannot
touch anyone else's.
### Tags
`site_tags` maps hosts to the worker tags they require. `service.Start`
writes the required tags on the job; a claim only returns jobs whose tags
the claiming worker has. The same rule routes link lookups.
### Link lookups without yt-dlp on the core
`probe.Broker` implements the same `Probe` call as the local yt-dlp
runner. It parks the request, wakes waiting claim calls, and a worker that
asked with `probe_only` takes it, runs yt-dlp and posts the info JSON
back. The service does not know which of the two it is talking to.
### Databases
`internal/db` has one set of queries. Three things differ by dialect:
placeholders (`?` rewritten to `$n`), claiming (`FOR UPDATE SKIP LOCKED` on
Postgres, an immediate transaction on SQLite), and two locks Postgres
needs because it runs transactions concurrently: `LockUser` (advisory) and
`LockJob` (row). Both are no-ops on SQLite.
The test suite runs unchanged on both: `dbtest` gives each test a SQLite
file or, with `DLBOT_TEST_DB=postgres`, its own Postgres schema.
## Events and reconciliation
The scheduler notifies the bot through an in-process callback after each