- Live job card widget (progress bar, in-widget cancel via widget_query) and live invoice widget (QR drawn from a bitmap, countdown, paid state), Bot API 10.4 sendWidget, private chats only - Automatic fallback to the ordinary message when sendWidget fails, with a breaker that pauses widgets after repeated failures or on servers without the method; TELEGRAM_WIDGETS - Prometheus metrics on the core (/metrics) and on every worker: queue depth and wait, slots, heartbeats, job outcomes, errors per site, deliveries, cache, invoices, Lightning, ledger check, widgets - Example Prometheus config and alert rules, Caddyfile, dashboard notes - Load test: cmd/loadtest and deploy/loadtest (make loadtest) - Browser check of the widgets' HTML, including decoding a drawn QR - docs/DEPLOY.md (production in under an hour) and docs/MONITORING.md Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
247 lines
8.9 KiB
Markdown
247 lines
8.9 KiB
Markdown
# Workers
|
|
|
|
A worker downloads. It runs yt-dlp and ffmpeg, uploads the result and
|
|
reports back. Capacity grows by starting more workers, on the same machine
|
|
as the core or anywhere else.
|
|
|
|
## What a worker is, and is not
|
|
|
|
- **Stateless.** Nothing on a worker matters after a job is done. Its temp
|
|
directory is wiped at startup.
|
|
- **Outbound only.** A worker calls the core over HTTPS. The core never
|
|
connects to a worker, so workers need no inbound ports, no public address
|
|
and no VPN. A machine behind home NAT works.
|
|
- **Unprivileged.** A worker has one secret: its own token. It never sees
|
|
the bot token, the database, or the payment key. The token lets it claim
|
|
jobs, report on its own jobs, and upload their results. Nothing else.
|
|
|
|
By default the core runs one worker inside its own process
|
|
(`WORKER_EMBEDDED=true`). That is all a small bot needs. Read on when you
|
|
want more capacity, or downloads from a different network than the core's.
|
|
|
|
## Add a remote worker
|
|
|
|
### 1. Create a token on the core
|
|
|
|
```sh
|
|
core worker add eu-1
|
|
```
|
|
|
|
```
|
|
Worker "eu-1" is registered. Its token is shown once, store it now:
|
|
|
|
WORKER_TOKEN=dlw_3f9c...
|
|
```
|
|
|
|
Only a hash of the token is stored. Run the command on the machine (or in
|
|
the container) of the core; it only needs the database, so the core can
|
|
keep running.
|
|
|
|
With docker compose the two bundled workers get their tokens from `.env`
|
|
instead (`WORKER_A_TOKEN`, `WORKER_B_TOKEN`), which the core registers at
|
|
startup through `WORKER_TOKENS`. Any random string of 16 characters or more
|
|
works: `openssl rand -hex 24`.
|
|
|
|
### 2. Start the worker
|
|
|
|
The worker needs `yt-dlp` and `ffmpeg`, the URL of the core, and its token.
|
|
|
|
```sh
|
|
CORE_URL=https://bot.example.com \
|
|
WORKER_TOKEN=dlw_3f9c... \
|
|
WORKER_TAGS=residential,eu \
|
|
WORKER_TEMP_DIR=/var/lib/dlbot-worker/tmp \
|
|
worker
|
|
```
|
|
|
|
Or as a container:
|
|
|
|
```sh
|
|
docker build -f deploy/Dockerfile --target worker -t dlbot-worker .
|
|
docker run -d --name dlbot-worker --restart unless-stopped \
|
|
-e CORE_URL=https://bot.example.com \
|
|
-e WORKER_TOKEN=dlw_3f9c... \
|
|
-e WORKER_TAGS=residential,eu \
|
|
dlbot-worker
|
|
```
|
|
|
|
Or with systemd: see `deploy/systemd/dlbot-worker.service`.
|
|
|
|
`CORE_URL` must reach the core's HTTP port (`HTTP_LISTEN`, 8080 by default).
|
|
Across the internet, put the core behind a TLS reverse proxy and use
|
|
`https://`. The token travels in a header and must not cross the internet
|
|
in the clear.
|
|
|
|
### 3. Check that it arrived
|
|
|
|
Send `/workers` to the bot as an owner, or run `core worker list`:
|
|
|
|
```
|
|
NAME STATE CAPACITY SEEN YT-DLP TAGS
|
|
eu-1 active 2 3s 2026.10.26 eu,residential
|
|
```
|
|
|
|
## Worker settings
|
|
|
|
| Variable | Default | Meaning |
|
|
| --- | --- | --- |
|
|
| `CORE_URL` | none | Base URL of the core. Required. |
|
|
| `WORKER_TOKEN` | none | This worker's token. Required. |
|
|
| `WORKER_TAGS` | none | Comma separated tags, see below. |
|
|
| `WORKER_CONCURRENCY` | `2` | Downloads at the same time. |
|
|
| `WORKER_TEMP_DIR` | `data/worker-tmp` | Scratch space. One per worker process. |
|
|
| `WORKER_PROBE_SLOTS` | `2` | Link lookups running next to the downloads. |
|
|
| `WORKER_HEARTBEAT` | `10s` | Must be well under the core's `QUEUE_LEASE`. |
|
|
| `WORKER_HEALTH_LISTEN` | `:8081` | `/healthz` and `/metrics`, or `off`. See [MONITORING.md](MONITORING.md). |
|
|
| `YTDLP_PATH` | `yt-dlp` | |
|
|
| `FFMPEG_PATH` | `ffmpeg` | `ffprobe` is expected next to it. |
|
|
| `YTDLP_EXTRA_ARGS` | none | Added to every yt-dlp call, for example `--proxy socks5://127.0.0.1:1080`. |
|
|
| `YTDLP_AUTO_UPDATE` | `24h` | How often yt-dlp updates itself. `0` turns it off. |
|
|
| `DOWNLOAD_TIMEOUT` | `30m` | Hard cap for one download. |
|
|
| `PROBE_TIMEOUT` | `45s` | Cap for one link lookup. |
|
|
| `LOG_LEVEL`, `LOG_FORMAT` | `info`, `json` | |
|
|
|
|
Several workers on one machine each need their own token, their own
|
|
`WORKER_TEMP_DIR` and their own `WORKER_HEALTH_LISTEN` port (or `off`).
|
|
|
|
## Tags: routing sites to the right network
|
|
|
|
Some sites refuse downloads from datacenter addresses. YouTube answers
|
|
"Sign in to confirm you are not a bot" there. The fix is a worker on a
|
|
network the site accepts, and a rule that sends that site's jobs to it.
|
|
|
|
1. Start a worker on a home connection with `WORKER_TAGS=residential`.
|
|
2. On the core, say which sites need the tag:
|
|
|
|
```sh
|
|
SITE_TAGS="youtube.com=residential;x.com=residential"
|
|
```
|
|
|
|
or in `config.toml`:
|
|
|
|
```toml
|
|
[site_tags]
|
|
"youtube.com" = ["residential"]
|
|
"x.com" = ["residential"]
|
|
```
|
|
|
|
A rule for a host covers its subdomains. A job for a listed site is only
|
|
given to workers that advertise every tag of the rule. This also applies
|
|
to the link lookup that builds the card. Sites without a rule go to any
|
|
worker.
|
|
|
|
Tags are free-form: `residential`, `eu`, `us`, `fast`, `bigdisk`. A worker
|
|
with more tags than a job needs still qualifies.
|
|
|
|
If no connected worker has a required tag, jobs for that site wait in the
|
|
queue and lookups time out. `/workers` shows the tags of each worker.
|
|
|
|
## Where link lookups run
|
|
|
|
A card needs yt-dlp metadata before any job exists. `PROBE_MODE` on the
|
|
core decides who fetches it:
|
|
|
|
| Value | Who looks links up |
|
|
| --- | --- |
|
|
| `auto` (default) | The core itself when `WORKER_EMBEDDED=true`, remote workers otherwise. |
|
|
| `local` | Always the core. It needs yt-dlp. |
|
|
| `worker` | Always remote workers. The core needs no yt-dlp. |
|
|
|
|
Use `worker` when the core sits on a network sites refuse but you still
|
|
keep the embedded worker for other sites.
|
|
|
|
## Draining and removing a worker
|
|
|
|
To take a worker out for maintenance without losing its downloads, as an
|
|
owner in the bot:
|
|
|
|
```
|
|
/drain eu-1 it finishes its jobs and gets no new ones
|
|
/drain eu-1 off it takes jobs again
|
|
```
|
|
|
|
Stopping a worker with SIGTERM (`systemctl stop`, `docker stop`) is also
|
|
safe: it stops its downloads and hands the jobs back to the queue at once,
|
|
and another worker picks them up without the retry being used up.
|
|
|
|
To lock a worker out for good:
|
|
|
|
```sh
|
|
core worker revoke eu-1
|
|
```
|
|
|
|
Its token stops working immediately. `core worker add eu-1` issues a new
|
|
token and brings it back.
|
|
|
|
## What happens when a worker dies
|
|
|
|
A claimed job is on lease. The worker extends the lease with a heartbeat
|
|
every 10 seconds. When heartbeats stop (power cut, crash, lost network) the
|
|
lease runs out after `QUEUE_LEASE` (30 seconds), the core puts the job back
|
|
in the queue, and another worker takes it. The requester sees the status
|
|
go back to "queued" and then on to "done". A worker that comes back late is
|
|
told the job is no longer its own and drops it.
|
|
|
|
A lost lease counts as an attempt, so a job that keeps killing workers is
|
|
given up on after `QUEUE_MAX_ATTEMPTS` (2).
|
|
|
|
You can watch all of this happen:
|
|
|
|
```sh
|
|
make acceptance # real processes
|
|
MODE=compose make acceptance # the same in containers
|
|
```
|
|
|
|
The script starts the core and two workers, has a user ask for a file,
|
|
kills the worker that is downloading it with SIGKILL, and waits for the
|
|
other worker to deliver it.
|
|
|
|
## How files get from a worker to Telegram
|
|
|
|
The worker uploads the finished file to storage, the core sends it to
|
|
Telegram.
|
|
|
|
- **Local disk storage:** the worker streams the file to the core
|
|
(`PUT /worker/v1/upload/<job>`), which writes it to its disk.
|
|
- **S3 storage:** the core gives the worker a presigned URL with the job,
|
|
and the file goes straight to the bucket. The core is not in the data
|
|
path. Workers must be able to reach the bucket; if they reach it under a
|
|
different address than the core does, set `S3_PRESIGN_ENDPOINT`. Set
|
|
`S3_PRESIGN=false` to route files through the core anyway.
|
|
|
|
## Keeping yt-dlp current
|
|
|
|
Sites change and yt-dlp follows weekly. Each worker updates its own yt-dlp
|
|
every `YTDLP_AUTO_UPDATE` (24 hours). For an urgent fix, as an owner:
|
|
|
|
```
|
|
/ytdlp update
|
|
```
|
|
|
|
Every connected worker updates when it next checks for work, which is
|
|
within seconds. `/workers` shows the version each one runs.
|
|
|
|
Self-update works for the standalone yt-dlp binaries (what the Docker
|
|
image uses). A yt-dlp installed by a package manager is updated by that
|
|
package manager instead.
|
|
|
|
## The protocol
|
|
|
|
All calls are HTTPS POST with JSON bodies and
|
|
`Authorization: Bearer <token>`, except the two file routes.
|
|
|
|
| Route | Purpose |
|
|
| --- | --- |
|
|
| `POST /worker/v1/claim` | Take a job or a lookup. Held open up to 25 seconds when there is none. The worker sends its tags and capacity. |
|
|
| `POST /worker/v1/heartbeat` | Extend the lease, report progress. The answer says whether to cancel. |
|
|
| `POST /worker/v1/complete` | Report a result that is already uploaded. |
|
|
| `POST /worker/v1/fail` | Report a failure with an error class and whether it is worth retrying. |
|
|
| `PUT /worker/v1/upload/<job>` | Send the result file (local disk storage). |
|
|
| `GET /worker/v1/source/<job>` | Fetch the stored file a transcode job starts from. |
|
|
| `POST /worker/v1/probe` | Answer a link lookup. |
|
|
|
|
Status `401` means the token is unknown or revoked. Status `409` means the
|
|
job is not (or no longer) leased to this worker, and the worker drops it.
|
|
The worker's identity always comes from its token, never from the request
|
|
body, and the storage key of a result is fixed by the core.
|