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:
co-authored by
Claude Fable 5.2
parent
9c9d641eda
commit
7d4893bc3f
@@ -0,0 +1,186 @@
|
||||
# Operations
|
||||
|
||||
How to deploy, scale and look after the bot. For workers see
|
||||
[WORKERS.md](WORKERS.md).
|
||||
|
||||
## Deploy with docker compose
|
||||
|
||||
You need Docker with the compose plugin, and a bot token from @BotFather.
|
||||
|
||||
```sh
|
||||
git clone <this repository> dlbot && cd dlbot
|
||||
cp .env.example .env
|
||||
```
|
||||
|
||||
Edit `.env`. The minimum is:
|
||||
|
||||
```sh
|
||||
BOT_TOKEN=123456:ABC...
|
||||
OWNER_IDS=<your Telegram user id>
|
||||
WORKER_A_TOKEN=<openssl rand -hex 24>
|
||||
WORKER_B_TOKEN=<openssl rand -hex 24>
|
||||
```
|
||||
|
||||
Then:
|
||||
|
||||
```sh
|
||||
docker compose up -d --build
|
||||
docker compose ps # core, worker-a, worker-b should be healthy
|
||||
curl localhost:8080/healthz # {"status":"ok"}
|
||||
```
|
||||
|
||||
Open the bot in Telegram, press Start, send a link. As an owner, `/workers`
|
||||
shows both workers.
|
||||
|
||||
The compose file runs the core without an embedded worker and two separate
|
||||
workers. For a single small machine you can instead run only the core with
|
||||
`WORKER_EMBEDDED=true` and no remote workers.
|
||||
|
||||
## Deploy without Docker
|
||||
|
||||
Build `bin/core` and `bin/worker` with `make build`, install `yt-dlp` and
|
||||
`ffmpeg`, and use the units in `deploy/systemd/`. Each unit file starts
|
||||
with the commands to install it.
|
||||
|
||||
## Choosing the database
|
||||
|
||||
**SQLite** is the default and needs nothing. It is a file
|
||||
(`DB_DSN=data/dlbot.db`) and handles a busy bot on one machine. Back it up
|
||||
with `sqlite3 dlbot.db ".backup backup.db"`, not with `cp`.
|
||||
|
||||
**Postgres** is for when the core should be restartable on another machine,
|
||||
or when you want the database on managed infrastructure.
|
||||
|
||||
```sh
|
||||
DB_DRIVER=postgres
|
||||
DB_DSN=postgres://dlbot:<password>@postgres:5432/dlbot?sslmode=disable
|
||||
POSTGRES_PASSWORD=<password>
|
||||
docker compose --profile postgres up -d
|
||||
```
|
||||
|
||||
The schema is the same on both and migrations run at startup. There is no
|
||||
tool to move data from one to the other; choose before you have users, or
|
||||
accept starting fresh.
|
||||
|
||||
Run only **one core** per database. Workers scale out; the core does not.
|
||||
|
||||
## Choosing storage
|
||||
|
||||
**Local disk** (default): files live under `STORAGE_LOCAL_DIR`. Workers
|
||||
send finished files through the core.
|
||||
|
||||
**S3 compatible** (AWS S3, Garage, MinIO): set
|
||||
|
||||
```sh
|
||||
STORAGE_BACKEND=s3
|
||||
S3_ENDPOINT=https://s3.example.com
|
||||
S3_BUCKET=dlbot
|
||||
S3_ACCESS_KEY=...
|
||||
S3_SECRET_KEY=...
|
||||
S3_REGION=garage # whatever your server expects
|
||||
S3_PATH_STYLE=true # Garage and MinIO: true. AWS: false.
|
||||
```
|
||||
|
||||
The bucket must exist. With `S3_PRESIGN=true` (default) workers upload
|
||||
straight to the bucket with presigned URLs. If workers reach the bucket
|
||||
under another address than the core, set `S3_PRESIGN_ENDPOINT`.
|
||||
|
||||
Storage holds the cache's stored copies. `CACHE_BUDGET_MB` caps it; the
|
||||
sweeper evicts the least used files when it is exceeded.
|
||||
|
||||
## Files up to 2 GB: the local Bot API server
|
||||
|
||||
The cloud Bot API accepts uploads up to 50 MB. A local Bot API server
|
||||
lifts that to 2000 MB.
|
||||
|
||||
1. Get `api_id` and `api_hash` at https://my.telegram.org.
|
||||
2. If the bot ever ran against the cloud API, log it out there once:
|
||||
`curl https://api.telegram.org/bot<TOKEN>/logOut`. A bot can be logged
|
||||
in to one server at a time.
|
||||
3. In `.env`:
|
||||
|
||||
```sh
|
||||
TELEGRAM_API_ID=...
|
||||
TELEGRAM_API_HASH=...
|
||||
TELEGRAM_API_URL=http://telegram-bot-api:8081
|
||||
TELEGRAM_MAX_UPLOAD_MB=2000
|
||||
```
|
||||
|
||||
4. `docker compose --profile local-api up -d`
|
||||
|
||||
`TELEGRAM_MAX_UPLOAD_MB` is the ceiling for everyone. Passes allow up to
|
||||
`PAID_MAX_SIZE_MB` (2000) but never more than this.
|
||||
|
||||
## Public address, webhooks and TLS
|
||||
|
||||
The core listens on `HTTP_LISTEN` (`:8080`). Compose binds it to localhost.
|
||||
Put a TLS reverse proxy (Caddy, nginx, Traefik) in front and set
|
||||
`PUBLIC_URL=https://bot.example.com` when you want any of:
|
||||
|
||||
- instant payment settlement (LNbits webhook) and the "Open in wallet" button
|
||||
- remote workers across the internet
|
||||
- Telegram webhooks instead of long polling (`TELEGRAM_MODE=webhook`,
|
||||
`WEBHOOK_URL`, `WEBHOOK_SECRET`)
|
||||
|
||||
Routes the proxy must pass through:
|
||||
|
||||
| Path | Who calls it |
|
||||
| --- | --- |
|
||||
| `/telegram/webhook` | Telegram (webhook mode only) |
|
||||
| `/lnbits/webhook/<secret>` | LNbits |
|
||||
| `/pay/<id>` | Users pressing "Open in wallet" |
|
||||
| `/worker/v1/...` | Remote workers. Allow long requests (30 s) and large uploads. |
|
||||
| `/healthz` | Your monitoring |
|
||||
|
||||
Without `PUBLIC_URL` the bot still works: long polling for Telegram,
|
||||
polling for payments, workers on the same network.
|
||||
|
||||
## Health and logs
|
||||
|
||||
- `GET /healthz` on the core answers 200 while the database is reachable.
|
||||
- `GET /healthz` on a worker (`WORKER_HEALTH_LISTEN`, `:8081`) answers 200
|
||||
while the core accepts its token and answers its calls.
|
||||
- Logs are JSON on stderr, one object per line (`LOG_FORMAT=text` for
|
||||
reading by eye). Every job line carries `job_id`. The bot token, payment
|
||||
keys and worker tokens are removed from all log output, and URLs are
|
||||
logged without credentials or signed parameters.
|
||||
- Raw yt-dlp errors are in the log under `detail`. Users only ever see the
|
||||
friendly message.
|
||||
|
||||
## Owner commands
|
||||
|
||||
Only in a private chat, only for the user IDs in `OWNER_IDS`.
|
||||
|
||||
| Command | What it does |
|
||||
| --- | --- |
|
||||
| `/workers` | Workers, their state, load, tags and yt-dlp version |
|
||||
| `/drain <worker>` | Finish current jobs, take no new ones. `/drain <worker> off` undoes it. |
|
||||
| `/ytdlp update` | Update yt-dlp on every worker now |
|
||||
| `/cache` | Cache size, hit rate, volume saved |
|
||||
| `/purge <link>` | Take a video down: delete its files and block it for good |
|
||||
|
||||
## Backups
|
||||
|
||||
Back up the database. It holds wallets and the ledger, which are money.
|
||||
Storage only holds cached files and can be lost without harm: the bot
|
||||
downloads again.
|
||||
|
||||
## Updating
|
||||
|
||||
```sh
|
||||
git pull
|
||||
docker compose up -d --build
|
||||
```
|
||||
|
||||
The core stops cleanly on SIGTERM: workers hand their running jobs back to
|
||||
the queue, and the next start resumes deliveries, refunds and unsettled
|
||||
invoices from the database.
|
||||
|
||||
## Checking a deployment
|
||||
|
||||
```sh
|
||||
make acceptance # or MODE=compose make acceptance
|
||||
```
|
||||
|
||||
runs the core with two workers against a fake Telegram, kills a worker in
|
||||
the middle of a download and checks that the other one delivers the file.
|
||||
Reference in New Issue
Block a user