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
+88
View File
@@ -0,0 +1,88 @@
package app
import (
"context"
"errors"
"fmt"
"io"
"regexp"
"time"
"github.com/noderunners/dlbot/internal/api"
"github.com/noderunners/dlbot/internal/config"
"github.com/noderunners/dlbot/internal/db"
)
var workerNameRe = regexp.MustCompile(`^[a-z0-9][a-z0-9._-]{0,39}$`)
// WorkerAdmin runs the "core worker ..." subcommands. They only need the
// database, so they work while the core is running.
//
// core worker add <name> create a worker and print its token once
// core worker revoke <name> make its token useless
// core worker list show all workers
func WorkerAdmin(ctx context.Context, cfg config.Config, args []string, out io.Writer) error {
usage := errors.New("usage: core worker add <name> | revoke <name> | list")
if len(args) == 0 {
return usage
}
d, err := db.Open(ctx, cfg.Database.Driver, cfg.Database.DSN)
if err != nil {
return err
}
defer d.Close()
if err := d.Migrate(ctx); err != nil {
return err
}
store := d.Store()
switch {
case args[0] == "add" && len(args) == 2:
name := args[1]
if !workerNameRe.MatchString(name) {
return errors.New("worker names are lowercase letters, digits, dot, dash and underscore, at most 40 characters")
}
token, hash := api.NewWorkerToken()
if err := store.UpsertWorker(ctx, name, hash); err != nil {
return err
}
fmt.Fprintf(out, "Worker %q is registered. Its token is shown once, store it now:\n\n WORKER_TOKEN=%s\n\n", name, token)
fmt.Fprintln(out, "Adding a worker that already exists replaces its token.")
return nil
case args[0] == "revoke" && len(args) == 2:
ok, err := store.RevokeWorker(ctx, args[1])
if err != nil {
return err
}
if !ok {
return fmt.Errorf("no worker named %q", args[1])
}
fmt.Fprintf(out, "Worker %q is revoked. Its running jobs return to the queue when their lease ends.\n", args[1])
return nil
case args[0] == "list" && len(args) == 1:
ws, err := store.ListWorkers(ctx)
if err != nil {
return err
}
if len(ws) == 0 {
fmt.Fprintln(out, "No workers are registered.")
return nil
}
fmt.Fprintf(out, "%-24s %-10s %-9s %-8s %-14s %s\n", "NAME", "STATE", "CAPACITY", "SEEN", "YT-DLP", "TAGS")
for _, w := range ws {
state := "active"
switch {
case w.Revoked:
state = "revoked"
case w.Draining:
state = "draining"
}
seen := "never"
if w.LastSeenAt > 0 {
seen = time.Since(time.UnixMilli(w.LastSeenAt)).Round(time.Second).String()
}
fmt.Fprintf(out, "%-24s %-10s %-9d %-8s %-14s %s\n", w.ID, state, w.Capacity, seen, w.Version, w.Tags)
}
return nil
}
return usage
}