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
+28 -6
View File
@@ -5,6 +5,7 @@ import (
"errors"
"strconv"
"strings"
"time"
"github.com/mymmrac/telego"
@@ -62,6 +63,18 @@ func (b *Bot) handleMessage(ctx context.Context, m *telego.Message) {
if b.isOwner(m) {
b.cmdCache(ctx, m, act)
}
case "workers":
if b.isOwner(m) {
b.cmdWorkers(ctx, m, act)
}
case "drain":
if b.isOwner(m) {
b.cmdDrain(ctx, m, act)
}
case "ytdlp":
if b.isOwner(m) {
b.cmdYtDlp(ctx, m, act)
}
case "info":
b.cmdInfo(ctx, m, act)
case "queue":
@@ -297,21 +310,25 @@ func (b *Bot) cmdQueue(ctx context.Context, m *telego.Message, act actor) {
}
func (b *Bot) cmdCancel(ctx context.Context, m *telego.Message, act actor) {
n := 0
// Playlist items that have not started go first. Cancelling a running
// download frees a slot, and the next waiting item would take it.
b.statusMu.Lock()
b.cancelledAt[act.user.ID] = time.Now()
b.statusMu.Unlock()
if waiting, err := b.store.CancelWaitingRequests(ctx, act.user.ID); err == nil {
n += int(waiting)
}
dels, err := b.store.ActiveDeliveriesForUser(ctx, act.user.ID)
if err != nil {
b.log.Error("list deliveries", "err", err)
return
}
n := 0
for _, d := range dels {
if b.cancelDelivery(ctx, d.ID, act.user.ID) == nil {
n++
}
}
// Playlist items that have not started yet are cancelled too.
if waiting, err := b.store.CancelWaitingRequests(ctx, act.user.ID); err == nil {
n += int(waiting)
}
key := "cancel.done"
if n == 0 {
key = "cancel.nothing"
@@ -337,11 +354,16 @@ func (b *Bot) cancelDelivery(ctx context.Context, deliveryID string, userID int6
// release gives back what an undelivered download took and returns a line
// to append to the status when sats were refunded.
func (b *Bot) release(ctx context.Context, d *db.Delivery) string {
refunded, err := b.svc.Release(context.WithoutCancel(ctx), d.ID)
bg := context.WithoutCancel(ctx)
refunded, err := b.svc.Release(bg, d.ID)
if err != nil {
b.log.Error("release delivery", "delivery_id", d.ID, "err", err)
return ""
}
if refunded <= 0 {
// Another path may have released it a moment ago.
refunded = b.svc.Refunded(bg, d.ID)
}
if refunded <= 0 {
return ""
}