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
+27 -3
View File
@@ -3,7 +3,7 @@
A Telegram bot that downloads video and audio from links, in private chats
and in groups. Written in Go on top of yt-dlp.
> Status: Phase 3 of 8 (core bot, payments, cache). Remote workers, group
> Status: Phase 4 of 8 (core bot, payments, cache, scale-out). Group
> settings, the Mini App and more languages follow. See
> [docs/ASSUMPTIONS.md](docs/ASSUMPTIONS.md) for the decisions made so far.
@@ -29,9 +29,23 @@ and in groups. Written in Go on top of yt-dlp.
copy. A cache hit is checked against exactly the same limits as a fresh
download.
- Playlists (each item its own job), subtitles, download history.
- Scale out: stateless workers that pull jobs over HTTPS with no inbound
ports, per-worker revocable tokens, tags to route sites to the right
network, drain, automatic requeue when a worker dies.
- SQLite or Postgres, local disk or S3, chosen by configuration.
- Friendly, translated error messages. Raw tool output stays in the logs.
## Quick start
## Quick start with docker compose
```sh
cp .env.example .env # set BOT_TOKEN, OWNER_IDS, WORKER_A_TOKEN, WORKER_B_TOKEN
docker compose up -d --build
```
This runs the core and two workers. [docs/OPERATIONS.md](docs/OPERATIONS.md)
covers Postgres, S3, 2 GB uploads and TLS.
## Quick start from source
You need Go 1.26.11 or newer (older Go toolchains download it on their
own), plus `yt-dlp` and `ffmpeg` on the PATH.
@@ -87,6 +101,9 @@ Owner commands (private chat, only for the IDs in `OWNER_IDS`):
| --- | --- |
| `/purge <link>` | Take a video down: delete its files, block it for good |
| `/cache` | Cache size, hit rate and volume saved |
| `/workers` | Workers, their state, load, tags and yt-dlp version |
| `/drain <worker>` | Let a worker finish its jobs and take no new ones |
| `/ytdlp update` | Update yt-dlp on every worker now |
### Turning on payments
@@ -133,6 +150,8 @@ The settings you are most likely to touch:
make check # gofmt, go vet, tests with the race detector
make test # tests only
make e2e # real yt-dlp against a public test file, needs network
make test-postgres PG_DSN=postgres://... # the same suite on Postgres
make acceptance # kill a worker mid-job, watch the other finish
```
The tests need neither a bot token nor network access. They run the real
@@ -141,13 +160,18 @@ yt-dlp.
## Documentation
- [docs/OPERATIONS.md](docs/OPERATIONS.md): deploying and running it.
- [docs/WORKERS.md](docs/WORKERS.md): adding download workers, tags, drain.
- [docs/ARCHITECTURE.md](docs/ARCHITECTURE.md): how the pieces fit.
- [docs/ASSUMPTIONS.md](docs/ASSUMPTIONS.md): decisions and verified facts.
## Layout
```
cmd/core the bot gateway (and, for now, the embedded worker)
cmd/core the bot gateway, with an embedded worker by default
cmd/worker the standalone download worker
cmd/faketelegram a fake Bot API for development and acceptance runs
deploy Dockerfile, systemd units, acceptance script
internal/bot Telegram handlers, cards, status, delivery
internal/service download rules shared by all front ends
internal/entitle who pays and which limits apply (pure function)